user1491801
user1491801

Reputation:

asp.net C# Poker game

Hello I am working on a poker game that is on my website designed in asp.net. As of right now, I have a lot working properly such as: when I hit a pair, 3 of a kind, flush, 4 of a king, and royal flush. The computer knows when those are hit. I am having trouble with getting it to understand when I have a two pair, which it just counts it as one pair. I have tried a few different ways. Here is a copy of my code

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;
                }
            }
        }
    }

Just trying to get it understand when you have two pair in your hard rather than just one pair. Thanks any help would be appreciated.

Upvotes: 0

Views: 2450

Answers (1)

Bert
Bert

Reputation: 82499

I think this is really an example of choosing the correct data structure. I can't see from your question what a PokerCard is, but it looks as if you're writing a lot of code, where, if you changed your data structure and used a little Linq, you would have a much easier time. For example if you defined your cards in this way:

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; }
}

then you could write some code similar to this that would allow you to determine if your hands met any poker hand criteria.

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;
        }
    }
}

To put it all together, I built a dotnetfiddle for you. Obviously, there would be some work to integrate this into whatever application you are working on, but this get's you pretty far along the path.

Upvotes: 2

Related Questions