void ResetAllValues() { Classes.Soldier.countPlayer1 = 0; Classes.Soldier.countPlayer2 = 0; finiteStateMachine = "initialization"; whoIsBeingDeployed = "none"; temporary = null; canContinue = true; win = false; for (int i = 0; i < player1Soldiers.Count; i++) { player1Soldiers = null; } for (int i = 0; i < computerSoldiers.Count; i++) { computerSoldiers = null; } for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { allowedMoves[i, j] = false; arrayOfSoldiers[i, j] = null; pictureArray[i, j] = null; } } }
private void pictureBox_Click(object sender, EventArgs e) { LogOfMoves.SelectionStart = LogOfMoves.Text.Length; LogOfMoves.ScrollToCaret(); PictureBox picture = (PictureBox)sender; Coordinates coordinates = new Coordinates(); coordinates = FindPicture(picture); int x = coordinates.x; int y = coordinates.y; // saving coordinates of the picture to two integers for ease of use if (finiteStateMachine == "placement") { if (y < 3) { PlaceUnit(picture, FindPicture(picture)); } else { MessageBox.Show("You can only place units within bottom 3 rows"); } } else if (finiteStateMachine == "playersTurn" && arrayOfSoldiers[x, y] != null) // can perform an action if and only if this place is occupied by players unit { if (arrayOfSoldiers[x, y].Player1Owned) { ActionPanel.Visible = true; ActionPanel.BackgroundImage = Properties.Resources.panel; temporary = arrayOfSoldiers[x, y]; } } else if (finiteStateMachine == "moving") { LogOfMoves.Text += "You are now moving. Boring.\n"; if (arrayOfSoldiers[x, y] == null) { ChangePosition(x, y); } else if (!arrayOfSoldiers[x, y].Player1Owned) { Melee(x, y); } } else if (finiteStateMachine == "shooting") { LogOfMoves.Text += "You are now shooting. That's exciting!\n"; Shooting(x, y); } } // game flow determined by states of FSM
} // game flow determined by states of FSM //----------------------Deployment methods below-----------------------------\\ private void ComputerDeployment() // randomly allocates computer's units { // method which deploys computers unit Random rnd = new Random(); while (computerGold > 150) { // randomly chooses coordinates int v = rnd.Next(0, width); int g = rnd.Next(height - 3, height); bool deployed = false; if (arrayOfSoldiers[v, g] == null && computerGold >= 400 && g == height - 1) { arrayOfSoldiers[v, g] = new Classes.Soldier(v, g, 2, 50, 100, 80, "Sniper", 50, 50, false); pictureArray[v, g].Image = Properties.Resources.S2; deployed = true; computerGold -= 400; } else if (arrayOfSoldiers[v, g] == null && computerGold >= 200 && g == height) { arrayOfSoldiers[v, g] = new Classes.Soldier(v, g, 3, 50, 33, 70, "Infantry", 200, 200, false); pictureArray[v, g].Image = Properties.Resources.I2; deployed = true; computerGold -= 200; } else if (arrayOfSoldiers[v, g] == null && computerGold >= 150) { arrayOfSoldiers[v, g] = new Classes.Soldier(v, g, 4, 60, 20, 60, "Mercenarie", 100, 100, false); pictureArray[v, g].Image = Properties.Resources.M2; deployed = true; computerGold -= 150; } if (deployed) { Coordinates coordinates = new Coordinates(); coordinates.x = v; coordinates.y = g; computerSoldiers.Add(coordinates); SetHealthBar(v, g); } } }
//-----------------------Deployment methods above---------------------------\\ //--------------------------MY AI BELOW--------------------------------------\\ private void HierarchyOfComputerMoves() // prioritizes Snipers - they deal most damage and have highest accuracy. { bool computerMoved = false; while (!computerMoved) { for (int i = 0; i < computerSoldiers.Count; i++) { int x = computerSoldiers.ElementAt(i).x; int y = computerSoldiers.ElementAt(i).y; string who = arrayOfSoldiers[x, y].Type; if (who == "Sniper") { LogOfMoves.Text += "\nEnemy Sniper shoots..."; ComputerShoots(x, y); computerMoved = true; break; } else if (who == "Mercenarie") { temporary = arrayOfSoldiers[x, y]; LogOfMoves.Text += "\nEnemy Mercenarie is checking if he can reach you..."; finiteStateMachine = "computerMelee"; MovesAllowed(); CleanBoard(); computerMoved = true; break; } else if (who == "Infantry") { LogOfMoves.Text += "\nEnemy Infantry shoots..."; ComputerShoots(x, y); computerMoved = true; break; } } break; } // finnaly check if user has any units left# DidILose(); } // prioritizes Snipers - they deal most damage and have highest accuracy.