コード例 #1
0
        public Move GetBestMove(char[] board, int boardSize, int CurrentPlayer, int alpha, int beta, GameLogic logic)
        {
            Move BestMove = null;
            int iPossibleMoves = board.Count(b => b == '-');

            //Start at a random square, so that if two or more moves
            //are of equal rank, one of them will be chosen at random.
            Random rand = new Random();
            int i = rand.Next(boardSize);
            int j = rand.Next(boardSize);

            while (iPossibleMoves > 0)
            {
                //Loop through board.aiBoard to find next available move
                do
                {
                    if (i < boardSize - 1)
                    {
                        i++;
                    }
                    else if (j < boardSize - 1)
                    {
                        i = 0; j++;
                    }
                    else
                    {
                        i = 0; j = 0;
                    }
                } while (board[FromMatrixToIndex(i,j)] != '-');

                Move NewMove = new Move(i, j);
                iPossibleMoves--;

                //Make Move
                char[] NewBoard = (new string(board)).ToCharArray();

                NewBoard[FromMatrixToIndex(NewMove.iRow, NewMove.iCol)] = 'O';
                var result = logic.GetResult(NewBoard);

                if (result == GameResult.NotFinished)
                {
                    Move tempMove = GetBestMove(NewBoard, 3, -CurrentPlayer, alpha, beta, logic);
                    NewMove.iRank = tempMove.iRank;
                }
                else
                {
                    //Assign a rank
                    if (result == GameResult.NotFinished)
                        NewMove.iRank = 0;
                    else
                    {
                        if (result == GameResult.WonByO)
                            NewMove.iRank = -1;
                        else
                        {
                            if (result == GameResult.WonByX)
                                NewMove.iRank = 1;
                        }
                    }
                }

                //Is NewMove the best move encountered at this level so far?
                if (BestMove == null ||
                    (CurrentPlayer == 1 && NewMove.iRank < BestMove.iRank) ||
                    (CurrentPlayer == -1 && NewMove.iRank > BestMove.iRank))
                {
                    BestMove = NewMove;
                }


                //Update alpha and beta, if necessary.
                if (CurrentPlayer == 1 && BestMove.iRank < beta)
                    beta = BestMove.iRank;
                if (CurrentPlayer == -1 && BestMove.iRank > alpha)
                    alpha = BestMove.iRank;


                //Check for pruning condition.
                if (alpha > beta)
                {
                    //prune this branch
                    iPossibleMoves = 0;
                }
            }
            return BestMove;
        }
コード例 #2
0
        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;
            }
        }