asp.net C#扑克游戏

时间:2016-08-14 18:58:10

标签: c# asp.net

您好我正在开发一个在我的网站上设计的扑克游戏。截至目前,我有很多正常工作,例如:当我击中一对,3对,同花,4对国王和皇家同花顺。计算机知道什么时候被击中。当我有两对时,我很难理解它,它只计算一对。我尝试了几种不同的方法。 这是我的代码的副本

string[] PokerCard = new String[5];

            PokerCard[0] = PokerCard1.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/BackCards/BackCard1.png");
            PokerCard[1] = PokerCard2.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/BackCards/BackCard2.png");
            PokerCard[2] = PokerCard3.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/BackCards/BackCard3.png");
            PokerCard[3] = PokerCard4.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/BackCards/BackCard4.png");
            PokerCard[4] = PokerCard5.ImageUrl = Path.Combine("~/GameStyles/VideoPoker/Images/BackCards/BackCard5.png");    

  public void Pair()
    {
        for (int i = 0; i < 4; i++)
        {
            for (int k = 0; k < 5; k++)
            {
                if (PokerCard[k] == two[i])
                {
                    PokerTwo++;
                }
                if (PokerTwo == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == three[i])
                {
                    PokerThree++;
                }
                if (PokerThree == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == four[i])
                {
                    PokerFour++;
                }
                if (PokerFour == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == five[i])
                {
                    PokerFive++;
                }
                if (PokerFive == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == six[i])
                {
                    PokerSix++;
                }
                if (PokerSix == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == seven[i])
                {
                    PokerSeven++;
                }
                if (PokerSeven == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == eight[i])
                {
                    PokerEight++;
                }
                if (PokerEight == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == nine[i])
                {
                    PokerNine++;
                }
                if (PokerNine == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == ten[i])
                {
                    PokerTen++;
                }
                if (PokerTen == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == jack[i])
                {
                    PokerJack++;
                }
                if (PokerJack == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == queen[i])
                {
                    PokerQueen++;
                }
                if (PokerQueen == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == king[i])
                {
                    PokerKing++;
                }
                if (PokerKing == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
                if (PokerCard[k] == ace[i])
                {
                    PokerAce++;
                }
                if (PokerAce == 2)
                {
                    cash = cash + 10;
                    winnings = winnings + 10;
                    Cash.Text = Convert.ToString(cash);
                    Winnings.Text = Convert.ToString(winnings);
                    PairWin.BackColor = Color.Red;
                    Winner.Visible = true;
                    return;
                }
            }
        }
    }

当你在坚硬而不是仅仅一对中有两对时,试着理解它。谢谢任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

我认为这是选择正确数据结构的一个例子。我无法从您的问题中看到PokerCard是什么,但看起来好像您正在编写大量代码,如果您更改了数据结构并使用了一点Linq,那么会有一个更容易的时间。例如,如果您以这种方式定义了卡片:

public enum Suit
{
    Clubs,
    Diamonds,
    Hearts,
    Spades
}

public enum Value
{
    Ace,
    Two,
    Three,
    Four,
    Five,
    Six,
    Seven,
    Eight,
    Nine,
    Ten,
    Jack,
    Queen,
    King
}

public class Card
{
    public Suit Suit { get; set; }
    public Value Value { get; set; }
}

然后你可以编写一些与此类似的代码,以便你确定你的手是否符合任何扑克手牌标准。

public class Hand
{
    public IEnumerable<Card> Cards {get;set;}

    public bool Contains(Value val)
    {
        return Cards.Where(c => c.Value == val).Any();
    }

    public bool IsPair
    {
        get
        {
            return Cards.GroupBy(h => h.Value)
                       .Where(g => g.Count() == 2)
                       .Count() == 1;
        }
    }

    public bool IsTwoPair
    {
        get
        {
            return Cards.GroupBy(h => h.Value)
                        .Where(g => g.Count() == 2)
                        .Count() == 2;
        }
    }

    public bool IsThreeOfAKind
    {
        get
        {
            return Cards.GroupBy(h => h.Value)
                        .Where(g => g.Count() == 3)
                        .Any();
        }
    }

    public bool IsFourOfAKind
    {
        get
        {
            return Cards.GroupBy(h => h.Value)
                        .Where(g => g.Count() == 4)
                        .Any();
        }
    }

    public bool IsFlush
    {
        get
        {
            return Cards.GroupBy(h => h.Suit).Count() == 1;
        }
    }

    public bool IsFullHouse
    {
        get
        {
            return IsPair && IsThreeOfAKind;
        }
    }

    public bool IsStraight
    {
        get
        {
            // If there is an Ace, we have to handle the 10,J,Q,K,A case, which isn't handled by the code
            // below because Ace is normally 0
            if (Contains(Value.Ace) &&
                Contains(Value.King) &&
                Contains(Value.Queen) &&
                Contains(Value.Jack) &&
                Contains(Value.Ten))
            {
                return true;
            }

            var ordered = Cards.OrderBy(h => h.Value).ToArray();
            var straightStart = (int)ordered.First().Value;
            for (var i = 1; i < ordered.Length; i++)
            {
                if ((int)ordered[i].Value != straightStart + i)
                    return false;
            }

            return true;
        }

    }

    public bool IsStraightFlush
    {
        get
        {
            return IsStraight && IsFlush;
        }
    }

    public bool IsRoyalStraightFlush
    {
        get
        {
            return IsStraight && IsFlush && Contains(Value.Ace) && Contains(Value.King);
        }
    }
}

public class Deck
{
    public Deck()
    {
        var cards = new List<Card>();
        foreach (Suit suit in Enum.GetValues(typeof(Suit)))
        {
            foreach (Value value in Enum.GetValues(typeof(Value)))
            {
                cards.Add(new Card { Suit = suit, Value = value });
            }
        }
        _cards = cards;

    }

    List<Card> _cards;
    public List<Card> Cards { get {return _cards;}}

    public Hand DealStandardHand()
    {
        return new Hand { Cards = Cards.Take(5)};
    }

    private static Random rng = new Random();
    public void Shuffle()
    {
        int n = Cards.Count;
        while (n > 1)
        {
            n--;
            int k = rng.Next(n + 1);
            Card value = Cards[k];
            Cards[k] = Cards[n];
            Cards[n] = value;
        }
    }
}

总而言之,我为你建造了dotnetfiddle。显然,将有一些工作可以将它集成到您​​正在处理的任何应用程序中,但是这使您在路径上走得很远。