asp.net c#扑克游戏

本文关键字:游戏 扑克 net asp | 更新日期: 2023-09-27 18:02:26

你好,我正在工作的一个扑克游戏,是在我的网站上设计的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;
                }
            }
        }
    }

试着让它明白当你有两双鞋而不是只有一双。非常感谢。

asp.net c#扑克游戏

我认为这确实是选择正确数据结构的一个例子。从你的问题中我看不出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;
        }
    }
}

把这一切放在一起,我为你建立了一个。显然,要将其集成到您正在开发的任何应用程序中都需要做一些工作,但这将使您在这条道路上走得很远。