Esempio n. 1
0
        public void IsLeadingCardsValidTestTractor2()
        {
            Dictionary <string, CurrentPoker> playerHoldingCards = new Dictionary <string, CurrentPoker>();

            playerHoldingCards["p2"] = new CurrentPoker(new List <int>()
            {
                8, 8, 7, 7
            }, Suit.Spade, 0);
            List <int> selectedCards = new List <int> {
                3, 3, 4, 4, 5, 5, 12
            };
            string player = "p1";

            ShowingCardsValidationResult result = TractorRules.IsLeadingCardsValid(playerHoldingCards, selectedCards, player);

            Assert.AreEqual(ShowingCardsValidationResultType.DumpingSuccess, result.ResultType);

            selectedCards = new List <int> {
                3, 3, 4, 4, 5, 5
            };
            player = "p1";

            result = TractorRules.IsLeadingCardsValid(playerHoldingCards, selectedCards, player);
            Assert.AreEqual(ShowingCardsValidationResultType.Valid, result.ResultType);
        }
Esempio n. 2
0
        public void IsLeadingCardsValidDumpSingles()
        {
            Dictionary <string, CurrentPoker> playerHoldingCards = new Dictionary <string, CurrentPoker>();

            playerHoldingCards["p2"] = new CurrentPoker(new List <int>()
            {
                35, 7, 9, 22, 52
            }, Suit.Spade, 0);
            playerHoldingCards["p3"] = new CurrentPoker(new List <int>()
            {
                30, 35, 36, 9, 10
            }, Suit.Spade, 0);
            playerHoldingCards["p4"] = new CurrentPoker(new List <int>()
            {
                27, 31, 37, 36, 16
            }, Suit.Spade, 0);
            List <int> selectedCards = new List <int> {
                42, 45, 47, 48, 49
            };
            string player = "p1";

            ShowingCardsValidationResult result = TractorRules.IsLeadingCardsValid(playerHoldingCards, selectedCards, player);

            Assert.AreEqual(ShowingCardsValidationResultType.DumpingSuccess, result.ResultType);
        }
Esempio n. 3
0
        public ShowingCardsValidationResult ValidateDumpingCards(List <int> selectedCards, string playerId)
        {
            var result = TractorRules.IsLeadingCardsValid(this.CurrentHandState.PlayerHoldingCards, selectedCards,
                                                          playerId);

            result.PlayerId = playerId;

            if (result.ResultType == ShowingCardsValidationResultType.DumpingFail)
            {
                foreach (var player in PlayersProxy)
                {
                    if (player.Key != playerId)
                    {
                        player.Value.NotifyDumpingValidationResult(result);
                    }
                }
            }
            var cardString = "";

            foreach (var card in selectedCards)
            {
                cardString += card.ToString() + " ";
            }
            log.Debug(playerId + " tried to dump cards: " + cardString + " Result: " + result.ResultType.ToString());
            return(result);
        }
Esempio n. 4
0
        public void RestartGame()
        {
            log.Debug("restart game");
            foreach (var player in CurrentGameState.Players)
            {
                player.Rank = 0;
            }

            this.CurrentHandState = new CurrentHandState(this.CurrentGameState);
            this.CurrentHandState.LeftCardsCount = TractorRules.GetCardNumberofEachPlayer(this.CurrentGameState.Players.Count);
            CurrentHandState.IsFirstHand         = true;
            UpdatePlayersCurrentHandState();
            var currentHandId = this.CurrentHandState.Id;

            DistributeCards();
            if (this.CurrentHandState.Id != currentHandId)
            {
                return;
            }
            Thread.Sleep(5000);
            if (this.CurrentHandState.Trump != Suit.None)
            {
                DistributeLast8Cards();
            }
            else if (PlayersProxy.Count == 4)
            {
                RestartGame();
            }
        }
