Elizabeth Fuenzalida Elizabeth Fuenzalida - 10 days ago 5
C# Question

Poker Game: Player and computer generating same hand

I'm creating a Pokergame where the player versus a computer.
I'm running into an issue though where:

the player and computer are generating the same hand/cards.

I'm assuming it's either a sorting issue or shuffling issue but can't seem to find where the problem is. Can anyone help?

Example of what I mean

enter image description here

Deal Class:

namespace PROG120_FINALPROJECT
{
class Deal : DeckofCards
{
private Card[] PlayerHand;
private Card[] ComputerHand;
private Card[] SortPHand; //sorted player hand
private Card[] SortCHand; // sorted computer hand


public Deal()
{
PlayerHand = new Card[5];
SortPHand = new Card[5];
ComputerHand = new Card[5];
SortCHand = new Card[5];
}

public void DealSetup()
{
SetDeck(); //create the deck of cards and shuffle them
GetHand();
sortCards();
evaluateHands();
}


public void GetHand()
{
//5 cards for the player
for (int i = 0; i < 5; i++)
PlayerHand[i] = GetDeck[i];


//5 cards for computer
for (int i = 0; i < 5; i++)
ComputerHand[i] = GetDeck[i];
}

public void sortCards()
{
var seePlayer = from hand in PlayerHand
orderby hand.CardValue
select hand;

var seeComputer = from hand in ComputerHand
orderby hand.CardValue
select hand;

var index = 0;
foreach (var element in seePlayer.ToList())
{
SortPHand[index] = element;
index++;
}

index = 0;
foreach (var element in seeComputer.ToList())
{
SortCHand[index] = element;
index++;
}
}
public void evaluateHands()
{
//create player's computer's evaluation objects (passing Sorted hand to constructor)
HandEvaluator playerHandEvaluator = new HandEvaluator(SortPHand);
HandEvaluator computerHandEvaluator = new HandEvaluator(SortCHand);

//get the player's and computer's handj
Hand playerHand = playerHandEvaluator.EvaluateHand();
Hand computerHand = computerHandEvaluator.EvaluateHand();

//display each hand
Console.WriteLine("\n\n\n\n\nPlayer's Hand: " + playerHand);
foreach (var card in PlayerHand)
{
Console.Write(card.CardValue.ToString());
Console.Write(" of ");
Console.Write(card.CardSuit.ToString());
Console.Write("\n");
}
Console.WriteLine("\n\n\n\n\nComputer's Hand: " + computerHand);

foreach (var card in PlayerHand)
{
Console.Write(card.CardValue.ToString());
Console.Write(" of ");
Console.Write(card.CardSuit.ToString());
Console.Write("\n");
}
//evaluate hands
if (playerHand > computerHand)
{
Console.WriteLine("Player WINS!");
}
else if (playerHand < computerHand)
{
Console.WriteLine("Computer WINS!");
}
else //if the hands are the same, evaluate the values
{
//first evaluate who has higher value of hand
if (playerHandEvaluator.HVs.Total > computerHandEvaluator.HVs.Total)
Console.WriteLine("Player WINS!");
else if (playerHandEvaluator.HVs.Total < computerHandEvaluator.HVs.Total)
Console.WriteLine("Computer WINS!");
//if both hanve the same poker hand
// player with the next higher card wins
else if (playerHandEvaluator.HVs.HighCard > computerHandEvaluator.HVs.HighCard)
Console.WriteLine("Player WINS!");
else if (playerHandEvaluator.HVs.HighCard < computerHandEvaluator.HVs.HighCard)
Console.WriteLine("Computer WINS!");
else
Console.WriteLine("Draw, no one wins!");
}
}
}
}


Class DeckofCards:

namespace PROG120_FINALPROJECT
{
//START CLASS DoC
class DeckofCards : Card
{
int AllCards = 52; //52 cards in a deck
private Card[] Deck; //array of playing cards

public DeckofCards()
{
Deck = new Card[AllCards];
}

public Card[] GetDeck //grab current deck
{
get
{ return Deck; }
}

public void SetDeck()
{
int i = 0;
foreach (SUIT s in Enum.GetValues(typeof(SUIT)))
{
foreach (VALUE v in Enum.GetValues(typeof(VALUE)))
{
Deck[i] = new Card { CardSuit = s, CardValue = v };
i++;
}
}
Shuffle();
}

//shuffle deck
public void Shuffle()
{
Random rand = new Random();
Card temp;

//run shuffle 100 times
for (int ShuffleAmount = 0; ShuffleAmount < 100; ShuffleAmount++)
{
for (int i = 0; i < AllCards; i++)
{
//swap cards
int SecondCardIndex = rand.Next(13);
temp = Deck[i];
Deck[i] = Deck[SecondCardIndex];
Deck[SecondCardIndex] = temp;
}
}
}
//END of CLASS DoC
}
}


I don't think you'll need these but just in case here are my other classes-

HandEval:

