// Constructor public CombatSim(int attackerID, BattleStatus battle, Color colorAttack, Color colorDefense) { // Set for first call to Form3, it will be replaced during the combat loop string actionContent = "Press continue if you're sure you'd like to initiate combat!"; InvaderWon = false; DefenderWon = false; RetreatOrTie = false; // Launch Form3 window using (var form3 = new Form3(actionContent, battle.InvaderMessage, battle.DefenderMessage, colorAttack, colorDefense)) { bool fighting = true; // This bool later breaks user from the Form3 combat loop // Launch Form3, save the user's button click choice var result = form3.ShowDialog(); // Perform first round of calculations if user clicks Continue button if (result == System.Windows.Forms.DialogResult.OK) { // Continuosly respawn the combat menu with updated combat info while true while (fighting) { // Run a round of the combat algorithm battle.SimulateARound(); // Report back with a string of the battle results actionContent = battle.CombatMessage; // Display the combat results by relaunching Form3 using (var form3relaunch = new Form3(actionContent, battle.InvaderMessage, battle.DefenderMessage, colorAttack, colorDefense)) { // Launch and return user button selection again var resultRelaunch = form3relaunch.ShowDialog(); // If a victory condition has been tripped if (battle.battleFinished) { fighting = false; // Break from combat loop // Compose this class level object from the current state of the battle object composed_battle = battle; FleetStatus(); // Update the state of both fleets } // Break from combat loop if user selected Retreat button if (resultRelaunch == System.Windows.Forms.DialogResult.Cancel) { fighting = false; // Break from combat loop // Compose this class level object from the current state of the battle object composed_battle = battle; composed_battle.RetreatOrTie = true; // Toggle retreat to true FleetStatus(); // Update the state of both fleets } } } } } }
// User initiated a move private void buttonMove_Click(object sender, EventArgs e) { // Player 1 is making a move if (game.WhichPlayersTurn == 1) { PerformMove(); } // Player 2 is making a move if (game.WhichPlayersTurn == 2) { PerformMove(); } // Reset numeric up down values void ResetUpDowns() { numericMoveFrigates.Value = 0; numericMoveBattleships.Value = 0; numericMoveCapitalships.Value = 0; } // Verify the move can be performed bool CanMove() { bool canMove = false; bool allUpDownsZero = false; bool tooManyShips = false; // If all numeric up-downs are set to 0 and a move is tried if (numericMoveFrigates.Value == 0 && numericMoveBattleships.Value == 0 && numericMoveCapitalships.Value == 0) { tooManyShips = true; allUpDownsZero = true; labelWarning.Text = "No ships selected!"; labelWarning.Visible = true; ResetUpDowns(); } // If theres not enough frigates on current planet to perform move if (numericMoveFrigates.Value > game.planets[game.IDofCurrentPlanet].CombatFrigate) { tooManyShips = true; ResetUpDowns(); labelWarning.Text = "Not enough frigates!"; labelWarning.Visible = true; } // If theres not enough battleships on current planet to perform move if (numericMoveBattleships.Value > game.planets[game.IDofCurrentPlanet].BattleShip) { tooManyShips = true; ResetUpDowns(); labelWarning.Text = "Not enough battleships!"; labelWarning.Visible = true; } // If theres not enough capitalships on current planet to perform move if (numericMoveCapitalships.Value > game.planets[game.IDofCurrentPlanet].CapitalShip) { tooManyShips = true; ResetUpDowns(); labelWarning.Text = "Not enough capital ships!"; labelWarning.Visible = true; } // Check to see if planet is neutral, or if planet ID is same as current user ID, makes sure user selected at least 1 ship if (game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID == 0 && !allUpDownsZero && !tooManyShips || game.WhichPlayersTurn == game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID && !allUpDownsZero && !tooManyShips) { canMove = true; } // Otherwise start a battle else if (game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID != 0 && game.WhichPlayersTurn != game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID && !allUpDownsZero && !tooManyShips) { // Assign these shorter variable names to make the constructor easier to understand int attacker_ID = game.WhichPlayersTurn; // ID of the attacking player int attacker_planet_ID = game.IDofCurrentPlanet; // Planet ID of attacking player int attackFrig = (int)numericMoveFrigates.Value; // Attack units int attackBatt = (int)numericMoveBattleships.Value; int attackCapi = (int)numericMoveCapitalships.Value; int defenseSSM = game.planets[(int)comboBoxMoveTo.SelectedValue - 1].SSMSite; // Defense units int defenseFrig = game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CombatFrigate; int defenseBatt = game.planets[(int)comboBoxMoveTo.SelectedValue - 1].BattleShip; int defenseCapi = game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CapitalShip; // Team colors Color offense, defense; // If player 1 is attacker if (game.WhichPlayersTurn == 1) { offense = game.player1Color; defense = game.player2Color; } else // Player 2 is attacker { offense = game.player2Color; defense = game.player1Color; } // Make a battle object that will maintain the state of both fleets after a skirmish BattleStatus battle = new BattleStatus(attackFrig, attackBatt, attackCapi, defenseSSM, defenseFrig, defenseBatt, defenseCapi); // Create a CombatSim object and simulate the battle, pass in the status of both fleets with object 'battle' CombatSim sim = new CombatSim(attacker_ID, battle, offense, defense); // Now grab the results of the skirmish out of the CombatSim object // Update the respective planets according to the state found in 'sim' if (sim.DefenderWon) { // --- Update fleet sizes // Defending planet fleet game.planets[(int)comboBoxMoveTo.SelectedValue - 1].SSMSite = sim.DefenseSSM; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CombatFrigate = sim.DefenseFrig; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].BattleShip = sim.DefenseBatt; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CapitalShip = sim.DefenseCapi; // Invading planet fleet game.planets[attacker_planet_ID].CombatFrigate = 0; game.planets[attacker_planet_ID].BattleShip = 0; game.planets[attacker_planet_ID].CapitalShip = 0; // Reset up-downs ResetUpDowns(); // End turn and switch to next player EndSwitchTurns(); // Update current window after move operation UpdatePlanetStatsBox(game.IDofCurrentPlanet); } if (sim.InvaderWon) { // Set the defending planet fleet size equal to the invader's fleet to move ships over game.planets[(int)comboBoxMoveTo.SelectedValue - 1].SSMSite = 0; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CombatFrigate = sim.AttackFrig; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].BattleShip = sim.AttackBatt; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CapitalShip = sim.AttackCapi; // Update the invaders planet to reflect the ships that have left and moved on to the new planet game.planets[attacker_planet_ID].CombatFrigate -= (int)numericMoveFrigates.Value; game.planets[attacker_planet_ID].BattleShip -= (int)numericMoveBattleships.Value; game.planets[attacker_planet_ID].CapitalShip -= (int)numericMoveCapitalships.Value; // Update flag color and owner ID if (game.WhichPlayersTurn == 1) { flagBoxes[(int)comboBoxMoveTo.SelectedValue - 1].BackColor = game.player1Color; // Change flag color game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID = 1; // Change planet owner ID game.player1Mines += game.planets[(int)comboBoxMoveTo.SelectedValue - 1].MinesActive; // Update how many mines are owned game.player2Mines -= game.planets[(int)comboBoxMoveTo.SelectedValue - 1].MinesActive; // Update how many mines are owned } if (game.WhichPlayersTurn == 2) { flagBoxes[(int)comboBoxMoveTo.SelectedValue - 1].BackColor = game.player2Color; // Change flag color game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID = 2; // Change planet owner ID game.player2Mines += game.planets[(int)comboBoxMoveTo.SelectedValue - 1].MinesActive; // Update how many mines are owned game.player1Mines -= game.planets[(int)comboBoxMoveTo.SelectedValue - 1].MinesActive; // Update how many mines are owned } // Reset up-downs ResetUpDowns(); // End turn and switch to next player EndSwitchTurns(); // Update current window after move operation UpdatePlanetStatsBox(game.IDofCurrentPlanet); } if (sim.RetreatOrTie) { // --- Update fleet sizes // Defending planet fleet game.planets[(int)comboBoxMoveTo.SelectedValue - 1].SSMSite = sim.DefenseSSM; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CombatFrigate = sim.DefenseFrig; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].BattleShip = sim.DefenseBatt; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CapitalShip = sim.DefenseCapi; // Invading planet fleet game.planets[attacker_planet_ID].CombatFrigate = sim.AttackFrig; game.planets[attacker_planet_ID].CombatFrigate = sim.AttackBatt; game.planets[attacker_planet_ID].CombatFrigate = sim.AttackCapi; // Reset up-downs ResetUpDowns(); // End turn and switch to next player EndSwitchTurns(); // Update current window after move operation UpdatePlanetStatsBox(game.IDofCurrentPlanet); } // Create a victory condition based on if either player owns 0 of the 8 planets int player1control = 0; int player2control = 0; // Loop through to check all planets for (int x = 0; x < game.planets.Length; x++) { if (game.planets[x].OwnedByUserID == 1) { player1control++; } else if (game.planets[x].OwnedByUserID == 2) { player2control++; } } // Now check to see if either player has been defeated if (player1control == 0) { MessageBox.Show(game.player2Name + " is victorious!", "You should close the game now!"); } else if (player2control == 0) { MessageBox.Show(game.player1Name + " is victorious!", "You should close the game now!"); } } return(canMove); } // Perform the move void PerformMove() { // Check if there are enough ships to perform requested move if (CanMove()) { // Update flag color if (game.WhichPlayersTurn == 1) { flagBoxes[(int)comboBoxMoveTo.SelectedValue - 1].BackColor = game.player1Color; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID = 1; } if (game.WhichPlayersTurn == 2) { flagBoxes[(int)comboBoxMoveTo.SelectedValue - 1].BackColor = game.player2Color; game.planets[(int)comboBoxMoveTo.SelectedValue - 1].OwnedByUserID = 2; } // Use the currently selected value of planet in the combobox to access correct planet index // Add frigates to selected planet game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CombatFrigate += (int)numericMoveFrigates.Value; game.planets[game.IDofCurrentPlanet].CombatFrigate -= (int)numericMoveFrigates.Value; // Add battleships to selected planet game.planets[(int)comboBoxMoveTo.SelectedValue - 1].BattleShip += (int)numericMoveBattleships.Value; game.planets[game.IDofCurrentPlanet].BattleShip -= (int)numericMoveBattleships.Value; // Add capital ships to selected planet game.planets[(int)comboBoxMoveTo.SelectedValue - 1].CapitalShip += (int)numericMoveCapitalships.Value; game.planets[game.IDofCurrentPlanet].CapitalShip -= (int)numericMoveCapitalships.Value; // Reset up-downs ResetUpDowns(); // End turn and switch to next player EndSwitchTurns(); // Update current window after move operation UpdatePlanetStatsBox(game.IDofCurrentPlanet); } } }