Ejemplo n.º 1
0
        private bool DoBotMove()
        {
            var nextBotMove = GetBotMove();

            if (nextBotMove == null)
            {
                return(false);
            }

            if (!GameAnalysis.IsMoveLegal(currentBoardState, nextBotMove))
            {
                WinnerAvailable?.Invoke(humanPlayer, WinningReason.InvalidMove, nextBotMove);
                return(false);
            }

            currentBoardState = currentBoardState.ApplyMove(nextBotMove);
            NewBoardStateAvailable?.Invoke(currentBoardState);

            if (nextBotMove is Capitulation)
            {
                WinnerAvailable?.Invoke(humanPlayer, WinningReason.Capitulation, null);
            }

            var winner2 = GameAnalysis.CheckWinningCondition(currentBoardState);

            if (winner2 != null)
            {
                WinnerAvailable?.Invoke(winner2, WinningReason.RegularQuoridorWin, null);
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
 public MonteCarloNode(MonteCarloNode Parent, Point point)
 {
     score          = 0;
     timesVisited   = 0;
     ai             = Parent.ai;
     parent         = Parent;
     children       = new List <MonteCarloNode> ();
     availableMoves = new List <MonteCarloNode> (parent.availableMoves);
     this.point     = point;
     board          = new BoardState(parent.board);
     board.ApplyMove(board.PlacePiece(point));
     board.turn = board.turn == BoardState.GameTurn.Computer ? BoardState.GameTurn.Player : BoardState.GameTurn.Computer;
     board.GenerateAvailableMoves();
     //AddAvailableMoves(board.availableMoves.Keys.ToList());
 }
Ejemplo n.º 3
0
    public int Simulate(MonteCarloNode node)
    {
        //Debug.Log ("simulate" );
        BoardState board = new BoardState(node.board);

        UnityEngine.Random.seed = (int)Time.timeSinceLevelLoad;
        board.GenerateAvailableMoves();
        while (board.availableMoves.Count != 0)
        {
            List <Point> moves = board.availableMoves.Keys.ToList();
            int          i     = UnityEngine.Random.Range(0, moves.Count);

            //List<Point> p = new List<Point>();W

            /*if(!board.AvailableMove(moves[i], ref p))
             * {
             *
             * }
             *
             * Debug.Log (i + "  " + moves.Count() );*/

            board.ApplyMove(board.PlacePiece(moves[i]));
            board.GenerateAvailableMoves();
        }
        //Debug.Log ("simulated");
        if (board.WhiteCount > board.BlackCount)
        {
            return(1);
        }
        else if (board.WhiteCount > board.BlackCount)
        {
            return(-1);
        }
        else
        {
            return(0);
        }
    }
Ejemplo n.º 4
0
        public void Run()
        {
            IsRunning = true;

            NewBoardStateAvailable?.Invoke(currentBoardState);

            var moveCounter = 0;

            while (!stopRunning)
            {
                if (moveCounter >= gameConstraints.MaximalMovesPerPlayer)
                {
                    WinnerAvailable?.Invoke(currentBoardState.TopPlayer.Player, WinningReason.ExceedanceOfMaxMoves);
                }

                var nextBottomPlayerBotMove = GetBottomPlayerBotMove();

                if (nextBottomPlayerBotMove == null)
                {
                    break;
                }

                if (!GameAnalysis.IsMoveLegal(currentBoardState, nextBottomPlayerBotMove))
                {
                    WinnerAvailable?.Invoke(currentBoardState.TopPlayer.Player, WinningReason.InvalidMove);
                    break;
                }

                currentBoardState = currentBoardState.ApplyMove(nextBottomPlayerBotMove);
                NewBoardStateAvailable?.Invoke(currentBoardState);

                if (nextBottomPlayerBotMove is Capitulation)
                {
                    WinnerAvailable?.Invoke(currentBoardState.TopPlayer.Player, WinningReason.Capitulation);
                }

                var winner = GameAnalysis.CheckWinningCondition(currentBoardState);
                if (winner != null)
                {
                    WinnerAvailable?.Invoke(winner, WinningReason.RegularQuoridorWin);
                    break;
                }

                var nextTopPlayerBotMove = GetTopPlayerBotMove();

                if (nextTopPlayerBotMove == null)
                {
                    break;
                }

                if (!GameAnalysis.IsMoveLegal(currentBoardState, nextTopPlayerBotMove))
                {
                    WinnerAvailable?.Invoke(currentBoardState.BottomPlayer.Player, WinningReason.InvalidMove);
                    break;
                }

                currentBoardState = currentBoardState.ApplyMove(nextTopPlayerBotMove);
                NewBoardStateAvailable?.Invoke(currentBoardState);

                if (nextTopPlayerBotMove is Capitulation)
                {
                    WinnerAvailable?.Invoke(currentBoardState.BottomPlayer.Player, WinningReason.Capitulation);
                }

                var winner2 = GameAnalysis.CheckWinningCondition(currentBoardState);
                if (winner2 != null)
                {
                    WinnerAvailable?.Invoke(winner2, WinningReason.RegularQuoridorWin);
                    break;
                }

                moveCounter++;
            }

            IsRunning = false;

            bottomPlayerBot.NextMoveAvailable -= OnNextBottomPlayerBotMoveAvailable;
            topPlayerBot.NextMoveAvailable    -= OnNextTopPlayerBotMoveAvailable;
        }
Ejemplo n.º 5
0
        public void Run()
        {
            IsRunning = true;

            computerPlayer = new Player(PlayerType.TopPlayer, botName);
            humanPlayer    = new Player(PlayerType.BottomPlayer);

            bot.Init(computerPlayer.PlayerType, gameConstraints);

            currentBoardState = BoardStateTransition.CreateInitialBoadState(computerPlayer, humanPlayer);
            NewBoardStateAvailable?.Invoke(currentBoardState);

            var moveCounter = 0;

            if (initialProgress != null)
            {
                var moves = ParseProgressText.FromFileText(initialProgress)
                            .Select(MoveParser.GetMove);

                foreach (var move in moves)
                {
                    currentBoardState = currentBoardState.ApplyMove(move);
                    NewBoardStateAvailable?.Invoke(currentBoardState);
                }

                if (moves.Count() % 2 == 1)
                {
                    var succeedGame = DoBotMove();

                    if (!succeedGame)
                    {
                        IsRunning              = false;
                        bot.NextMoveAvailable -= OnNextBotMoveAvailable;
                        return;
                    }
                }

                moveCounter = (int)Math.Ceiling(moves.Count() / 2.0);
            }

            while (!stopRunning)
            {
                if (moveCounter >= gameConstraints.MaximalMovesPerPlayer)
                {
                    WinnerAvailable?.Invoke(computerPlayer, WinningReason.ExceedanceOfMaxMoves, null);
                }

                bool succeedGame;

                succeedGame = DoHumanMove();
                if (!succeedGame)
                {
                    break;
                }


                succeedGame = DoBotMove();
                if (!succeedGame)
                {
                    break;
                }

                moveCounter++;
            }

            IsRunning              = false;
            bot.NextMoveAvailable -= OnNextBotMoveAvailable;
        }