namespace PROG120_FINALPROJECT
{
public enum Hand
{
Nothing,
OnePair, //Jacks or Better
TwoPairs,
ToK, //Three of a kind
Str, //straight
Flush,
StrFlush, //straight flush
FullH, // Full House
FoK, // Four of a Kind
Royal, //Royal Flush
}

public struct HandValue
{
public int Total
{
get;
set;
}
public int HighCard
{
get;
set;
}
}

class HandEvaluator : Card
{
private int SpadeSum;
private int HeartSum;
private int DiamondSum;
private int ClubSum;
private Card[] cards;
private HandValue HV; //Hand Value

public HandEvaluator(Card[] SortedHand)
{
SpadeSum = 0;
HeartSum = 0;
DiamondSum = 0;
ClubSum = 0;
cards = new Card[5];
cards = SortedHand;
HV = new HandValue();
}

public HandValue HVs
{
get
{ return HV; }
set
{ HV = value; }
}

public Card [] Cards
{
get
{ return cards; }
set
{
cards[0] = value[0];
cards[1] = value[1];
cards[2] = value[2];
cards[3] = value[3];
cards[4] = value[4];
}
}

public Hand EvaluateHand()
{
//gets number of each suit on hand
getNumberofSuit();
if (Royal())
return Hand.Royal;
else if (FoK())
return Hand.FoK;
else if (FullH())
return Hand.FullH;
else if (StrFlush())
return Hand.StrFlush;
else if (Flush())
return Hand.Flush;
else if (Str())
return Hand.Str;
else if (ToK())
return Hand.ToK;
else if (TwoPairs())
return Hand.TwoPairs;
else if (OnePair())
return Hand.OnePair;

//if hand is nothing, player with highest card wins
HV.HighCard = (int)cards[4].CardValue;
return Hand.Nothing;
}

private void getNumberofSuit()
{
foreach (var element in Cards)
{
if (element.CardSuit == Card.SUIT.SPADE)
SpadeSum++;
else if (element.CardSuit == Card.SUIT.HEART)
HeartSum++;
else if (element.CardSuit == Card.SUIT.DIAMOND)
DiamondSum++;
else if (element.CardSuit == Card.SUIT.CLUB)
ClubSum++;
}
}

private bool Royal()
{
// a straight flush including ace, king, queen, jack, and ten all in the same suit.
//all suits are same & 5 consecutive values
if (SpadeSum == 5 || HeartSum == 5 || DiamondSum == 5 || ClubSum == 5 &&
cards[0].CardValue + 1 == cards[1].CardValue &&
cards[1].CardValue + 1 == cards[2].CardValue &&
cards[2].CardValue + 1 == cards[3].CardValue &&
cards[3].CardValue + 1 == cards[4].CardValue)
{
HV.Total = (int)cards[4].CardValue;
return true;
}

return false;
}

private bool FoK()
{
//if the first 4 cards, add values of the four cards and last card is the highest
if (cards[0].CardValue == cards[1].CardValue && cards[0].CardValue == cards[2].CardValue && cards[0].CardValue == cards[3].CardValue)
{
HV.Total = (int)cards[1].CardValue * 4;
HV.HighCard = (int)cards[4].CardValue;
return true;
}
else if (cards[1].CardValue == cards[2].CardValue && cards[1].CardValue == cards[3].CardValue && cards[1].CardValue == cards[4].CardValue)
{
HV.Total = (int)cards[1].CardValue * 4;
HV.HighCard = (int)cards[0].CardValue;
return true;
}

return false;
}

private bool FullH()
{
//the first three cards and last two cards are of the same value
//first two cards, and last three cards are of the same value
if ((cards[0].CardValue == cards[1].CardValue && cards[0].CardValue == cards[2].CardValue && cards[3].CardValue == cards[4].CardValue) ||
(cards[0].CardValue == cards[1].CardValue && cards[2].CardValue == cards[3].CardValue && cards[2].CardValue == cards[4].CardValue))
{
HV.Total = (int)(cards[0].CardValue) + (int)(cards[1].CardValue) + (int)(cards[2].CardValue) +
(int)(cards[3].CardValue) + (int)(cards[4].CardValue);
return true;
}

return false;
}

private bool StrFlush()
{
//all suits are same & 5 consecutive values
if (SpadeSum == 5 || HeartSum == 5 || DiamondSum == 5 || ClubSum == 5 &&
cards[0].FaceValue + 1 == cards[1].FaceValue &&
cards[1].FaceValue + 1 == cards[2].FaceValue &&
cards[2].FaceValue + 1 == cards[3].FaceValue &&
cards[3].FaceValue + 1 == cards[4].FaceValue)
{
HV.Total = (int)cards[4].CardValue;
return true;
}
return false;
}

private bool Flush()
{
//if all suits are the same
if (SpadeSum == 5 || HeartSum == 5 || DiamondSum == 5 || ClubSum == 5)
{
//if tie flush player with higher cards win
//whomever has last card has highest value, has automatically all the cards total higher
HV.Total = (int)cards[4].CardValue;
return true;
}
return false;
}

private bool Str()
{
//if 5 consecutive value
if (cards[0].CardValue + 1 == cards[1].CardValue &&
cards[1].CardValue + 1 == cards[2].CardValue &&
cards[2].CardValue + 1 == cards[3].CardValue &&
cards[3].CardValue + 1 == cards[4].CardValue)
{
//player with highest value of the last card wins
HV.Total = (int)cards[4].CardValue;
return true;
}

return false;
}

private bool ToK()
{
//if the 1,2,3 cards are the same OR
//2,3,4 cards are the same OR
//3,4,5 cards are the same
//3rds card will always be a part of Three of A Kind

if ((cards[0].CardValue == cards[1].CardValue && cards[0].CardValue == cards[2].CardValue) ||
(cards[1].CardValue == cards[2].CardValue && cards[1].CardValue == cards[3].CardValue))
{
HV.Total = (int)cards[2].CardValue * 3;
HV.HighCard = (int)cards[4].CardValue;
return true;
}
else if (cards[2].CardValue == cards[3].CardValue && cards[2].CardValue == cards[4].CardValue)
{
HV.Total = (int)cards[2].CardValue * 3;
HV.HighCard = (int)cards[1].CardValue;
return true;
}
return false;
}

private bool TwoPairs()
{
//if 1,2 and 3,4
//if 1.2 and 4,5
//if 2.3 and 4,5
//with two pairs, the 2nd card will always be a part of one pair
//and 4th card will always be a part of second pair
if (cards[0].CardValue == cards[1].CardValue && cards[2].CardValue == cards[3].CardValue)
{
HV.Total = ((int)cards[1].CardValue * 2) + ((int)cards[3].CardValue * 2);
HV.HighCard = (int)cards[4].CardValue;
return true;
}
else if (cards[0].CardValue == cards[1].CardValue && cards[3].CardValue == cards[4].CardValue)
{
HV.Total = ((int)cards[1].CardValue * 2) + ((int)cards[3].CardValue * 2);
HV.HighCard = (int)cards[2].CardValue;
return true;
}
else if (cards[1].CardValue == cards[2].CardValue && cards[3].CardValue == cards[4].CardValue)
{
HV.Total = ((int)cards[1].CardValue * 2) + ((int)cards[3].CardValue * 2);
HV.HighCard = (int)cards[0].CardValue;
return true;
}
return false;
}

private bool OnePair()
{
//if 1,2 -> 5th card has the highest value
//2.3
//3,4
//4,5 -> card #3 has the highest value
if (cards[0].CardValue == cards[1].CardValue)
{
HV.Total = (int)cards[0].CardValue * 2;
HV.HighCard = (int)cards[4].CardValue;
return true;
}
else if (cards[1].CardValue == cards[2].CardValue)
{
HV.Total = (int)cards[1].CardValue * 2;
HV.HighCard = (int)cards[4].CardValue;
return true;
}
else if (cards[2].CardValue == cards[3].CardValue)
{
HV.Total = (int)cards[2].CardValue * 2;
HV.HighCard = (int)cards[4].CardValue;
return true;
}
else if (cards[3].CardValue == cards[4].CardValue)
{
HV.Total = (int)cards[3].CardValue * 2;
HV.HighCard = (int)cards[2].CardValue;
return true;
}

return false;
}


}

//END OF NAMESPACE
}


