Example #1
0
        public Move GetMove(Board board, BoardState state)
        {
            Move[] moves = board.GetPossibleMoves(state);

            Thread.Sleep(3500);

            return(moves[random.Next(moves.Length)]);
        }
Example #2
0
        private MCTSNode(MCTSNode parent, Board board, BoardState state)
        {
            this.board = board;
            State      = state;
            Visits     = 0;
            Score      = 0;
            Parent     = parent;

            IsTerminal = board.GetPossibleMoves(state).Length == 0;
        }
Example #3
0
        public void Expand(PieceType currentPieceType)
        {
            Children = board.GetPossibleMoves(State).Select(
                x => new MCTSNode(this, board,
                                  State.PlayMove(x)
                                  )
                ).ToArray();

            bool   allTerminal = true;
            double totalValue  = 0;

            foreach (MCTSNode child in Children)
            {
                child.Visits = 1;
                double value;
                if (child.IsTerminal)
                {
                    value = child.Score = child.GetTerminalScore(currentPieceType);
                }
                else
                {
                    allTerminal = false;
                    value       = child.Score = child.Simulate(currentPieceType);
                }
                totalValue += value;
            }

            if (allTerminal)
            {
                IsTerminal = true;
                UpdateTerminal();
            }
            else
            {
                UpdateValue(totalValue, Children.Length);
            }
        }
Example #4
0
        private void run()
        {
            while (gameState != GameState.End)
            {
                if (gameState == GameState.Start)
                {
                    HandleStart();
                }
                else if (gameState == GameState.RedToMove)
                {
                    // Ask the player for a move
                    Move move = redPlayer.GetMove(board, boardState);
                    boardState = boardState.PlayMove(move);

                    // Fire the event player moved
                    if (OnPlayerMove != null)
                    {
                        PlayerMoveEventArgs playerMoveEventArgs = new PlayerMoveEventArgs(board, boardState, redPlayer, move);
                        OnPlayerMove(this, playerMoveEventArgs);
                    }

                    gameState = GameState.RedMoved;
                }
                else if (gameState == GameState.RedMoved)
                {
                    // Check if there are any possible moves
                    Move[] moves = board.GetPossibleMoves(boardState);

                    if (moves.Length == 0)
                    {
                        if (OnGameEnd != null)
                        {
                            GameBoardEventArgs gameBoardEventArgs = new GameBoardEventArgs(board, boardState);
                            OnGameEnd(this, gameBoardEventArgs);
                        }
                        gameState = GameState.End;
                    }
                    else
                    {
                        if (OnPlayerChanged != null)
                        {
                            PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, blackPlayer);
                            OnPlayerChanged(this, playerEventArgs);
                        }
                        gameState = GameState.BlackToMove;
                    }
                }
                else if (gameState == GameState.BlackToMove)
                {
                    // Ask the player for a move
                    Move move = blackPlayer.GetMove(board, boardState);
                    boardState = boardState.PlayMove(move);

                    // Fire the event player moved
                    if (OnPlayerMove != null)
                    {
                        PlayerMoveEventArgs playerMoveEventArgs = new PlayerMoveEventArgs(board, boardState, blackPlayer, move);
                        OnPlayerMove(this, playerMoveEventArgs);
                    }
                    gameState = GameState.BlackMoved;
                }
                else if (gameState == GameState.BlackMoved)
                {
                    // Check if there are any possible moves
                    Move[] moves = board.GetPossibleMoves(boardState);

                    if (moves.Length == 0)
                    {
                        if (OnGameEnd != null)
                        {
                            GameBoardEventArgs gameBoardEventArgs = new GameBoardEventArgs(board, boardState);
                            OnGameEnd(this, gameBoardEventArgs);
                        }
                        gameState = GameState.End;
                    }
                    else
                    {
                        if (OnPlayerChanged != null)
                        {
                            PlayerEventArgs playerEventArgs = new PlayerEventArgs(board, boardState, redPlayer);
                            OnPlayerChanged(this, playerEventArgs);
                        }
                        gameState = GameState.RedToMove;
                    }
                }
            }
        }