コード例 #1
0
ファイル: Form1.cs プロジェクト: James-P-D/TicTacToeMinimax
        /// <summary>
        /// Start a game. Clear the board, display the UI and if the first move is human,
        /// let the user click a button, otherwise use Minimax to calculate computer move.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StartButton_Click(object sender, EventArgs e)
        {
            this.startButton.Enabled = false;
            this._board = new Board();

            /*
             * this.board = new Board(new Board.Cell[] {
             *  Board.Cell.Computer, Board.Cell.Empty, Board.Cell.Human,
             *  Board.Cell.Empty, Board.Cell.Human,Board.Cell.Empty,
             *  Board.Cell.Computer, Board.Cell.Computer, Board.Cell.Human
             * });
             */
            this.DrawBoard();
            this.bottomInformationTextBox.Text = string.Empty;

            if (this._currentMoveIsHuman)
            {
                this.topInformationTextBox.Text = Resources.Form1_Humans_turn;
            }
            else
            {
                this.topInformationTextBox.Text = Resources.Form1_Computers_turn;
                var miniMaxTree = new MiniMaxTree(_board);
                var bestMove    = miniMaxTree.GetBestMove();
                _board.Cells[bestMove.UpdatedCellIndex] = Board.Cell.Computer;
                this.Process();
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: James-P-D/TicTacToeMinimax
 /// <summary>
 /// Main method for actually processing the board. Update the UI and check to see
 /// if someone has won, or we have a draw. If the game is not yet over, let the user
 /// press a button, or calculate the next computer move accordingly.
 /// </summary>
 private void Process()
 {
     this.DrawBoard();
     if (this._board.GetState() == Board.State.ComputerWins)
     {
         this.bottomInformationTextBox.Text = Resources.Form1_Computer_wins;
         this.DisableAllCellButtons();
         this.topInformationTextBox.Text = Resources.Form1_Press_Start_to_play_a_game;
         this.startButton.Enabled        = true;
         this._currentMoveIsHuman        = !this._currentMoveIsHuman;
     }
     else if (this._board.GetState() == Board.State.HumanWins)
     {
         this.bottomInformationTextBox.Text = Resources.Form1_Human_wins;
         this.DisableAllCellButtons();
         this.topInformationTextBox.Text = Resources.Form1_Press_Start_to_play_a_game;
         this.startButton.Enabled        = true;
         this._currentMoveIsHuman        = !this._currentMoveIsHuman;
     }
     else if (this._board.GetState() == Board.State.Draw)
     {
         this.bottomInformationTextBox.Text = Resources.From1_Draw;
         this.DisableAllCellButtons();
         this.topInformationTextBox.Text = Resources.Form1_Press_Start_to_play_a_game;
         this.startButton.Enabled        = true;
         this._currentMoveIsHuman        = !this._currentMoveIsHuman;
     }
     else
     {
         // The game hasn't finished yet (noone has won and it isn't a draw), so invert
         // the current-move boolean.
         this._currentMoveIsHuman = !this._currentMoveIsHuman;
         if (this._currentMoveIsHuman)
         {
             // If the next move is human, set the text box and do nothing (wait for
             // user to actually press a button)
             this.topInformationTextBox.Text = Resources.Form1_Humans_turn;
         }
         else
         {
             // If the next move is computer, use Minimax to calculate the next move
             // based on the current board
             this.topInformationTextBox.Text = Resources.Form1_Computers_turn;
             var miniMaxTree = new MiniMaxTree(_board);
             var bestMove    = miniMaxTree.GetBestMove();
             _board.Cells[bestMove.UpdatedCellIndex] = Board.Cell.Computer;
             this.Process();
         }
     }
 }