public void MainMethod() { board = new TicTacToeBoard(); Console.WriteLine("Hello! To play tic tac toe, when it is your turn, enter the index of where you want to play (0-8)"); Console.Read(); while (!board.IsFull() && !board.IsWinner(SquareType.X) && !board.IsWinner(SquareType.O)) { DisplayBoardGrid(); Console.WriteLine("It is {0} turn.", currentTurn); int move = promptForInt("Enter your index: "); if (board.IsLegalMove(move)) board.MakeMove(move, currentTurn); else { Console.WriteLine("Illegal Move! Press enter!"); Console.Read(); continue; } // Swap the turn currentTurn = currentTurn == SquareType.X ? SquareType.O : SquareType.X; } DisplayBoardGrid(); if (board.IsFull()) Console.WriteLine("Tie! Board is full!"); else if (board.IsWinner(SquareType.X)) AccounceWinner(SquareType.X); else if (board.IsWinner(SquareType.O)) AccounceWinner(SquareType.O); else Console.WriteLine("Something has gone wrong!"); }
/// <summary> /// Plays the game. /// </summary> public void Play() { int move; bool over = false; Move currentPlayer = Move.X; var board = new TicTacToeBoard(); // Print a greeting. Console.WriteLine("xox Play Tic-Tac-Toe! xox" + Newline); // Keep going until the game is deemed "over" while (!over) { // Print the board. Console.WriteLine(board.ToString()); Console.WriteLine("Player 1 => X, Player 2 => O"); // Ask the player to enter a square number Console.Write("Enter Square [player " + PrintMove(currentPlayer) + "]: "); var playersMove = Console.ReadLine(); Console.Write(Newline); // Process player's move if (int.TryParse(playersMove, out move)) { try { // Attempt to make the move, then change players. board.MakeMove(move, currentPlayer); currentPlayer = FlipCurrentPlayer(currentPlayer); } catch (Exception e) { Console.WriteLine(e.Message + Newline); } } else { // Just in case someone doesn't give us an int. Console.WriteLine("Unable to parse your move, please try again." + Newline); } // A little magic here. If the board has a winner set then we're done. if (board.Winner != null) { this.PrintEndGame(board); over = true; } } }
public void TakeATurn(TicTacToeBoard board) { board.DisplayBoard(); bool validMove = false; while (!validMove) { Console.WriteLine("Please make a move (1-9):"); int move; if (Int32.TryParse(Console.ReadLine(), out move)) { if (board.GetMove(move) == 0) { board.MakeMove(move, 2); validMove = true; } else { Console.WriteLine("Invalid move. Please try again."); } } else { Console.WriteLine("Invalid syntax. Please try again."); } } }
public void PlayAGame() { TicTacToeBot ai = new TicTacToeBot(); TicTacToeBoard board = new TicTacToeBoard(); Random rng = new Random(); bool yourTurn; Console.WriteLine("Randomizing who starts..."); if (rng.Next(2) == 0) { Console.WriteLine("You start!"); yourTurn = true; } else { Console.WriteLine("AI starts."); yourTurn = false; } int winner = 0; bool draw = false; while (winner == 0 && !draw) { if (yourTurn) { TakeATurn(board); } else { board.MakeMove(ai.GetBestMove(board), 1); } yourTurn = !yourTurn; winner = board.CheckForWin(); draw = board.CheckForDraw(); } board.DisplayBoard(); if (winner == 1) { Console.WriteLine("AI wins."); } else if (winner == 2) { Console.WriteLine("You win!"); } else { Console.WriteLine("Draw game."); } }
public double AlphaBeta(TicTacToeBoard oldBoard, double alpha, double beta, int player) { if (oldBoard.CheckForWin() != 0 || oldBoard.CheckForDraw()) { return Score(oldBoard); } if (player == 1) { double val = -10000.0; for (int i = 1; i <= 9; i++) { if (oldBoard.GetMove(i) == 0) { TicTacToeBoard newBoard = new TicTacToeBoard(oldBoard); newBoard.MakeMove(i, player); val = Math.Max(val, gamma * AlphaBeta(newBoard, alpha, beta, 2)); alpha = Math.Max(alpha, val); if (beta <= alpha) break; } } return alpha; } else { double val = 10000.0; for (int i = 1; i <= 9; i++) { if (oldBoard.GetMove(i) == 0) { TicTacToeBoard newBoard = new TicTacToeBoard(oldBoard); newBoard.MakeMove(i, player); val = Math.Min(val, gamma * AlphaBeta(newBoard, alpha, beta, 1)); beta = Math.Min(val, beta); if (beta <= alpha) break; } } return beta; } }
public int GetBestMove(TicTacToeBoard board) { double max = -100000.0; int bestMoveSoFar = 0; for (int i = 1; i <= 9; i++) { if (board.GetMove(i) == 0) { TicTacToeBoard testBoard = new TicTacToeBoard(board); testBoard.MakeMove(i, 1); double moveScore = AlphaBeta(testBoard, -10000.0, 10000.0, 2); if (moveScore > max) { max = moveScore; bestMoveSoFar = i; } } } return bestMoveSoFar; }