Esempio n. 1
0
 public List <Card> SelectTrick(ScopaGame game, AIScopaPlayer player, Card selectedCard)
 {
     if (selection.SelectedCard != null && player.IsHolding((Card)selection.SelectedCard))
     {
         return(selection.SelectedTrick);
     }
     selection = DoTrickSelection(game, player, selectedCard);
     return(selection.SelectedTrick);
 }
Esempio n. 2
0
 public WeightedTreeNode(WeightedTreeNode parent)
 {
     this.parent    = parent;
     this.children  = new List <WeightedTreeNode>();
     this.selection = new WeightedSelection();
     if (parent != null)
     {
         parent.children.Add(this);
     }
 }
Esempio n. 3
0
        private WeightedSelection DoLevelSelection(GameState state)
        {
            WeightedSelection selection = new WeightedSelection();

            if (state.Hand.Count == 1)
            {
                selection = DoLevelTrickSelection(state, state.Hand[0]);
                return(selection);
            }
            List <Card> playable = state.Hand.FindAll(a => !state.PossibleActions[a].IsThrowable);

            if (playable.Count > 0)
            {
                // Always take the Sette Bello
                if (playable.Contains <Card>(Card.SetteBello))
                {
                    selection = DoLevelTrickSelection(state, Card.SetteBello);
                    return(selection);
                }

                List <WeightedSelection> possibles = new List <WeightedSelection>();
                foreach (Card card in playable)
                {
                    possibles.Add(DoLevelTrickSelection(state, card));
                }
                List <WeightedSelection> actuals = Utilities.MaximumElements <WeightedSelection>(possibles, a => a.Weight);
                selection = actuals[0];
                return(selection);
            }

            // All cards in hand are throwable
            Card?bestThrow = null;
            int  maxWeight = Int32.MinValue;

            foreach (Card card in state.Hand)
            {
                // TODO: This does not take into account that throwing a card can enable a trick,
                // which should be given more weight than throwing a card that does not do so.
                int weight = -CalculateWeight(state.Tracker, card, new List <Card>(), false);
                if (weight > maxWeight)
                {
                    bestThrow = card;
                    maxWeight = weight;
                }
            }

            selection.SelectedCard  = bestThrow;
            selection.SelectedTrick = new List <Card>();
            selection.Weight        = maxWeight;
            return(selection);
        }
Esempio n. 4
0
        public WeightedTreeNode BuildTree(ScopaGame game, IScopaPlayer player, int maxDepth)
        {
            WeightedTreeNode root  = new WeightedTreeNode(null);
            GameState        state = new GameState();

            state.Table           = game.Table;
            state.PossibleActions = game.PossibleActions;
            state.Tracker         = player.GetPossibleScores(new List <Card>(), false);
            state.Hand            = new List <Card>(player.Hand);
            List <object[]> queue = new List <object[]>()
            {
                new object[] { root, state, 0, },
            };

            while (queue.Count > 0)
            {
                WeightedTreeNode currNode  = queue[0][0] as WeightedTreeNode;
                GameState        currState = queue[0][1] as GameState;
                int depth = (int)(queue[0][1] as int?);
                queue.RemoveAt(0);
                if (depth > 0 && depth < maxDepth)
                {
                    WeightedSelection selection = DoLevelSelection(currState);
                    List <Card>       trick     = new List <Card>(selection.SelectedTrick);
                    trick.Add((Card)selection.SelectedCard);
                    GameState childState = new GameState();
                    childState.Table = new List <Card>(game.Table);
                    childState.Table.RemoveAll(a => trick.Contains(a));
                    childState.PossibleActions = new PlayerActions(); // AbstractScopaGame.EnumerateAll(childState.Table, selection.SelectedCard);
                    childState.Tracker         = currState.Tracker.GetPossibleScores(trick, currState.Table.Count == selection.SelectedTrick.Count);
                    childState.Hand            = new List <Card>(currState.Hand);
                    childState.Hand.Remove((Card)selection.SelectedCard);
                    queue.Add(new object[] { new WeightedTreeNode(currNode), childState, depth + 1, });
                    currNode.Selection = selection;
                }
            }
            return(root);
        }
Esempio n. 5
0
        private WeightedSelection DoTrickSelection(ScopaGame game, AIScopaPlayer player, Card selectedCard)
        {
            List <List <Card> > possibleTricks = game.PossibleActions[selectedCard].PossibleTricks;
            List <Card>         bestTrick      = null;
            int maxWeight = Int32.MinValue;

            foreach (List <Card> trick in possibleTricks)
            {
                int weight = CalculateWeight(player, selectedCard, trick, game.Table.Count == trick.Count);
                if (weight > maxWeight)
                {
                    bestTrick = trick;
                    maxWeight = weight;
                }
            }

            WeightedSelection selection = new WeightedSelection();

            selection.SelectedCard  = selectedCard;
            selection.SelectedTrick = bestTrick;
            selection.Weight        = maxWeight;
            return(selection);
        }
Esempio n. 6
0
        private WeightedSelection DoLevelTrickSelection(GameState state, Card selectedCard)
        {
            List <List <Card> > possibleTricks = state.PossibleActions[selectedCard].PossibleTricks;
            List <Card>         bestTrick      = null;
            int maxWeight = Int32.MinValue;

            foreach (List <Card> trick in possibleTricks)
            {
                int weight = CalculateWeight(state.Tracker, selectedCard, trick, state.Table.Count == trick.Count);
                if (weight > maxWeight)
                {
                    bestTrick = trick;
                    maxWeight = weight;
                }
            }

            WeightedSelection selection = new WeightedSelection();

            selection.SelectedCard  = selectedCard;
            selection.SelectedTrick = bestTrick;
            selection.Weight        = maxWeight;
            return(selection);
        }