Esempio n. 5
0
        public void IsLeadingCardsValidTestTractor()
        {
            Dictionary <string, CurrentPoker> playerHoldingCards = new Dictionary <string, CurrentPoker>();

            playerHoldingCards["p2"] = new CurrentPoker(new List <int>()
            {
                8, 8, 7, 7
            }, Suit.Spade, 0);
            List <int> selectedCards = new List <int> {
                4, 4, 5, 5, 12
            };
            string player = "p1";

            ShowingCardsValidationResult result = TractorRules.IsLeadingCardsValid(playerHoldingCards, selectedCards, player);

            Assert.AreEqual(ShowingCardsValidationResultType.DumpingFail, result.ResultType);
            Assert.AreEqual(4, result.MustShowCardsForDumpingFail.Count);
            Assert.IsTrue(result.MustShowCardsForDumpingFail.Contains(4));
            Assert.IsTrue(result.MustShowCardsForDumpingFail.Contains(5));

            selectedCards = new List <int> {
                12, 10, 10, 9, 9
            };
            result = TractorRules.IsLeadingCardsValid(playerHoldingCards, selectedCards, player);
            Assert.AreEqual(ShowingCardsValidationResultType.DumpingSuccess, result.ResultType);
        }
Esempio n. 6
0
        public void IsValidTest2()
        {
            CurrentTrickState currentTrickState = new CurrentTrickState {
                Trump = Suit.Heart, Rank = 0, Learder = "p1"
            };

            currentTrickState.ShowedCards.Add("p1", new List <int> {
                39, 39, 0, 0
            });
            currentTrickState.ShowedCards.Add("p2", new List <int> {
            });
            currentTrickState.ShowedCards.Add("p3", new List <int> {
            });
            currentTrickState.ShowedCards.Add("p4", new List <int> {
            });
            List <int> selectedCards = new List <int> {
                1, 2, 5, 5
            };
            CurrentPoker currentCards = new CurrentPoker(new int[] { 1, 2, 3, 4, 5, 5 }, Suit.Heart, 0);
            ShowingCardsValidationResultType expected = ShowingCardsValidationResultType.Valid;
            ShowingCardsValidationResultType actual;

            actual = TractorRules.IsValid(currentTrickState, selectedCards, currentCards).ResultType;
            Assert.AreEqual(expected, actual);
        }
Esempio n. 7
0
        private void ToShowCards()
        {
            var      pigRect = new Rectangle(296, 300, 53, 46);
            Graphics g       = Graphics.FromImage(bmp);

            if (ThisPlayer.CurrentHandState.CurrentHandStep == HandStep.Playing &&
                ThisPlayer.CurrentTrickState.NextPlayer() == ThisPlayer.PlayerId)
            {
                ShowingCardsValidationResult showingCardsValidationResult =
                    TractorRules.IsValid(ThisPlayer.CurrentTrickState, SelectedCards, ThisPlayer.CurrentPoker);
                //如果我准备出的牌合法
                if (showingCardsValidationResult.ResultType == ShowingCardsValidationResultType.Valid)
                {
                    //擦去小猪
                    g.DrawImage(image, pigRect, pigRect, GraphicsUnit.Pixel);

                    foreach (int card in SelectedCards)
                    {
                        ThisPlayer.CurrentPoker.RemoveCard(card);
                    }
                    ThisPlayer.ShowCards(SelectedCards);
                    drawingFormHelper.DrawMyShowedCards();
                    SelectedCards.Clear();
                }
                else if (showingCardsValidationResult.ResultType == ShowingCardsValidationResultType.TryToDump)
                {
                    //擦去小猪
                    g.DrawImage(image, pigRect, pigRect, GraphicsUnit.Pixel);

                    ShowingCardsValidationResult result = ThisPlayer.ValidateDumpingCards(SelectedCards);
                    if (result.ResultType == ShowingCardsValidationResultType.DumpingSuccess) //甩牌成功.
                    {
                        foreach (int card in SelectedCards)
                        {
                            ThisPlayer.CurrentPoker.RemoveCard(card);
                        }
                        ThisPlayer.ShowCards(SelectedCards);

                        drawingFormHelper.DrawMyShowedCards();
                        SelectedCards.Clear();
                    }
                    //甩牌失败
                    else
                    {
                        foreach (int card in result.MustShowCardsForDumpingFail)
                        {
                            ThisPlayer.CurrentPoker.RemoveCard(card);
                        }
                        Thread.Sleep(2000);
                        ThisPlayer.ShowCards(result.MustShowCardsForDumpingFail);

                        SelectedCards = result.MustShowCardsForDumpingFail;
                        SelectedCards.Clear();
                    }
                }
            }
            g.Dispose();
        }
