Exemple #1
0
        public void AddMove_success()
        {
            //Arrange
            var move = new MoveHistory
            {
                move   = _fixture.Create <Card>(),
                Player = _human.Object
            };

            //Act

            //Assert
            Assert.DoesNotThrow(() => _state.AddMove(move));
        }
        public void TakeTurn(IGameState state)
        {
            //For simplicity the skill we assume is that this player will rank it's cards and play the card with the highest score

            /******************************
             * ** card scoring rules     **
             * ****************************
             *
             * Suit with most cards is highest value suit
             * scores 4-1
             *
             * add count below 7 vs count above 7
             *
             * */

            IDictionary <Card, int> scores = CalculateScore();

            //Now we have our dictionary of best cards we need to find the highest scoring eligible card and play it
            var bestCards = scores.OrderByDescending(x => x.Value).Select(x => x.Key).ToList();

            var move = new MoveHistory
            {
                Player = this
            };

            foreach (var card in bestCards)
            {
                if (_board.IsEligible(card))
                {
                    //Play the card
                    _board.SetGameBoardState(card);
                    UpdateHand(card, true);
                    move.move = card;

                    state.AddMove(move);
                    return;
                }
            }

            //if no cards are eligible, no cards are played.
            state.AddMove(move);
        }
Exemple #3
0
        public void TakeTurn(IGameState state)
        {
            var move = new MoveHistory
            {
                Player = this
            };

            foreach (var card in _hand)
            {
                if (_board.IsEligible(card))
                {
                    //Play the card
                    _board.SetGameBoardState(card);
                    UpdateHand(card, true);

                    move.move = card;
                    state.AddMove(move);
                    return;
                }
            }

            state.AddMove(move);
        }
Exemple #4
0
        public void TakeTurn(Card?card, IGameState state)
        {
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            var move = new MoveHistory
            {
                Player = this
            };

            if (card != null)
            {
                _board.SetGameBoardState(card);
                UpdateHand(card, true);
                move.move = card;
            }

            state.AddMove(move);
        }
Exemple #5
0
        public void FirstTurn(IGameState state)
        {
            // If first turn check if can play
            if (_board.IsFirstTurn())
            {
                if (HasSevenDiamonds())
                {
                    //Play the seven of diamonds
                    var card = new Card(Suits.Diamonds, CardValue.Seven);

                    _board.SetGameBoardState(card);
                    UpdateHand(card, true);

                    var move = new MoveHistory
                    {
                        Player = this,
                        move   = card
                    };

                    state.AddMove(move);
                    return;
                }
            }
        }
Exemple #6
0
    public MoveMgr.Move GetMove(IGameState rootState, int playerNum, int maxIterations = 5000)
    {
        // Tree reuse

        /*
         * if (null == _root)
         * {
         *  _root = new Node(rootState, MoveMgr.Move.Fetch(playerNum, 0, 0), null);
         * } else
         * {
         *  _root = _root.AdvanceToCurrentState(rootState);
         * }
         */

        // Fresh tree each time (clears visits and probablities)
        ClearState();
        _root = new Node(rootState, MoveMgr.Move.Fetch(playerNum, 0, 0), null);



        for (int i = 0; i < maxIterations; ++i)
        {
            Node       node  = _root; //TODO: may need to recreate tree each time until probablites array and visits array implemented.  Need to save state for each player.
            IGameState state = rootState.CloneState();

            // Select
            while (node.HasUntriedMoves == false && node.HasChildren) // Fully expanded and non-terminal
            {
                node = node.UCTSelectChild();
                state.AddMove(node.Move);
            }

            // Expand
            if (node.HasUntriedMoves)
            {
                MoveMgr.Move move = node.GetRandomUntriedMove();
                state.AddMove(move);
                node = node.AddChild(move, state);  // Add child and descend
            }

            // Simulate (Rollout - not adding nodes to Terminal state )
            while (!state.IsGameOver)
            {
                MoveMgr.Move move = state.GetRandomMove(state.PlayerTurn);
                state.AddMove(move);
            }

            // Prioritize if a win or lose, blacklist/whitelist
            if (node.IsTerminal && !node.IsDraw)
            {
                if (node.Winner != playerNum)
                {
                    // must blacklist parent which is 'this' players move.
                    //This player cannot lose on their turn, but can play a move that will make them lose on opponents turn.
                    if (null != node.Parent)
                    {
                        node.BlackListParent();
                    }
                }
                else
                {
                    node.WhiteList();
                }
            }

            // Backpropagate
            while (null != node)
            {
                node.Update(state.GetResult(node.Move, playerNum));
                node = node.Parent;
            }
        }
        _root = _root.GetMostVisitedChild();

        _root.TrimTreeTop();

        return(_root.Move);
    }