Ejemplo n.º 1
0
        public bool CreateGame(string name, string connectionId)
        {
            var user = _users.FirstOrDefault(u => u.ConnectionId == connectionId);

            while (_currentGames.Any(g => g.Name == name))
            {
                Random rnd = new Random();
                name += rnd.Next(1, 100);
            }

            var player = new Player(user);

            var game = new Game {
                Name    = name,
                Players = new List <Player> {
                    player
                },
                GameStarted = false,
                Deck        = DeckCreator.CreateCards(),
                Creator     = player
            };

            _currentGames.Add(game);

            return(true);
        }
        public static void SimDistribution()
        {
            Deck          deck    = DeckCreator.CreateCards();
            List <Player> players = new List <Player>();

            for (int i = 0; i < 1; ++i)
            {
                players.Add(new Player("P" + i.ToString()));
                players[i].Cards = new List <Card>();
            }

            for (int i = 0; i < s_nTimesPerSim; ++i)
            {
                InitGame(deck, players);
                deck.Cards = DeckCreator.Shuffle(deck.Cards);
                Play(deck, players[0]);

                if (i % 10000 == 0)
                {
                    System.Diagnostics.Debug.WriteLine("Progress: Player: {0} Time: {1} Hand: {2}", players[0].Display, i, players[0].Cards[0].DisplayCard);
                }

                if (HandRank.StraightFlush == players[0].Hand.HandRank)
                {
                    System.Diagnostics.Debug.WriteLine("End: Player: {0} Time: {1} Hand: {2}", players[0].Display, i, players[0].Hand.HandRank);
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        public static void SimFiveCardsFini()
        {
            Deck          deck    = DeckCreator.CreateCards();
            List <Player> players = new List <Player>();

            for (int i = 0; i < 4; ++i)
            {
                players.Add(new Player("P" + i.ToString()));
                players[i].Cards = new List <Card>();
            }

            Card[]      arAxis  = deck.Cards.ToArray();
            List <Card> lstAxis = new List <Card>(arAxis);

            lstAxis.Sort(Comparer <Card> .Create((x, y) =>
                                                 (x.Rank < y.Rank || (x.Rank == y.Rank && x.Suit < y.Suit)) ? 1 :
                                                 (x.Rank > y.Rank || (x.Rank == y.Rank && x.Suit > y.Suit)) ? -1 : 0));
            arAxis = lstAxis.ToArray();

            string[] arHandRankNames = System.Enum.GetNames(typeof(HandRank));

            double[][] arReport = new double[arHandRankNames.Length][];
            for (int i = 0; i < arHandRankNames.Length; ++i)
            {
                arReport[i] = new double[arAxis.Length];
                for (int j = 0; j < arReport[i].Length; ++j)
                {
                    arReport[i][j] = 0;
                }
            }

            for (int i = 0; i < s_nTimesPerSim; ++i)
            {
                InitGame(deck, players);
                deck.Cards = DeckCreator.Shuffle(deck.Cards);
                Game   game   = new Game(deck, players);
                Result result = game.Play();
                int    nAxis  = System.Array.IndexOf(arAxis, result.Winner.Hand.SortedCards[0]);
                ++arReport[(int)result.Winner.Hand.HandRank][nAxis];

                if (i % 1000000 == 0)
                {
                    System.Diagnostics.Debug.WriteLine("Progress: Player: {0} Time: {1}", players[0].Display, i);
                }
            }

            for (int i = 0; i < arReport.Length; ++i)
            {
                for (int j = 0; j < arReport[i].Length; ++j)
                {
                    arReport[i][j] /= s_nTimesPerSim;
                }
            }

            PrintReport(arHandRankNames, arAxis, arReport);
        }
        public void New_Deck_Should_Contain_40Cards()
        {
            //Arrange
            var deckCreator = new DeckCreator();

            //Act
            var cards = deckCreator.CreateCards();

            //Assert
            var       actualNumberOfCards   = cards.Count;
            const int expectedNumberOfCards = 40;

            Assert.AreEqual(expectedNumberOfCards, actualNumberOfCards);
        }
Ejemplo n.º 5
0
        public void DiscardPileShuffledIntoDrawPile()
        {
            //Arrange
            var deckCreator = new DeckCreator();
            var player      = new Player("Player 1")
            {
                Draw    = new Queue <Card>(),
                Discard = deckCreator.CreateCards()
            };

            //Act
            var discardCardPile = deckCreator.RefillPlayerDeck(player);

            //Assert
            Assert.IsTrue(discardCardPile.Draw.Count > 0);
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            PokerGame.Simulator.SimGames.SimTwoCardsInit();
            //PokerGame.Simulator.SimGamesFini.SimFiveCardsFini();
            //PokerGame.Simulator.SimGamesDistribution.SimDistribution();

            Console.WriteLine("Hello World");
            Deck          deck    = DeckCreator.CreateCards();
            List <Player> players = new List <Player>();

            players.Add(new Player("P1"));
            players.Add(new Player("P2"));
            players.Add(new Player("P3"));
            players.Add(new Player("P4"));
            Game game = new Game(deck, players);

            Result result = game.Play();

            //System.Diagnostics.Debug.WriteLine("Winer: {0} Hand: {1}", result.Winner.Name, result.Winner.Display);
            Console.WriteLine("Winer: {0} Hand: {1}", result.Winner.Name, result.Winner.Display);
        }
Ejemplo n.º 7
0
        public void NextRound()
        {
            RoundFinished = false;
            Round++;
            StartPlayer++;
            if (StartPlayer >= Players.Count)
            {
                StartPlayer = 0;
            }
            Deck = DeckCreator.CreateCards();

            // if the player is the only one who dropped his cards, he gets -50 points
            var playerWhoWonRound            = Players.FirstOrDefault(p => p.Deck.Count == 0);
            var anotherPlayerHasDroppedCards = Players.Where(player => player.User.ConnectionId != playerWhoWonRound.User.ConnectionId).Any(p => p.HasDroppedCards);

            if (!anotherPlayerHasDroppedCards)
            {
                playerWhoWonRound.Points -= 50;
            }

            foreach (var player in Players)
            {
                foreach (var card in player.Deck)
                {
                    player.Points += card.Value;
                }

                player.DroppedCards.Clear();

                if (Round == 2)
                {
                    player.DroppedCards = new List <List <Card> >
                    {
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        }
                    };
                    player.Deck = Deck.GetAndRemove(0, 10);
                }
                if (Round == 3)
                {
                    player.DroppedCards = new List <List <Card> >
                    {
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        }
                    };
                    player.Deck = Deck.GetAndRemove(0, 10);
                }
                if (Round == 4)
                {
                    player.DroppedCards = new List <List <Card> >
                    {
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        }
                    };
                    player.Deck = Deck.GetAndRemove(0, 10);
                }
                if (Round == 5)
                {
                    player.DroppedCards = new List <List <Card> >
                    {
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        }
                    };
                    player.Deck = Deck.GetAndRemove(0, 10);
                }
                if (Round == 6)
                {
                    player.DroppedCards = new List <List <Card> >
                    {
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        }
                    };
                    player.Deck = Deck.GetAndRemove(0, 11);
                }
                if (Round == 7)
                {
                    player.DroppedCards = new List <List <Card> >
                    {
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        }
                    };
                    player.Deck = Deck.GetAndRemove(0, 12);
                }
                if (Round == 8)
                {
                    player.DroppedCards = new List <List <Card> >
                    {
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        },
                        new List <Card>
                        {
                            new Card("empty")
                        }
                    };
                    player.Deck = Deck.GetAndRemove(0, 13);
                }

                for (int i = 0; i < player.Deck.Count; i++)
                {
                    player.Deck[i].Index = i;
                }

                player.Turn = false;
                player.CurrentAllowedMove    = MoveType.DrawCard;
                player.PlayerAskedToKeepCard = false;
                player.FeedbackOnKeepingCard = null;
                player.PlayerKnocked         = false;
                player.FeedbackOnKnock       = null;
                player.HasDroppedCards       = false;
            }

            Players[StartPlayer].Turn = true;
            DiscardPile = new List <Card>
            {
                new Card("empty")
            };
            AskToKeepCardPile = new List <Card>
            {
                new Card("empty")
            };
            DiscardPile.AddRange(Deck.GetAndRemove(0, 1));
        }