Cards Class:

namespace PROG120_FINALPROJECT
{
//START CLASS CARD
class Card
{
// Establishes suits
public enum SUIT
{
SPADE,
HEART,
DIAMOND,
CLUB
}

// Gives Cards a numeric vakue i.e. NINE of hearts
public enum VALUE
{
TWO = 1, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN,
JACK, QUEEN, KING, ACE
}

// just the face cards
public enum FACE
{
JACK,
QUEEN,
KING,
ACE
}

//Properties
public SUIT CardSuit
{
get;
set;
}


public VALUE CardValue
{
get;
set;
}

public FACE FaceValue
{
get;
set;
}


//END CLASS CARDS
}
}


Program class:

namespace PROG120_FINALPROJECT
{
class Program
{
static void Main(string[] args)
{
//Define
Deal dc = new Deal();
// write string repersentation
// enum


Console.WriteLine("Please Enter the amount you would like to bet 1-5");
float bet = float.Parse(Console.ReadLine());

if (bet < 0)
{
Console.WriteLine("Please enter valid bet amount");
}
else
{
dc.DealSetup();
}


if (bet > 5)
{
Console.WriteLine("Please enter higher bet amount");
}
else
{
dc.GetHand();
}
}
}
}

Answer
public void GetHand()
{
    //5 cards for the player 
    for (int i = 0; i < 5; i++)
        PlayerHand[i] = GetDeck[i];


    //5 cards for computer
    for (int i = 0; i < 5; i++)
        ComputerHand[i] = GetDeck[i];
}

Deck is an array; you are retrieving the first 5 elements of that array twice. You probably want a function more like "GetNextCard" and keep track of the index inside the Deck class as it's not something the dealer should care about. Or implement Deck as a stack rather than array and pop each card off.

Comments