Esempio n. 8
0
        //发牌
        public void DistributeCards()
        {
            CurrentHandState.CurrentHandStep = HandStep.DistributingCards;
            UpdatePlayersCurrentHandState();
            string currentHandId = this.CurrentHandState.Id;

            this.CardsShoe.Shuffle();
            int cardNumberofEachPlayer = TractorRules.GetCardNumberofEachPlayer(this.CurrentGameState.Players.Count);
            int j = 0;

            var LogList = new Dictionary <string, StringBuilder>();

            foreach (var player in PlayersProxy)
            {
                LogList[player.Key] = new StringBuilder();
            }

            for (int i = 0; i < cardNumberofEachPlayer; i++)
            {
                foreach (var player in PlayersProxy)
                {
                    var index = j++;
                    if (this.CurrentHandState.Id == currentHandId)
                    {
                        player.Value.GetDistributedCard(CardsShoe.Cards[index]);
                        LogList[player.Key].Append(CardsShoe.Cards[index].ToString() + ", ");
                    }
                    else
                    {
                        return;
                    }

                    this.CurrentHandState.PlayerHoldingCards[player.Key].AddCard(CardsShoe.Cards[index]);
                }
                Thread.Sleep(500);
            }

            log.Debug("distribute cards to each player: ");
            foreach (var logItem in LogList)
            {
                log.Debug(logItem.Key + ": " + logItem.Value.ToString());
            }

            foreach (var keyvalue in this.CurrentHandState.PlayerHoldingCards)
            {
                log.Debug(keyvalue.Key + "'s cards:  " + keyvalue.Value.ToString());
            }

            CurrentHandState.CurrentHandStep = HandStep.DistributingCardsFinished;
            UpdatePlayersCurrentHandState();
        }
Esempio n. 9
0
        public void StartNextHand(PlayerEntity nextStarter)
        {
            UpdateGameState();
            this.CurrentHandState                = new CurrentHandState(this.CurrentGameState);
            this.CurrentHandState.Starter        = nextStarter.PlayerId;
            this.CurrentHandState.Rank           = nextStarter.Rank;
            this.CurrentHandState.LeftCardsCount = TractorRules.GetCardNumberofEachPlayer(this.CurrentGameState.Players.Count);

            log.Debug("start next hand, starter: " + this.CurrentHandState.Starter + " Rank: " + this.CurrentHandState.Rank.ToString());

            UpdatePlayersCurrentHandState();

            var currentHandId = this.CurrentHandState.Id;

            DistributeCards();
            if (this.CurrentHandState.Id != currentHandId)
            {
                return;
            }

            Thread.Sleep(5000);
            if (this.CurrentHandState.Trump != Suit.None)
            {
                DistributeLast8Cards();
            }

            else if (PlayersProxy.Count == 4)
            {
                //如果庄家TEAM亮不起,则庄家的下家成为新的庄家
                var nextStarter2 = CurrentGameState.GetNextPlayerAfterThePlayer(false, CurrentHandState.Starter);
                this.CurrentHandState.Starter = nextStarter2.PlayerId;
                this.CurrentHandState.Rank    = nextStarter2.Rank;
                log.Debug("starter team fail to make trump, next starter: " + this.CurrentHandState.Starter + " Rank: " + this.CurrentHandState.Rank.ToString());

                UpdatePlayersCurrentHandState();

                //10 seconds to make trump
                Thread.Sleep(10000);
                if (this.CurrentHandState.Trump != Suit.None)
                {
                    DistributeLast8Cards();
                }
                else if (PlayersProxy.Count == 4)
                {
                    //如果下家也亮不起,重新发牌
                    StartNextHand(nextStarter);
                }
            }
        }
