/// <summary> /// IsWinningMove helps the AI determine what the next winning move is based on the Row and Column /// </summary> /// <returns></returns> public bool IsWinningMove() { ///Checks for the first row if the there has been a winning move if (!saBoard[0, 0].Equals(" ") && saBoard[0, 0].Equals(saBoard[0, 1]) && saBoard[0, 1].Equals(saBoard[0, 2])) { ///sets the winning move to Row1 eWinningMove = WinningMove.Row1; return(true); } ///Checks for the second row if the there has been a winning move if (!saBoard[1, 0].Equals(" ") && saBoard[1, 0].Equals(saBoard[1, 1]) && saBoard[1, 1].Equals(saBoard[1, 2])) { ///sets the winning move to Row2 eWinningMove = WinningMove.Row2; return(true); } ///Checks for the third row if the there has been a winning move if (!saBoard[2, 0].Equals(" ") && saBoard[2, 0].Equals(saBoard[2, 1]) && saBoard[2, 1].Equals(saBoard[2, 2])) { ///sets the winning move to Row2 eWinningMove = WinningMove.Row3; return(true); } ///Checks for the first col if the there has been a winning move if (!saBoard[0, 0].Equals(" ") && saBoard[0, 0].Equals(saBoard[1, 0]) && saBoard[1, 0].Equals(saBoard[2, 0])) { ///sets the winning move to Col1 eWinningMove = WinningMove.Col1; return(true); } ///Checks for the second col if the there has been a winning move if (!saBoard[0, 1].Equals(" ") && saBoard[0, 1].Equals(saBoard[1, 1]) && saBoard[1, 1].Equals(saBoard[2, 1])) { ///sets the winning move to Col2 eWinningMove = WinningMove.Col2; return(true); } ///Checks for the third col if the there has been a winning move if (!saBoard[0, 2].Equals(" ") && saBoard[0, 2].Equals(saBoard[1, 2]) && saBoard[1, 2].Equals(saBoard[2, 2])) { ///sets the winning move to Col3 eWinningMove = WinningMove.Col3; return(true); } ///Checks for the first diagonal if the there has been a winning move if (!saBoard[0, 0].Equals(" ") && saBoard[0, 0].Equals(saBoard[1, 1]) && saBoard[1, 1].Equals(saBoard[2, 2])) { ///sets the winning move to Diag1 eWinningMove = WinningMove.Diag1; return(true); } ///Checks for the second diagonal if the there has been a winning move if (!saBoard[2, 0].Equals(" ") && saBoard[2, 0].Equals(saBoard[1, 1]) && saBoard[1, 1].Equals(saBoard[0, 2])) { ///sets the winning move to Diag2 eWinningMove = WinningMove.Diag2; return(true); } return(false); }
private void DisplayWinningLine(WinningMove winningMove) { switch (winningMove) { case WinningMove.Row1: btnPos0.BackColor = Color.Yellow; btnPos1.BackColor = Color.Yellow; btnPos2.BackColor = Color.Yellow; break; case WinningMove.Row2: btnPos3.BackColor = Color.Yellow; btnPos4.BackColor = Color.Yellow; btnPos5.BackColor = Color.Yellow; break; case WinningMove.Row3: btnPos6.BackColor = Color.Yellow; btnPos7.BackColor = Color.Yellow; btnPos8.BackColor = Color.Yellow; break; case WinningMove.Col1: btnPos0.BackColor = Color.Yellow; btnPos3.BackColor = Color.Yellow; btnPos6.BackColor = Color.Yellow; break; case WinningMove.Col2: btnPos1.BackColor = Color.Yellow; btnPos4.BackColor = Color.Yellow; btnPos7.BackColor = Color.Yellow; break; case WinningMove.Col3: btnPos2.BackColor = Color.Yellow; btnPos5.BackColor = Color.Yellow; btnPos8.BackColor = Color.Yellow; break; case WinningMove.Diag1: btnPos0.BackColor = Color.Yellow; btnPos4.BackColor = Color.Yellow; btnPos8.BackColor = Color.Yellow; break; case WinningMove.Diag2: btnPos2.BackColor = Color.Yellow; btnPos4.BackColor = Color.Yellow; btnPos6.BackColor = Color.Yellow; break; } }
/// <summary> /// Checks to see if there was a Vertical winning move then sets then stores which Vertical /// the winning move was. /// </summary> /// <returns></returns> public bool isVertical() { //Check Player 1 Col 1 if (board[0, 0].Equals("X") && board[1, 0].Equals("X") && board[2, 0].Equals("X")) { eWinningMove = WinningMove.Col1; winningPlayer = Player.player1; return(true); } //Check Player 1 Col 2 if (board[0, 1].Equals("X") && board[1, 1].Equals("X") && board[2, 1].Equals("X")) { eWinningMove = WinningMove.Col2; winningPlayer = Player.player1; return(true); } //Check Player 1 Col 3 if (board[0, 2].Equals("X") && board[1, 2].Equals("X") && board[2, 2].Equals("X")) { eWinningMove = WinningMove.Col3; winningPlayer = Player.player1; return(true); } //Check Player 2 Col 1 if (board[0, 0].Equals("O") && board[1, 0].Equals("O") && board[2, 0].Equals("O")) { eWinningMove = WinningMove.Col1; winningPlayer = Player.player2; return(true); } //Check Player 2 Col 2 if (board[0, 1].Equals("O") && board[1, 1].Equals("O") && board[2, 1].Equals("O")) { eWinningMove = WinningMove.Col2; winningPlayer = Player.player2; return(true); } //Check Player 2 Col 3 if (board[0, 2].Equals("O") && board[1, 2].Equals("O") && board[2, 2].Equals("O")) { eWinningMove = WinningMove.Col3; winningPlayer = Player.player2; return(true); } return(false); }
/// <summary> /// Resets the game board back to being full of empty strings /// </summary> public void ResetBoard() { // Row for (int i = 0; i < saBoard.GetLength(0); i++) { // Column for (int j = 0; j < saBoard.GetLength(1); j++) { saBoard[i, j] = ""; } } eWinningMove = WinningMove.None; }
/// <summary> /// Checks to see if there was a horizontal winning move then sets then stores which horizontal /// the winning move was. /// </summary> /// <returns></returns> public bool isHorizontal() { //Check Player 1 Row 1 if (board[0, 0].Equals("X") && board[0, 1].Equals("X") && board[0, 2].Equals("X")) { eWinningMove = WinningMove.Row1; winningPlayer = Player.player1; return(true); } //Check Player 1 Row 2 if (board[1, 0].Equals("X") && board[1, 1].Equals("X") && board[1, 2].Equals("X")) { eWinningMove = WinningMove.Row2; winningPlayer = Player.player1; return(true); } //Check Player 1 Row 3 if (board[2, 0].Equals("X") && board[2, 1].Equals("X") && board[2, 2].Equals("X")) { eWinningMove = WinningMove.Row3; winningPlayer = Player.player1; return(true); } //Check Player 2 Row 1 if (board[0, 0].Equals("O") && board[0, 1].Equals("O") && board[0, 2].Equals("O")) { eWinningMove = WinningMove.Row1; winningPlayer = Player.player2; return(true); } //Check Player 2 Row 2 if (board[1, 0].Equals("O") && board[1, 1].Equals("O") && board[1, 2].Equals("O")) { eWinningMove = WinningMove.Row2; winningPlayer = Player.player2; return(true); } //Check Player 2 Row 3 if (board[2, 0].Equals("O") && board[2, 1].Equals("O") && board[2, 2].Equals("O")) { eWinningMove = WinningMove.Row3; winningPlayer = Player.player2; return(true); } return(false); }
/// <summary> /// Reset the board to blank tiles /// </summary> public void reset() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { board[i, j] = ""; } } bIsGameStarted = true; turnTracker = Player.player1; winFlag = false; eWinningMove = WinningMove.None; }
/// <summary> /// Checks each column of the board to see if there is a vertical win /// </summary> /// <returns>Returns true if there is a vertical win, false if not</returns> private bool IsVertical() { // For each column for (int i = 0; i < 3; i++) { // If all marks in column are the same as the current mark, set eWinningMove to corresponding column and return true if (saBoard[0, i] == sCurrentMark && saBoard[1, i] == sCurrentMark && saBoard[2, i] == sCurrentMark) { // Uses integer value of WinningMove to determine which row is set as eWinningMove (values 3-5) eWinningMove = (WinningMove)(i + 3); return(true); } } return(false); }
/// <summary> /// Checks each row of the board to see if there is a horizontal win /// </summary> /// <returns>Returns true if there is a horizontal win, false if not</returns> private bool IsHorizontal() { // for each row for (int i = 0; i < 3; i++) { // If all marks in row are the same as the current mark, set eWinningMove to corresponding row and return true if (saBoard[i, 0] == sCurrentMark && saBoard[i, 1] == sCurrentMark && saBoard[i, 2] == sCurrentMark) { // Uses integer value of WinningMove to determine which row is set as eWinningMove (values 0-2) eWinningMove = (WinningMove)i; return(true); } } return(false); }
/// <summary> /// Checks each diagonal of the board to see if there is a diagonal win /// </summary> /// <returns>Returns true if there is a diagonal win, false if not</returns> private bool IsDiag() { // If all marks on Diag1 (0-0,1-1,2-2) are the same as the current mark, set eWinningMove to Diag1 if (saBoard[0, 0] == sCurrentMark && saBoard[1, 1] == sCurrentMark && saBoard[2, 2] == sCurrentMark) { eWinningMove = WinningMove.Diag1; return(true); } // If all marks on Diag2 (0-2,1-1,2-0) are the same as the current mark, set eWinningMove to Diag2 else if (saBoard[0, 2] == sCurrentMark && saBoard[1, 1] == sCurrentMark && saBoard[2, 0] == sCurrentMark) { eWinningMove = WinningMove.Diag2; return(true); } return(false); }
/// <summary> /// Determines if there is a diagonal win on the board and which diagonal win it is /// </summary> private bool isDiagWin() { if (saBoard[0, 0] == saBoard[1, 1] && saBoard[1, 1] == saBoard[2, 2] && saBoard[0, 0] != " ") { eWinningMove = WinningMove.Diag1; return(true); } else if (saBoard[0, 2] == saBoard[1, 1] && saBoard[1, 1] == saBoard[2, 0] && saBoard[0, 2] != " ") { eWinningMove = WinningMove.Diag2; return(true); } else { return(false); } }
/// <summary> /// Determines if there is a horizontal win on the board and which horizontal win it is /// </summary> private bool isHorzWin() { if (saBoard[0, 0] == saBoard[1, 0] && saBoard[1, 0] == saBoard[2, 0] && saBoard[0, 0] != " ") { eWinningMove = WinningMove.Row1; return(true); } else if (saBoard[0, 1] == saBoard[1, 1] && saBoard[1, 1] == saBoard[2, 1] && saBoard[0, 1] != " ") { eWinningMove = WinningMove.Row2; return(true); } else if (saBoard[0, 2] == saBoard[1, 2] && saBoard[1, 2] == saBoard[2, 2] && saBoard[0, 2] != " ") { eWinningMove = WinningMove.Row3; return(true); } else { return(false); } }
/// <summary> /// Determines if there is a vertical win on the board and which vertical win it is /// </summary> private bool isVertWin() { if (saBoard[0, 0] == saBoard[0, 1] && saBoard[0, 1] == saBoard[0, 2] && saBoard[0, 0] != " ") { eWinningMove = WinningMove.Col1; return(true); } else if (saBoard[1, 0] == saBoard[1, 1] && saBoard[1, 1] == saBoard[1, 2] && saBoard[1, 0] != " ") { eWinningMove = WinningMove.Col2; return(true); } else if (saBoard[2, 0] == saBoard[2, 1] && saBoard[2, 1] == saBoard[2, 2] && saBoard[2, 0] != " ") { eWinningMove = WinningMove.Col3; return(true); } else { return(false); } }
/// <summary> /// Checks to see if there was a diagonal winning move then sets then stores which diagonal /// the winning move was. /// </summary> /// <returns></returns> public bool isDiagonal() { //Check Player 1 Diagonal 1 if (board[0, 0].Equals("X") && board[1, 1].Equals("X") && board[2, 2].Equals("X")) { eWinningMove = WinningMove.Diag1; winningPlayer = Player.player1; return(true); } //Check Player 1 Diagonal 2 if (board[0, 2].Equals("X") && board[1, 1].Equals("X") && board[2, 0].Equals("X")) { eWinningMove = WinningMove.Diag1; winningPlayer = Player.player1; return(true); } //Check Player 2 Diagonal 1 if (board[0, 0].Equals("O") && board[1, 1].Equals("O") && board[2, 2].Equals("O")) { eWinningMove = WinningMove.Diag2; winningPlayer = Player.player2; return(true); } //Check Player 2 Diagonal 2 if (board[0, 2].Equals("O") && board[1, 1].Equals("O") && board[2, 0].Equals("O")) { eWinningMove = WinningMove.Diag2; winningPlayer = Player.player2; return(true); } return(false); }
/// <summary> /// is that the winning move /// </summary> /// <returns></returns> public bool IsWinningMove() { eWinningMove = WinningMove.Row2; return(false); }
private MoveWinsGameCheck CheckMoveWinsGame() { WinningMove winningMovePos = WinningMove.Row1; bool horizontalCheck = false; if (GameBoard[0] == GameBoard[1] && GameBoard[1] == GameBoard[2]) { winningMovePos = WinningMove.Row1; horizontalCheck = true; WinningMoves = new List <string>() { "0,0", "0,1", "0,2" }; } else if (GameBoard[3] == GameBoard[4] && GameBoard[4] == GameBoard[5]) { winningMovePos = WinningMove.Row2; horizontalCheck = true; WinningMoves = new List <string>() { "1,0", "1,1", "1,2" }; } else if (GameBoard[6] == GameBoard[7] && GameBoard[7] == GameBoard[8]) { winningMovePos = WinningMove.Row3; horizontalCheck = true; WinningMoves = new List <string>() { "2,0", "2,1", "2,2" }; } bool verticalCheck = false; if (GameBoard[0] == GameBoard[3] && GameBoard[3] == GameBoard[6]) { winningMovePos = WinningMove.Col1; verticalCheck = true; WinningMoves = new List <string>() { "0,0", "1,0", "2,0" }; } else if (GameBoard[1] == GameBoard[4] && GameBoard[4] == GameBoard[7]) { winningMovePos = WinningMove.Col2; verticalCheck = true; WinningMoves = new List <string>() { "0,1", "1,1", "2,1" }; } else if (GameBoard[2] == GameBoard[5] && GameBoard[5] == GameBoard[8]) { winningMovePos = WinningMove.Col3; verticalCheck = true; WinningMoves = new List <string>() { "0,2", "1,2", "2,2" }; } bool diagonalCheck = false; if (GameBoard[0] == GameBoard[4] && GameBoard[4] == GameBoard[8]) { winningMovePos = WinningMove.Diag1; diagonalCheck = true; WinningMoves = new List <string>() { "0,0", "1,1", "2,2" }; } else if (GameBoard[2] == GameBoard[4] && GameBoard[4] == GameBoard[6]) { winningMovePos = WinningMove.Diag2; diagonalCheck = true; WinningMoves = new List <string>() { "0,2", "1,1", "2,0" }; } string winningGameMessage = string.Empty; bool moveWinsGame = false; if (horizontalCheck || verticalCheck || diagonalCheck) { moveWinsGame = true; WinningPlayer = PlayerSwitch + 1; winningGameMessage = $"Congratulations!! Player { WinningPlayer } wins!"; GameStatusMessage = CurrentPlayerIsUser ? "Congratulations you win" : "Unlucky the computer wins"; } var moveWinsGameCheck = new MoveWinsGameCheck() { MoveWinsGame = moveWinsGame, WinningMovePos = winningMovePos, WinningGameMessage = winningGameMessage, GameComplete = moveWinsGame, }; return(moveWinsGameCheck); }