Esempio n. 1
0
        public void MakeMove(SimulationState state)
        {
            var bestMoves = AlphaBeta(state);
            var bestMove  = bestMoves[bestMoves.Count - 1];

            //Now we can preplace our pieces, we have to resimulate the simulation to do so however, as we only know the best moves to make, not who makes them
            int preplaceAmount = 0;

            _lookahead.Clear();
            state.CloneTo(_replaySimulation);
            _replaySimulation.Fidelity = SimulationFidelity.NoPiecePlacing;
            for (var i = bestMoves.Count - 1; i >= 0; i--)
            {
                var move       = bestMoves[i];
                var player     = _replaySimulation.ActivePlayer;
                var patchIndex = _replaySimulation.LeatherPatchesIndex;

                //We purchased a piece
                if (player == state.ActivePlayer && move != -1)
                {
                    _lookahead.Add(PieceDefinition.AllPieceDefinitions[_replaySimulation.Pieces[(_replaySimulation.NextPieceIndex + move) % _replaySimulation.Pieces.Count]]);
                }

                if (move == -1)
                {
                    _replaySimulation.PerformAdvanceMove();
                }
                else
                {
                    _replaySimulation.PerformPurchasePiece(_replaySimulation.NextPieceIndex + move);
                }

                //If we moved past a leather patch, we must have got it
                if (player == state.ActivePlayer && _replaySimulation.LeatherPatchesIndex > patchIndex)
                {
                    _lookahead.Add(PieceDefinition.LeatherTile);
                }

                if (i == bestMoves.Count - 1)
                {
                    preplaceAmount = _lookahead.Count;
                }
            }

            //Tell the Preplacer what we are planning on getting
            if (preplaceAmount > 0)
            {
                _preplacer.PreparePlacePiece(state.PlayerBoardState[state.ActivePlayer], _lookahead, preplaceAmount);
            }

            if (bestMove == -1)
            {
                state.PerformAdvanceMove();
            }
            else
            {
                state.PerformPurchasePiece(state.NextPieceIndex + bestMove);
            }
        }
Esempio n. 2
0
        public override void MakeMove(SimulationState state)
        {
            var root = Mcts.PerformMCTS(state, true, 0);

            //DumpChildren(root);

            _lookahead.Clear();

            SearchNode bestRootChild  = null;
            int        preplaceAmount = 0;

            //Go through what we currently think are our best moves and record the pieces we'll get
            while (root.Children.Count > 0)
            {
                var bestChild = Mcts.FindBestChild(root);

                if (bestRootChild == null)
                {
                    bestRootChild = bestChild;
                }

                //If this move was made by us
                if (root.State.ActivePlayer == state.ActivePlayer)
                {
                    if (bestChild.PieceToPurchase.HasValue)
                    {
                        _lookahead.Add(PieceDefinition.AllPieceDefinitions[root.State.Pieces[bestChild.PieceToPurchase.Value % root.State.Pieces.Count]]);
                    }

                    //If we moved past a leather patch, we must have got it
                    if (bestChild.State.LeatherPatchesIndex > root.State.LeatherPatchesIndex)
                    {
                        _lookahead.Add(PieceDefinition.LeatherTile);
                    }
                }

                //Only need to preplace if we get a piece in our first move
                if (bestRootChild == bestChild && _lookahead.Count > 0)
                {
                    //If we have made one move and got 2 pieces, that means we purchased and got a leather tile, so we need to plan for that
                    preplaceAmount = _lookahead.Count;
                }

                root = bestChild;
            }


            //Tell the Preplacer what we are planning on getting
            if (preplaceAmount > 0)
            {
                _preplacer.PreparePlacePiece(state.PlayerBoardState[state.ActivePlayer], _lookahead, preplaceAmount);
            }


            if (bestRootChild.PieceToPurchase.HasValue)
            {
                state.PerformPurchasePiece(bestRootChild.PieceToPurchase.Value);
            }
            else
            {
                state.PerformAdvanceMove();
            }

            MonteCarloTreeSearch <SearchNode> .NodePool.Value.ReturnAll();
        }