Esempio n. 10
0
        public void IsLeadingCardsValidDump2Pairs()
        {
            Dictionary <string, CurrentPoker> playerHoldingCards = new Dictionary <string, CurrentPoker>();

            playerHoldingCards["p2"] = new CurrentPoker(new List <int>()
            {
                18, 15, 22, 19, 21
            }, Suit.Spade, 0);
            playerHoldingCards["p3"] = new CurrentPoker(new List <int>()
            {
                25, 25, 24, 24, 22
            }, Suit.Spade, 0);
            List <int> selectedCards = new List <int> {
                10, 10, 6, 6
            };
            string player = "p1";

            ShowingCardsValidationResult result = TractorRules.IsLeadingCardsValid(playerHoldingCards, selectedCards, player);

            Assert.AreEqual(ShowingCardsValidationResultType.DumpingSuccess, result.ResultType);
        }
Esempio n. 11
0
        //player get card
        public void GetDistributedCard(int number)
        {
            this.CurrentPoker.AddCard(number);
            if (PlayerOnGetCard != null)
            {
                PlayerOnGetCard(number);
            }

            if (this.CurrentPoker.Count == TractorRules.GetCardNumberofEachPlayer(this.CurrentGameState.Players.Count) && this.PlayerId != this.CurrentHandState.Last8Holder)
            {
                if (AllCardsGot != null)
                {
                    AllCardsGot();
                }
            }
            else if (this.CurrentPoker.Count == TractorRules.GetCardNumberofEachPlayer(this.CurrentGameState.Players.Count) + 8)
            {
                if (AllCardsGot != null)
                {
                    AllCardsGot();
                }
            }
        }
Esempio n. 12
0
        public void GetWinnerTractor3()
        {
            CurrentTrickState trickState = new CurrentTrickState();

            trickState.Trump             = Suit.Club;
            trickState.Rank              = 0;
            trickState.Learder           = "p1";
            trickState.ShowedCards["p1"] = new List <int> {
                1, 1, 2, 2, 3, 3
            };
            trickState.ShowedCards["p2"] = new List <int> {
                40, 40, 45, 45, 48, 48
            };
            trickState.ShowedCards["p3"] = new List <int> {
                8, 8, 10, 10, 11, 11
            };
            trickState.ShowedCards["p4"] = new List <int> {
                53, 53, 52, 52, 25, 25
            };

            string actual = TractorRules.GetWinner(trickState);

            Assert.AreEqual("p1", actual);
        }
Esempio n. 13
0
        public void GetWinnerDumpSingle3()
        {
            CurrentTrickState trickState = new CurrentTrickState();

            trickState.Trump             = Suit.Club;
            trickState.Rank              = 0;
            trickState.Learder           = "p1";
            trickState.ShowedCards["p1"] = new List <int> {
                14, 15, 16, 17
            };
            trickState.ShowedCards["p2"] = new List <int> {
                40, 41, 42, 13
            };
            trickState.ShowedCards["p3"] = new List <int> {
                40, 41, 42, 39
            };
            trickState.ShowedCards["p4"] = new List <int> {
                44, 45, 46, 47
            };

            string actual = TractorRules.GetWinner(trickState);

            Assert.AreEqual("p3", actual);
        }
Esempio n. 14
0
        public void GetWinnerDumpPair3()
        {
            CurrentTrickState trickState = new CurrentTrickState();

            trickState.Trump             = Suit.Club;
            trickState.Rank              = 0;
            trickState.Learder           = "p1";
            trickState.ShowedCards["p1"] = new List <int> {
                1, 3, 3, 5, 5
            };
            trickState.ShowedCards["p2"] = new List <int> {
                40, 40, 53, 53, 50
            };
            trickState.ShowedCards["p3"] = new List <int> {
                18, 18, 20, 20, 21
            };
            trickState.ShowedCards["p4"] = new List <int> {
                49, 49, 39, 39, 50
            };

            string actual = TractorRules.GetWinner(trickState);

            Assert.AreEqual("p2", actual);
        }
Esempio n. 15
0
        public void GetWinnerDumpTractor2()
        {
            CurrentTrickState trickState = new CurrentTrickState();

            trickState.Trump             = Suit.Club;
            trickState.Rank              = 0;
            trickState.Learder           = "p1";
            trickState.ShowedCards["p1"] = new List <int> {
                12, 3, 3, 4, 4
            };
            trickState.ShowedCards["p2"] = new List <int> {
                40, 40, 45, 45, 13
            };
            trickState.ShowedCards["p3"] = new List <int> {
                19, 19, 20, 20, 21
            };
            trickState.ShowedCards["p4"] = new List <int> {
                12, 9, 9, 11, 11
            };

            string actual = TractorRules.GetWinner(trickState);

            Assert.AreEqual("p1", actual);
        }
