Esempio n. 1
0
        public override int PlayGame(PerfectInformationGame pig, int alpha, int beta, int depthLimit, int card = -1)
        {
            if (Sueca.UTILITY_FUNC == 2 && pig.IsAnyTeamWinning())
            {
                return(pig.EvalGame2());
            }
            if (pig.reachedDepthLimit(depthLimit) || pig.IsEndGame())
            {
                return(pig.EvalGame1());
            }

            int chosenCard;

            if (card != -1)
            {
                chosenCard = card;
            }
            else
            {
                chosenCard = InfoSet.RuleBasedDecision();
            }
            Move move = new Move(Id, chosenCard);

            pig.ApplyMove(move);

            int value = pig.GetNextPlayer().PlayGame(pig, alpha, beta, depthLimit);

            pig.UndoMove(move);

            return(value);
        }
Esempio n. 2
0
        public override int PlayGame(PerfectInformationGame pig, int alpha, int beta, int depthLimit, int card)
        {
            int        v = Int16.MinValue;
            List <int> cards;

            if (Sueca.UTILITY_FUNC == 2 && pig.IsAnyTeamWinning())
            {
                return(pig.EvalGame2());
            }
            if (pig.reachedDepthLimit(depthLimit) || pig.IsEndGame())
            {
                return(pig.EvalGame1());
            }

            if (card == -1)
            {
                cards = Sueca.PossibleMoves(Hand, pig.GetLeadSuit());
                pig.orderPossibleMoves(cards, Id);
            }
            else
            {
                cards = new List <int>();
                cards.Add(card);
            }

            foreach (int c in cards)
            {
                Move move = new Move(Id, c);
                pig.ApplyMove(move);
                int moveValue = pig.GetNextPlayer().PlayGame(pig, alpha, beta, depthLimit);

                if (moveValue > v)
                {
                    v = moveValue;
                }

                pig.UndoMove(move);

                if (v >= beta)
                {
                    return(v);
                }

                if (v > alpha)
                {
                    alpha = v;
                }
            }

            return(v);
        }
Esempio n. 3
0
        private static int[] HybridSearchMainLoop(int[] cardsSamplingValues, Object thisLock, int playerId, InformationSet infoSet, List <int> possibleMoves, int M, int handSize)
        {
            for (int i = 0; i < possibleMoves.Count; i++)
            {
                cardsSamplingValues[i * 2]       = possibleMoves[i];
                cardsSamplingValues[(i * 2) + 1] = 0;
            }

            List <List <int> > playersHands;

            lock (thisLock)
            {
                playersHands = infoSet.Sample();
            }
            PerfectInformationGame game;
            int cardUtility;

            for (int j = 0; j < possibleMoves.Count; j++)
            {
                int card = possibleMoves[j];
                int hybridTrickChange = handSize - 5;

                if (handSize > 5)
                {
                    for (int k = 0; k < M; k++)
                    {
                        RuleBasedNode p0 = new RuleBasedNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p1 = new RuleBasedNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p2 = new RuleBasedNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p3 = new RuleBasedNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        game        = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints, true, hybridTrickChange);
                        cardUtility = game.SampleGame(1000, card);
                        cardsSamplingValues[j * 2 + 1] += cardUtility;
                    }
                }
                else
                {
                    MaxNode p0 = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    MinNode p1 = new MinNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    MaxNode p2 = new MaxNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    MinNode p3 = new MinNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    game        = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    cardUtility = game.SampleGame(1000, card);
                    cardsSamplingValues[j * 2 + 1] += cardUtility;
                }
            }
            return(cardsSamplingValues);
        }
Esempio n. 4
0
        public static int ExecuteWithTimeLimit(int playerId, InformationSet infoSet, List <int> depthLimits)
        {
            List <int> possibleMoves = infoSet.GetPossibleMoves();

            if (possibleMoves.Count == 1)
            {
                return(possibleMoves[0]);
            }

            Dictionary <int, int> dict = new Dictionary <int, int>();

            foreach (int card in possibleMoves)
            {
                dict.Add(card, 0);
            }

            int       depthLimit, handSize = infoSet.GetHandSize();
            int       n  = 0;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            long time = sw.ElapsedMilliseconds;

            depthLimit = depthLimits[handSize - 1];
            for (; time < 2000;)
            {
                n++;
                List <List <int> > playersHands = infoSet.Sample();

                PerfectInformationGame game;
                int cardUtility;

                for (int j = 0; j < possibleMoves.Count; j++)
                {
                    int     card = possibleMoves[j];
                    MaxNode p0   = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    MinNode p1   = new MinNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    MaxNode p2   = new MaxNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    MinNode p3   = new MinNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                    game        = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    cardUtility = game.SampleGame(depthLimit, card);
                    dict[card] += cardUtility;
                }
                time = sw.ElapsedMilliseconds;
            }

            sw.Stop();

            int bestCard  = -1;
            int bestValue = Int32.MinValue;

            foreach (KeyValuePair <int, int> cardValue in dict)
            {
                if (cardValue.Value >= bestValue)
                {
                    bestValue = (int)cardValue.Value;
                    bestCard  = cardValue.Key;
                }
            }

            if (bestCard == -1)
            {
                Console.WriteLine("Trouble at InformationSet.GetBestCardAndValue()");
            }

            return(bestCard);
        }
