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); }
private void buttonFire_Click(object sender, EventArgs e) { // This event handles what to do when a player fires. // After the player fires it lets the computer firing using ComputerTurn(). // It also checks if there is a winner. bool isHit; //LINQ to find which radio is checked var checkedRadio = tableLayoutPanel1.Controls.OfType <RadioButton>() .FirstOrDefault(r => r.Checked); checkedRadio.Checked = false; //unchecks the selected radio button checkedRadio.Enabled = false; //disables the selected radio button int controlRow = tableLayoutPanel1.GetRow(checkedRadio); //gets radiobutton row int controlColumn = tableLayoutPanel1.GetColumn(checkedRadio); //gets radiobutton column //checks to see if the any ships are hit from the player firing. This method returns a bool. //If any ships are hit that is also handled in GameMechanics.FireIsHit. isHit = GameMechanics.FireIsHit(ref computerShips, ref playerShotsFired, ref playerShotsFiredCount, controlColumn, controlRow); lbMoves.Items.Add("Player fires at " + ALPHA[controlRow] + "-" + (controlColumn + 1) + ". Was a ship hit: " + isHit); buttonFire.Enabled = false; //disabling button tableLayoutPanel1.Refresh(); //redraws tablelayoutpanel so cells can be painted //computer can only go if player hasn't won if (isWinner == false) { ComputerTurn(); } tableLayoutPanel2.Refresh(); //redraws tablelayoutpanel so cells can be painted }