Esempio n. 16
0
        public void GetWinnerSingleTrump()
        {
            CurrentTrickState trickState = new CurrentTrickState();

            trickState.Trump             = Suit.Club;
            trickState.Rank              = 0;
            trickState.Learder           = "p1";
            trickState.ShowedCards["p1"] = new List <int> {
                1
            };
            trickState.ShowedCards["p2"] = new List <int> {
                40
            };
            trickState.ShowedCards["p3"] = new List <int> {
                3
            };
            trickState.ShowedCards["p4"] = new List <int> {
                12
            };

            string actual = TractorRules.GetWinner(trickState);

            Assert.AreEqual("p2", actual);
        }
Esempio n. 17
0
        public void GetWinnerDumpSingle2()
        {
            CurrentTrickState trickState = new CurrentTrickState();

            trickState.Trump             = Suit.Club;
            trickState.Rank              = 0;
            trickState.Learder           = "p1";
            trickState.ShowedCards["p1"] = new List <int> {
                52, 53
            };
            trickState.ShowedCards["p2"] = new List <int> {
                26, 39
            };
            trickState.ShowedCards["p3"] = new List <int> {
                51, 39
            };
            trickState.ShowedCards["p4"] = new List <int> {
                25, 1
            };

            string actual = TractorRules.GetWinner(trickState);

            Assert.AreEqual("p1", actual);
        }
Esempio n. 18
0
        public void PlayerShowCards(CurrentTrickState currentTrickState)
        {
            string lastestPlayer = currentTrickState.LatestPlayerShowedCard();

            if (PlayersProxy[lastestPlayer] != null)
            {
                this.CurrentTrickState.ShowedCards[lastestPlayer] = currentTrickState.ShowedCards[lastestPlayer];
                string cardsString = "";
                foreach (var card in this.CurrentTrickState.ShowedCards[lastestPlayer])
                {
                    cardsString += card.ToString() + " ";
                }
                log.Debug("Player " + lastestPlayer + " showed cards: " + cardsString);
                //更新每个用户手中的牌在SERVER
                foreach (int card in this.CurrentTrickState.ShowedCards[lastestPlayer])
                {
                    this.CurrentHandState.PlayerHoldingCards[lastestPlayer].RemoveCard(card);
                }
                //回合结束
                if (this.CurrentTrickState.AllPlayedShowedCards())
                {
                    this.CurrentTrickState.Winner = TractorRules.GetWinner(this.CurrentTrickState);
                    if (!string.IsNullOrEmpty(this.CurrentTrickState.Winner))
                    {
                        if (
                            !this.CurrentGameState.ArePlayersInSameTeam(CurrentHandState.Starter,
                                                                        this.CurrentTrickState.Winner))
                        {
                            CurrentHandState.Score += currentTrickState.Points;
                            UpdatePlayersCurrentHandState();
                        }


                        log.Debug("Winner: " + this.CurrentTrickState.Winner);
                    }

                    UpdatePlayerCurrentTrickState();

                    CurrentHandState.LeftCardsCount -= currentTrickState.ShowedCards[lastestPlayer].Count;

                    //开始新的回合
                    if (this.CurrentHandState.LeftCardsCount > 0)
                    {
                        BeginNewTrick(this.CurrentTrickState.Winner);
                    }
                    else //所有牌都出完了
                    {
                        //扣底
                        CalculatePointsFromDiscarded8Cards();
                        Thread.Sleep(2000);
                        this.CurrentHandState.CurrentHandStep = HandStep.Ending;
                        UpdatePlayersCurrentHandState();

                        Thread.Sleep(5000);
                        var starter = this.CurrentGameState.NextRank(this.CurrentHandState, this.CurrentTrickState);

                        Thread.Sleep(5000);
                        StartNextHand(starter);
                    }
                }
                else
                {
                    UpdatePlayerCurrentTrickState();
                }
            }
        }