gpt4 book ai didi

c# - 扑克游戏 : Player and computer generating same hand

转载 作者:行者123 更新时间:2023-11-30 13:18:54 25 4
gpt4 key购买 nike

我正在创建一个玩家与计算机对战的扑克游戏。我遇到了一个问题:

玩家和计算机生成相同的手牌/牌。

我假设这是排序问题或洗牌问题,但似乎无法找到问题所在。谁能帮忙?

我的意思的例子

enter image description here

交易类别:

    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!");
}
}
}
}

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

我认为您不需要这些,但以防万一这是我的其他类(class)-

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
}

卡片类:

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

程序类:

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

最佳答案

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是一个数组;您正在两次检索该数组的前 5 个元素。您可能想要一个更像“GetNextCard”的函数并跟踪 Deck 类中的索引,因为这不是经销商应该关心的事情。或者将 Deck 实现为堆栈而不是数组,然后弹出每张卡片。

关于c# - 扑克游戏 : Player and computer generating same hand,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37636273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com