using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace BattleShip_Game
public partial class Form1 : Form
List<Button> playerPosition;
List<Button> enemyPosition;
Random rand = new Random();
int playerTotalScore = 0;
attackButton.Enabled = false;
enemyLocationList.Text = null;
private void playerPicksPosition(object sender, EventArgs e)
var button = (Button)sender;
button.Tag = "playerShip";
button.BackColor = System.Drawing.Color.Blue;
attackButton.Enabled = true;
attackButton.BackColor = System.Drawing.Color.Red;
helpText.Text = "2) Now pick a attack position from the drop down";
private void attackEnemyPosition(object sender, EventArgs e)
if (enemyLocationList.Text != "")
var attackPos = enemyLocationList.Text;
attackPos = attackPos.ToLower();
int index = enemyPosition.FindIndex(a => a.Name == attackPos);
if (enemyPosition[index].Enabled && rounds > 0)
roundsText.Text = "Rounds " + rounds;
if (enemyPosition[index].Tag == "enemyship")
enemyPosition[index].Enabled = false;
enemyPosition[index].BackgroundImage = Properties.Resources.fireIcon;
enemyPosition[index].BackColor = System.Drawing.Color.DarkBlue;
playerScore.Text = "" + playerTotalScore;
enemyPosition[index].Enabled = false;
enemyPosition[index].BackgroundImage = Properties.Resources.missIcon;
enemyPosition[index].BackColor = System.Drawing.Color.DarkBlue;
MessageBox.Show("Choose a location from the drop down list. ");
private void enemyAttackPlayer(object sender, EventArgs e)
if (playerPosition.Count > 0 && rounds > 0)
roundsText.Text = "Rounds " + rounds;
int index = rand.Next(playerPosition.Count);
if (playerPosition[index].Tag == "playerShip")
playerPosition[index].BackgroundImage = Properties.Resources.fireIcon;
enemyMoves.Text = "" + playerPosition[index].Text;
playerPosition[index].Enabled = false;
playerPosition[index].BackColor = System.Drawing.Color.DarkBlue;
playerPosition.RemoveAt(index);
enemyScore.Text = "" + enemyTotalScore;
playerPosition[index].BackgroundImage = Properties.Resources.missIcon;
enemyMoves.Text = "" + playerPosition[index].Text;
playerPosition[index].Enabled = false;
playerPosition[index].BackColor = System.Drawing.Color.DarkBlue;
playerPosition.RemoveAt(index);
if (rounds < 1 || playerTotalScore > 2 || enemyTotalScore > 2)
if (playerTotalScore > enemyTotalScore)
MessageBox.Show("You Win", "Winning");
if (playerTotalScore == enemyTotalScore)
MessageBox.Show("No one wins this", "Draw");
if (enemyTotalScore > playerTotalScore)
MessageBox.Show("Haha! I Sunk Your Battle Ship", "Lost");
private void enemyPicksPositions(object sender, EventArgs e)
int index = rand.Next(enemyPosition.Count);
if (enemyPosition[index].Enabled == true && enemyPosition[index].Tag == null)
enemyPosition[index].Tag = "enemyship";
Debug.WriteLine("Enemy Position " + enemyPosition[index].Text);
index = rand.Next(enemyPosition.Count);
enemyPositionPicker.Stop();
private void loadbuttons()
playerPosition = new List<Button>{w1, w2, w3, w4, x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4};
enemyPosition = new List<Button>{a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4};
for (int i = 0; i < enemyPosition.Count; i++)
enemyPosition[i].Tag = null;
enemyLocationList.Items.Add(enemyPosition[i].Text);