public void StartGame(bool firstTime = false) { if (firstTime) { playButton.Hide(); this.BackgroundImage = Properties.Resources.background2; soundButton.Location = new Point(1012, 0); soundButton.Show(); this.Update(); this.BackColor = SystemColors.Control; gameManager = new BattleshipGame(); this.player = gameManager.GetPlayer(); this.computer = gameManager.GetComputer(); DrawBoard(); } else { placedShips.Clear(); placedShipsBoxes.Clear(); computerTurnsCount = 0; computersTurnsCountLabel.Text = computerTurnsCount.ToString(); computersTurnsCountLabel.Hide(); playerTurnsCount = 0; playersTurnsCountLabel.Text = playerTurnsCount.ToString(); playersTurnsCountLabel.Hide(); possibleToPlaceShip = false; debugModeToolStripMenuItem.Enabled = false; backwardButton.Enabled = true; gameStarted = false; foreach (GraphicSquare gs in computerGraphicBoard) { gs.Clickable = false; } gameManager.Reset(); foreach (GraphicSquare temp in computerGraphicBoard) { temp.Reset(); } foreach (GraphicSquare temp in playerGraphicBoard) { temp.Reset(); } for (int i = 0; i < shipsBoxes.Length; i++) { shipsBoxes[i].Reset(); shipsBoxes[i].Enabled = true; } startButton.Enabled = true; } }
private void ComputerTurn() { //This gets the coordinates for the computer to fire at then sends it to see if its a hit. bool isHit; int[] compHitCords = ComputerAI.ComputerMove(computerShotsFired, computerShotsFiredCounter); int compShotRow = compHitCords[0]; int compShotCol = compHitCords[1]; isHit = GameMechanics.FireIsHit(ref playerShips, ref computerShotsFired, ref computerShotsFiredCounter, compShotCol, compShotRow); lbMoves.Items.Add("Computer fires at " + ALPHA[compShotRow] + "-" + (compShotCol + 1) + ". Was a ship hit: " + isHit); }
public BattleshipGame() { player = new Player(this); computer = new ComputerAI(this); turn = player; }
public Form1() { InitializeComponent(); //below is naming the player of the ship and subscribing to each ships event foreach (Ship s in playerShips) { s.ShipSunk += s_ShipSunk; s.Player = Player1Name; } foreach (Ship s in computerShips) { s.ShipSunk += s_ShipSunk; s.Player = Player2Name; } //Below is creating the radio buttons for each cell in the tablelayoutpanels int checkrow = 0; //counter for filling rows int checkcol = 0; //counter for filling columns int countArrayFill = 0; //counts for fill array int counter = 0; //counter for while loops ComputerAI.SettingAIShips(ref computerShips); //setting Computer Ships //creates 144 check boxes RadioButton[] radioButtonsShips = new RadioButton[144]; //array of checkboxes while (countArrayFill < 144) //filling array { RadioButton putIn = new RadioButton(); //creates new checkbox putIn.Text = ""; //removing text putIn.Size = new System.Drawing.Size(14, 15); // resizing putIn.Anchor = AnchorStyles.None; //removing anchors putIn.BackColor = Color.Transparent; //change back color radioButtonsShips[countArrayFill] = putIn; //adding checkbox to array radioButtonsShips[countArrayFill].Name = "rbShip" + countArrayFill; //adding checkbox name countArrayFill++; } //while loop that fills tablelayoutpanel with checkboxes while (checkrow < 12) { while (checkcol < 12) { tableLayoutPanel2.Controls.Add(radioButtonsShips[counter], checkcol, checkrow); checkcol++; counter++; } checkrow++; checkcol = 0; } //creates 144 radio buttons RadioButton[] radioButtons = new RadioButton[144]; // creates an array of radio buttons countArrayFill = 0; //reseting counter while (countArrayFill < 144) //filling array { RadioButton putIn = new RadioButton(); //creating radio button putIn.Text = ""; //removing text putIn.Size = new System.Drawing.Size(14, 15); //resizing putIn.Anchor = AnchorStyles.None; // removing anchor putIn.BackColor = Color.Transparent; //change back color putIn.CheckedChanged += new System.EventHandler(checkBoxUnlockButton); //event to unlock button radioButtons[countArrayFill] = putIn; //adding check box to array radioButtons[countArrayFill].Name = "rbTarget" + countArrayFill; //giving checkbox a name countArrayFill++; } //reseting counters checkrow = 0; checkcol = 0; counter = 0; while (checkrow < 12) //while loop to fill tablelayoutpanel { while (checkcol < 12) { tableLayoutPanel1.Controls.Add(radioButtons[counter], checkcol, checkrow); checkcol++; counter++; } checkrow++; checkcol = 0; } }