public void RunGame(object sender, EventArgs e) { HasWinner = false; IsUserTurn = startplayer.Checked; winnerlabel.Text = ""; Round = 0; TicTacToeDifficulty difficulty; if (easy.Checked) { difficulty = TicTacToeDifficulty.Easy; } else if (medium.Checked) { difficulty = TicTacToeDifficulty.Medium; } else { difficulty = TicTacToeDifficulty.Difficult; } AI = new TicTacToeAI(difficulty); Board = new TicTacToeCell[9]; for (int i = 0; i < 9; i++) { Board[i] = TicTacToeCell.None; } PrintBoard(Board); if (startai.Checked) { HandleAIInput(AI.ComputeNextMove(Board)); } }
public TicTacToeGameScreen() { tictactoeAI = new TicTacToeAI(); turnpiece = TurnPiece.oturn; winningformation = WinningFormation.nowin; gameMouse = new MouseState(); blankBackgroundRect = new Rectangle(0, 0, 1024, 720); mouseCount = 0; previousState = new MouseState(); middleState = new MouseState(); hitEnterToReplay = false; pieceSize.X = 128; pieceSize.Y = 128; // the peices rectangles set #region Pieces xRect = new Rectangle[9]; oRect = new Rectangle[100]; xRect[0] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[1] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[2] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[3] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[4] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[5] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[6] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[7] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); xRect[8] = new Rectangle(0, 0, (int)pieceSize.X, (int)pieceSize.Y); terminateLoop = false; // rectangle pieces oRect[0] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[1] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[2] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[3] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[4] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[5] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[6] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[7] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[8] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[9] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[10] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[11] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[12] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[13] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[14] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[15] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[16] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[17] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[18] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[19] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[20] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[21] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[22] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[23] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[24] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[25] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[26] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[27] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[28] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[29] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[30] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[31] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[32] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[33] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[34] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[35] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[36] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[37] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[38] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[39] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[40] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[41] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[42] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[43] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[44] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[45] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[46] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[47] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[48] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[49] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[50] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[51] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[52] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[53] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[54] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[55] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[56] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[57] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[58] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[59] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[60] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[61] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[62] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[63] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[64] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[65] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[66] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[60] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[61] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[62] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[63] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[64] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[65] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[66] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[67] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[68] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[69] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[70] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[71] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[72] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[73] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[74] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[75] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[76] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[77] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[78] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[79] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[80] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[81] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[82] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[83] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[84] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[85] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[86] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[87] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[88] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[89] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[90] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[91] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[92] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[93] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[94] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[95] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[96] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[97] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[98] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); oRect[99] = new Rectangle(130, 0, (int)pieceSize.X, (int)pieceSize.Y); #endregion gameBoardRect = new Rectangle(100, 50, 640, 640); mouseCollision = new Rectangle(0, 0, 24, 24); // intitalizing the texture array // gameboard location is set to empty gameboardlocation = GameBoardLocation.empty; // locations of each and every piece put into a vector2 array to explicit or implicit conversion to int for rectangular collision boxLocations = new Vector2[3, 3]; boxLocations[0, 0] = new Vector2(150, 100); boxLocations[0, 1] = new Vector2(370, 90); boxLocations[0, 2] = new Vector2(570, 95); boxLocations[1, 0] = new Vector2(140, 290); boxLocations[1, 1] = new Vector2(365, 300); boxLocations[1, 2] = new Vector2(575, 290); boxLocations[2, 0] = new Vector2(155, 520); boxLocations[2, 1] = new Vector2(365, 510); boxLocations[2, 2] = new Vector2(570, 520); // confirm or deny if the location is used or not based on boolean logic algorithim tictactoeAI.IsLocationUsed = new bool[3, 3]; // nested double for loop that initializes all in the multi dimensional array as false for update method // deep logical algorithims for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { tictactoeAI.IsLocationUsed[a, b] = false; } } // boolean multi dimensional array to see if x is using the spot tictactoeAI.LocationUsedByX = new bool[3, 3]; for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { tictactoeAI.LocationUsedByX[a, b] = false; } } // double nested for loop to set all values to false // boolean multi dimensional array to see if o is using the spot tictactoeAI.LocationUsedByO = new bool[3, 3]; for (int a = 0; a < 3; a++) { for (int b = 0; b < 3; b++) { tictactoeAI.LocationUsedByO[a, b] = false; } } /* comment only * Locations for each field in debug mode * textfile that maps locations * 1 2 3 * 4 5 6 * 7 8 9 * 1.) x: 150 y: 100 * 2.) x: 370 y: 90 * 3.) x: 570 y: 95 * 4.) x: 140 y: 290 * 5.) x: 365 y: 300 * 6.) x: 575 y: 290 * 7.) x: 155 y: 520 * 8.) x: 365 y: 510 * 9.) x: 570 y: 520 */ boxCollisionRect = new Rectangle[9]; boxCollisionRect[0] = new Rectangle((int)boxLocations[0, 0].X, (int)boxLocations[0, 0].Y, 130, 130); boxCollisionRect[1] = new Rectangle((int)boxLocations[0, 1].X, (int)boxLocations[0, 1].Y, 130, 130); boxCollisionRect[2] = new Rectangle((int)boxLocations[0, 2].X, (int)boxLocations[0, 2].Y, 130, 130); boxCollisionRect[3] = new Rectangle((int)boxLocations[1, 0].X, (int)boxLocations[1, 0].Y, 130, 130); boxCollisionRect[4] = new Rectangle((int)boxLocations[1, 1].X, (int)boxLocations[1, 1].Y, 130, 130); boxCollisionRect[5] = new Rectangle((int)boxLocations[1, 2].X, (int)boxLocations[1, 2].Y, 130, 130); boxCollisionRect[6] = new Rectangle((int)boxLocations[2, 0].X, (int)boxLocations[2, 0].Y, 130, 130); boxCollisionRect[7] = new Rectangle((int)boxLocations[2, 1].X, (int)boxLocations[2, 1].Y, 130, 130); boxCollisionRect[8] = new Rectangle((int)boxLocations[2, 2].X, (int)boxLocations[2, 2].Y, 130, 130); /* comment only * Locations for each field in debug mode * textfile that maps locations * 1 2 3 * 4 5 6 * 7 8 9 * 1.) x: 150 y: 100 * 2.) x: 370 y: 90 * 3.) x: 570 y: 95 * 4.) x: 140 y: 290 * 5.) x: 365 y: 300 * 6.) x: 575 y: 290 * 7.) x: 155 y: 520 * 8.) x: 365 y: 510 * 9.) x: 570 y: 520 */ }
protected void Click_Command(object sender, CommandEventArgs e) { if (ViewState["Board"] == null) { ViewState["Board"] = "---------"; } var board = ViewState["Board"].ToString().ToCharArray(); var index = int.Parse(e.CommandArgument.ToString()); if (board[index] != '-') { this.Result.Text = "Invalid move - try again !"; return; } board[index] = 'X'; ViewState["Board"] = new string(board); var logic = new GameLogic(); var resultX = logic.GetResult(board); if (resultX == GameResult.WonByX) { this.Result.Text = "Incredible ! You win ! "; this.RestartGame(); return; } else if (resultX == GameResult.Draw) { this.Result.Text = "Draw .. what a surprise .. "; this.RestartGame(); return; } var ai = new TicTacToeAI(); for (int i = 0; i < board.Length; i++) { if (board[i] == '-') { board[i] = 'O'; break; } } ViewState["Board"] = new string(board); var resultY = logic.GetResult(board); if (resultY == GameResult.WonByO) { this.Result.Text = "Damn ! You loose ! "; this.RestartGame(); return; } else if (resultY == GameResult.Draw) { this.Result.Text = "Draw .. what a surprise .. "; this.RestartGame(); return; } }