Esempio n. 5
0
        // version = 0 - The search is the MinMax algorithm
        // version = 1 - The search strats at the last 5 tricks with the MinMax algorithm, until there choices are rulebased
        public static int Execute(int playerId, InformationSet infoSet, int version, List <int> numIterations = null, List <int> depthLimits = null)
        {
            List <int> possibleMoves = infoSet.GetPossibleMoves();

            if (possibleMoves.Count == 1)
            {
                return(possibleMoves[0]);
            }

            Dictionary <int, int> dict = new Dictionary <int, int>();

            foreach (int card in possibleMoves)
            {
                dict.Add(card, 0);
            }


            int N, depthLimit, handSize = infoSet.GetHandSize();

            if (numIterations != null)
            {
                N = numIterations[handSize - 1];
                if (depthLimits != null)
                {
                    depthLimit = depthLimits[handSize - 1];
                }
                else
                {
                    depthLimit = 10;
                }
            }
            else
            {
                N          = 50;
                depthLimit = 1;
            }

            var watch = Stopwatch.StartNew();

            for (int i = 0; i < N; i++)
            {
                if (watch.ElapsedMilliseconds > Sueca.MAX_MILISEC_DELIBERATION)
                {
                    watch.Stop();
                    return(infoSet.RuleBasedDecision());
                }

                List <List <int> > playersHands = infoSet.Sample();

                PerfectInformationGame game;
                int cardUtility;

                for (int j = 0; j < possibleMoves.Count; j++)
                {
                    int card = possibleMoves[j];
                    if (version == 0)
                    {
                        MaxNode p0 = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p1 = new MinNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MaxNode p2 = new MaxNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p3 = new MinNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        game = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    }
                    else if (version == 1)
                    {
                        MaxNode       p0 = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p1 = new RuleBasedNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p2 = new RuleBasedNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p3 = new RuleBasedNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        game = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    }
                    else
                    {
                        Console.WriteLine("PIMC::Execute >> Undefinied version of the algorithm.");
                        MaxNode p0 = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p1 = new MinNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MaxNode p2 = new MaxNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p3 = new MinNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        game = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    }

                    cardUtility = game.SampleGame(depthLimit, card);
                    if (cardUtility > 120 || cardUtility < -120)
                    {
                        Console.WriteLine("lol");
                    }
                    dict[card] += cardUtility;
                }
            }
            watch.Stop();


            int bestCard  = -1;
            int bestValue = Int32.MinValue;

            int suitseven = 4, suitace = 5;
            int valueseven = Int32.MinValue;
            int valueace   = valueseven + 1;
            int key        = -1;

            Console.WriteLine("Printing dictionary:");

            foreach (KeyValuePair <int, int> card in dict)
            {
                if (Card.GetRank(card.Key) == 8)
                {
                    suitseven  = Card.GetSuit(card.Key);
                    valueseven = card.Value;
                }

                if (Card.GetRank(card.Key) == 9)
                {
                    suitace  = Card.GetSuit(card.Key);
                    valueace = card.Value;
                }

                if (suitseven == suitace)
                {
                    if (valueseven > valueace)
                    {
                        Console.Write("-----" + card.Key + "----");
                        //suitace *= 10;
                        //int key = 9 + suitace;
                        // var newEntry = new KeyValuePair<int, int>(card.Key, valueseven);
                        key = card.Key;
                    }
                }
                Console.Write(Card.ToString(card.Key) + ":" + card.Value + ", ");
            }
            if (key != -1)
            {
                dict.Remove(key);
                dict.Add(key, valueseven);
            }

            Console.WriteLine();

            foreach (KeyValuePair <int, int> cardValue in dict)
            {
                if (cardValue.Value >= bestValue)
                {
                    bool flag = false;

                    if (Card.ToString(cardValue.Key)[0] == '7')
                    {
                        foreach (KeyValuePair <int, int> card in dict)
                        {
                            if (Card.ToString(card.Key)[0] == 'A' && Card.ToString(card.Key)[1] == Card.ToString(cardValue.Key)[1])
                            {
                                flag = true;
                                //Console.WriteLine("Switch 7 with A");
                            }
                        }
                    }

                    if (!flag)
                    {
                        bestValue = (int)cardValue.Value;
                        bestCard  = cardValue.Key;
                        Console.WriteLine("bestCard: " + Card.ToString(bestCard));
                    }
                    else
                    {
                        Console.WriteLine("Switch 7 with A");
                    }
                }
            }
            Console.WriteLine();

            if (bestCard == -1)
            {
                Console.WriteLine("Trouble at InformationSet.GetBestCardAndValue()");
            }

            return(bestCard);
        }
