private Spot Minimax(Game G, int level, int maxLevel, char turn) { Spot BestSpot = new Spot(); //if current AI player if (level % 2 == 0) { BestSpot.score = -1000000; // Negative infinity. Trying to maximize this score. } //if AI opponent else { BestSpot.score = 1000000; // Positive infinity. Trying to minimize this score. } //if there is a winner or we are as deep as we are allowed to go if (G.checkWinner().Item1 != ' ' || G.BoardIsFull() || level >= maxLevel) { BestSpot.score = scoreGame(G, level + 1, turn); //MessageBox.Show(string.Format("Score is {0}, level is {1}", BestSpot.score, level)); } else { //note that consts in C# are static, meaning that to access //them you should use the class name for (int i = 0; i < Game.ROWS; i++) { for (int j = 0; j < Game.COLS; j++) { if (G.CellIsPlayable(i, j)) { Game TmpGame = new Game(G); // Create virtual games and pick the best one. TmpGame.MarkGameBoard(i, j); Spot TmpSpot = new Spot(); TmpSpot.row = i; TmpSpot.col = j; //if (TmpGame.checkWinner().Item1 != ' ') //{ TmpSpot.score = Minimax(TmpGame, level + 1, maxLevel, turn).score; //MessageBox.Show(string.Format("Score is {0}, level is {1}", TmpSpot.score, level)); //if current AI player if (level % 2 == 0) { if (TmpSpot.score > BestSpot.score) { BestSpot = TmpSpot; } } //if AI opponent else { if (TmpSpot.score < BestSpot.score) { BestSpot = TmpSpot; } } //} } } } } //MessageBox.Show(string.Format("Score is {0}, level is {1}", BestSpot.score, level)); return(BestSpot); }
private void PlayGrid_MouseLeftDown(object sender, MouseButtonEventArgs e) { var point = Mouse.GetPosition(PlayGrid); int row = 0; int col = 0; double accumulatedHeight = 0.0; double accumulatedWidth = 0.0; // calc row mouse was over foreach (var rowDefinition in PlayGrid.RowDefinitions) { accumulatedHeight += rowDefinition.ActualHeight; if (accumulatedHeight >= point.Y) { break; } row++; } // calc col mouse was over foreach (var columnDefinition in PlayGrid.ColumnDefinitions) { accumulatedWidth += columnDefinition.ActualWidth; if (accumulatedWidth >= point.X) { break; } col++; } int rowIndexEmpty = -1; for (int i = Game.ROWS - 1; i >= 0; i--) { if (TTT_Game.CellIsEmpty(Game.ROWS - i - 1, col)) { rowIndexEmpty = i; break; } } if (rowIndexEmpty != -1) { if (TTT_Game.turn == 'X') { DrawX(rowIndexEmpty, col); TTT_Game.MarkGameBoard(Game.ROWS - rowIndexEmpty - 1, col); } else if (TTT_Game.turn == 'O') { DrawO(rowIndexEmpty, col); TTT_Game.MarkGameBoard(Game.ROWS - rowIndexEmpty - 1, col); } } else { MessageBox.Show(string.Format("CANNOT PUT A TOKEN IN THAT COLUMN! IT'S NOT EMPTY!")); return; } Tuple <char, List <Tuple <int, int> > > winResult = TTT_Game.checkWinner(); char winner = winResult.Item1; if (txtPlayer1.Text == "") { txtPlayer1.Text = "Player 1"; } if (txtPlayer2.Text == "") { txtPlayer2.Text = "Player 2"; } if (winner != ' ') { List <Tuple <int, int> > winningSpots = winResult.Item2; string winnerName = ""; if (winner == 'X') { winnerName = txtPlayer1.Text; player1Wins++; lblPlayer1Wins.Content = player1Wins.ToString(); } else { winnerName = txtPlayer2.Text; player2Wins++; lblPlayer2Wins.Content = player2Wins.ToString(); } string message = string.Format("Winner is {0}!", winnerName); EndGame(message, winResult); } else if (TTT_Game.BoardIsFull()) { ties++; lblTie.Content = ties.ToString(); string message = string.Format("It's a TIE!"); EndGame(message, null); } else { if (PlayVsAi) { int difficulty = (int)sliderDifficulty.Value; Game G = new Game(TTT_Game); Spot spot = AiPlayer.GetBestMove(G, difficulty); int r = spot.row; int c = spot.col; TTT_Game.MarkGameBoard(r, c); Draw(Game.ROWS - r - 1, c, GetPlayerColor('O')); winResult = TTT_Game.checkWinner(); if (TTT_Game.checkWinner().Item1 != ' ') { string message = string.Format("Winner is {0}!", txtPlayer2.Text); EndGame(message, winResult); } else if (TTT_Game.BoardIsFull()) { string message = string.Format("It's a TIE!"); EndGame(message, null); } } } }
public Spot GetBestMove(Game G, int difficulty) { Spot s = Minimax(G, 0, difficulty, G.getTurn()); return(s); }