private void AttackBtnEvent(object sender, EventArgs e) { if (EnemyLocationBox.Text != "") { var attackPosition = EnemyLocationBox.Text.ToLower(); int index = enemyPositions.FindIndex(position => position.Name == attackPosition); if (enemyPositions[index].Enabled && round > 0) { --round; txtRounds.Text = "Round: " + round; if ((string)enemyPositions[index].Tag == "enemyShip") { enemyPositions[index].Enabled = false; enemyPositions[index].BackgroundImage = Properties.Resources.fireIcon; enemyPositions[index].BackColor = Color.DarkBlue; ++playerScore; txtPlayer.Text = playerScore.ToString(); EnemyPlayTimer.Start(); } else { enemyPositions[index].Enabled = false; enemyPositions[index].BackgroundImage = Properties.Resources.missIcon; enemyPositions[index].BackColor = Color.DarkBlue; EnemyPlayTimer.Start(); } } } else { MessageBox.Show("Choose a location from the drop down first!"); } }
private void EnemyPlayTimerEvent(object sender, EventArgs e) { if (playerPositionButtons.Count > 0 && round > 0) { round -= 1; txtRounds.Text = "Round: " + round; int index = rand.Next(playerPositionButtons.Count); if ((string)playerPositionButtons[index].Tag == "playerShip") { playerPositionButtons[index].BackgroundImage = Properties.Resources.fireIcon; enemyMove.Text = playerPositionButtons[index].Text; playerPositionButtons[index].Enabled = false; playerPositionButtons[index].BackColor = Color.DarkBlue; playerPositionButtons.RemoveAt(index); enemyScore += 1; txtEnemy.Text = enemyScore.ToString(); EnemyPlayTimer.Stop(); } else { playerPositionButtons[index].BackgroundImage = Properties.Resources.missIcon; enemyMove.Text = playerPositionButtons[index].Text; playerPositionButtons[index].Enabled = false; playerPositionButtons[index].BackColor = Color.DarkBlue; playerPositionButtons.RemoveAt(index); EnemyPlayTimer.Stop(); } } if (round < 1 || enemyScore > 2 || playerScore > 2) { if (playerScore > enemyScore) { MessageBox.Show("You Win!!", "Winning"); RestartGame(); } else if (enemyScore > playerScore) { MessageBox.Show("Haha, I sunk your battle ship", "Lost"); RestartGame(); } else if (enemyScore == playerScore) { MessageBox.Show("No one wins this game", "Draw"); RestartGame(); } } }
private void EnemyPlayTimerEvent(object sender, EventArgs e) { if (playerPositions.Count > 0 && round > 0) { --round; txtRounds.Text = "Round: " + round; int index = rand.Next(playerPositions.Count); if ((string)playerPositions[index].Tag == "playerShip") { playerPositions[index].Enabled = false; txtMove.Text = playerPositions[index].Text; playerPositions[index].BackgroundImage = Properties.Resources.fireIcon; playerPositions[index].BackColor = Color.DarkBlue; playerPositions.RemoveAt(index); ++enemyScore; txtEnemy.Text = enemyScore.ToString(); EnemyPlayTimer.Stop(); } else { txtMove.Text = playerPositions[index].Text; playerPositions[index].BackgroundImage = Properties.Resources.missIcon; playerPositions[index].BackColor = Color.DarkBlue; playerPositions.RemoveAt(index); EnemyPlayTimer.Stop(); } } if (round < 1) { if (playerScore > enemyScore) { MessageBox.Show("You won!"); RestartGame(); } else if (enemyScore > playerScore) { MessageBox.Show("You lost!"); RestartGame(); } else { MessageBox.Show("It's a draw!"); RestartGame(); } } }
/// <summary> /// Handles the click event for the attack button /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AttackButtonEvent(object sender, EventArgs e) { // if user selects a postion from the combo list box if (cmBoxEnemyLocation.Text != "") { // get that selected postion string attackPostion = cmBoxEnemyLocation.Text.ToLower(); int index = enemyPositionButtons.FindIndex(a => a.Name == attackPostion); // the player hasnt attacked this postion before and still have more round to play if (enemyPositionButtons[index].Enabled && round > 0) { round -= 1; lblRounds.Text = "Round: " + round; // if the player hit the enemy ship if ((string)enemyPositionButtons[index].Tag == "enemyShip") { enemyPositionButtons[index].Enabled = false; enemyPositionButtons[index].BackgroundImage = Properties.Resources.hit_ship_icon; enemyPositionButtons[index].BackColor = Color.Red; playerScore += 1; lblPlayerWins.Text = playerScore.ToString(); EnemyPlayTimer.Start(); } // if the player missed the enemy ship else { enemyPositionButtons[index].Enabled = false; enemyPositionButtons[index].BackgroundImage = Properties.Resources.missed_ship_icon; enemyPositionButtons[index].BackColor = Color.Blue; EnemyPlayTimer.Start(); } } } else { MessageBox.Show("Choose a location from the drop down first", "Information"); } }
/// <summary> /// Handles the enemy timer event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void EnemyPlayTimerEvent(object sender, EventArgs e) { // if the game is still playable if (playerPositionButtons.Count > 0 && round > 0) { round -= 1; lblRounds.Text = "Round: " + round; int index = rand.Next(playerPositionButtons.Count); // if the enemy (AI) hits the player ship if ((string)playerPositionButtons[index].Tag == "playerShip") { playerPositionButtons[index].BackgroundImage = Properties.Resources.hit_ship_icon; playerPositionButtons[index].BackColor = Color.Red; lblEnemyMove.Text = playerPositionButtons[index].Name.ToUpper(); playerPositionButtons[index].Enabled = false; playerPositionButtons.RemoveAt(index); enemyScore += 1; lblEnemyWins.Text = enemyScore.ToString(); EnemyPlayTimer.Stop(); } // if enemy misses the player's ship else { playerPositionButtons[index].BackgroundImage = Properties.Resources.missed_ship_icon; playerPositionButtons[index].BackColor = Color.Blue; lblEnemyMove.Text = playerPositionButtons[index].Name.ToUpper(); playerPositionButtons[index].Enabled = false; playerPositionButtons.RemoveAt(index); EnemyPlayTimer.Stop(); } } DetermineEndGameResult(); }