Esempio n. 6
0
        // version = 0 - The search is the MinMax algorithm
        // version = 1 - The search strats at the last 5 tricks with the MinMax algorithm, until there choices are rulebased
        public static int Execute(int playerId, InformationSet infoSet, int version, List <int> numIterations = null, List <int> depthLimits = null)
        {
            List <int> possibleMoves = infoSet.GetPossibleMoves();

            if (possibleMoves.Count == 1)
            {
                return(possibleMoves[0]);
            }

            Dictionary <int, int> dict = new Dictionary <int, int>();

            foreach (int card in possibleMoves)
            {
                dict.Add(card, 0);
            }


            int N, depthLimit, handSize = infoSet.GetHandSize();

            if (numIterations != null)
            {
                N = numIterations[handSize - 1];
                if (depthLimits != null)
                {
                    depthLimit = depthLimits[handSize - 1];
                }
                else
                {
                    depthLimit = 10;
                }
            }
            else
            {
                N          = 50;
                depthLimit = 1;
            }

            var watch = Stopwatch.StartNew();

            for (int i = 0; i < N; i++)
            {
                if (watch.ElapsedMilliseconds > Sueca.MAX_MILISEC_DELIBERATION)
                {
                    watch.Stop();
                    return(infoSet.RuleBasedDecision());
                }

                List <List <int> > playersHands = infoSet.Sample();

                PerfectInformationGame game;
                int cardUtility;

                for (int j = 0; j < possibleMoves.Count; j++)
                {
                    int card = possibleMoves[j];
                    if (version == 0)
                    {
                        MaxNode p0 = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p1 = new MinNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MaxNode p2 = new MaxNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p3 = new MinNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        game = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    }
                    else if (version == 1)
                    {
                        MaxNode       p0 = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p1 = new RuleBasedNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p2 = new RuleBasedNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        RuleBasedNode p3 = new RuleBasedNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        game = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    }
                    else
                    {
                        Console.WriteLine("PIMC::Execute >> Undefinied version of the algorithm.");
                        MaxNode p0 = new MaxNode(playerId, playersHands[0], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p1 = new MinNode((playerId + 1) % 4, playersHands[1], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MaxNode p2 = new MaxNode((playerId + 2) % 4, playersHands[2], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        MinNode p3 = new MinNode((playerId + 3) % 4, playersHands[3], infoSet.TrumpCard, infoSet.TrumpPlayerId);
                        game = new PerfectInformationGame(p0, p1, p2, p3, infoSet.Trump, infoSet.GetPastMoves(), infoSet.MyTeamPoints, infoSet.OtherTeamPoints);
                    }

                    cardUtility = game.SampleGame(depthLimit, card);
                    if (cardUtility > 120 || cardUtility < -120)
                    {
                        Console.WriteLine("lol");
                    }
                    dict[card] += cardUtility;
                }
            }
            watch.Stop();


            int bestCard  = -1;
            int bestValue = Int32.MinValue;

            //Console.WriteLine("Printing dictionary:");
            foreach (KeyValuePair <int, int> cardValue in dict)
            {
                //Console.Write(Card.ToString(cardValue.Key) + ":" + cardValue.Value + ", ");
                if (cardValue.Value >= bestValue)
                {
                    bestValue = (int)cardValue.Value;
                    bestCard  = cardValue.Key;
                }
            }
            //Console.WriteLine("");

            if (bestCard == -1)
            {
                Console.WriteLine("Trouble at InformationSet.GetBestCardAndValue()");
            }

            return(bestCard);
        }
Esempio n. 7
0
 public abstract int PlayGame(PerfectInformationGame pig, int alpha, int beta, int depthLimit, int card = -1);