Ejemplo n.º 8
0
        public static void SimTwoCardsInit()
        {
            Deck          deck    = DeckCreator.CreateCards();
            List <Player> players = new List <Player>();

            for (int i = 0; i < 4; ++i)
            {
                players.Add(new Player("P" + i.ToString()));
                players[i].Cards = new List <Card>();
            }

            Card[]      arAxis  = deck.Cards.ToArray();
            List <Card> lstAxis = new List <Card>(arAxis);

            lstAxis.Sort(Comparer <Card> .Create((x, y) =>
                                                 (x.Rank < y.Rank || (x.Rank == y.Rank && x.Suit < y.Suit)) ? 1 :
                                                 (x.Rank > y.Rank || (x.Rank == y.Rank && x.Suit > y.Suit)) ? -1 : 0));
            arAxis = lstAxis.ToArray();

            double[][] arReport = new double[arAxis.Length][];
            for (int i = 0; i < arReport.Length; ++i)
            {
                arReport[i] = new double[arAxis.Length];
                for (int j = 0; j < arReport[i].Length; ++j)
                {
                    arReport[i][j] = 0;
                }
            }

            for (int i = 0; i < arAxis.Length - 1; ++i)
            {
                for (int j = i + 1; j < arAxis.Length; ++j)
                {
                    InitGame(deck, players);

                    Card card = deck.HaveACardUpMySleeve(arAxis[i].Rank, arAxis[i].Suit);
                    players[0].AddCard(card);
                    card = deck.HaveACardUpMySleeve(arAxis[j].Rank, arAxis[j].Suit);
                    players[0].AddCard(card);

                    deck.Save();
                    for (int k = 0; k < players.Count; ++k)
                    {
                        players[k].Save();
                    }

                    int nWin = 0;
                    for (int k = 0; k < s_nTimesPerSim; ++k)
                    {
                        deck.Load();
                        deck.Cards = DeckCreator.Shuffle(deck.Cards);
                        for (int m = 0; m < players.Count; ++m)
                        {
                            players[m].Load();
                        }

                        Game   game   = new Game(deck, players);
                        Result result = game.Play();

                        if (result.Winner == players[0])
                        {
                            ++nWin;
                        }
                    }

                    players[0].Load();
                    arReport[i][j] = (double)nWin / s_nTimesPerSim;
                    System.Diagnostics.Debug.WriteLine("Progress: Player: {0} Win: {1}", players[0].Display, (double)nWin / s_nTimesPerSim);
                }
            }

            PrintReport(arAxis, arReport);
        }