- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个玩家与计算机对战的扑克游戏。我遇到了一个问题:
玩家和计算机生成相同的手牌/牌。
我假设这是排序问题或洗牌问题,但似乎无法找到问题所在。谁能帮忙?
我的意思的例子
交易类别:
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/
我试图理解 java.util.concurrent.locks 库,并希望实现两个通过列表运行的线程,而第二个线程不应超过(领先)第一个线程。具体来说,我想实现手动锁定。 我写了下面的代码,但不起作
我正在寻找 Adobe Reader 中的张开的手/闭合的手 鼠标光标。他们在哪里?如果它们是非标准的,我用 C# 创建它们的最简单方法是什么? 最佳答案 是的,它们是非标准的。您只需要四处搜索,
我有这些警告,但我不知道如何解决它们。 warning: left-hand operand of comma has no effect warning: right-hand operand of
我在网站上寻求帮助,但仍然找不到此问题的答案。 这是代码: import java.util.Scanner; public class Player { private Card[] hand; p
当我尝试这样做时出现错误 friend std::ostream& operator<<(std::ostream& os, const hand& obj) { return obj.sho
我之前没有学过javascript,完全不知道自己该怎么做,因为我只是跟着别人的项目,希望有人能告诉我如何修改这些错误的地方。谢谢T_T 最佳答案 改变 function aniSprite (col
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
题目地址:https://leetcode.com/problems/hand-of-straights/description/ 题目描述 Alice has a hand of cards,
我有一个包含几个变量的循环;其中一个在每一步递增。然而,有时,这个变量可能会被重置为 0。因此我可以写: (loop with z = 0 ... do (progn
在 this link ,期望的结果是当光标悬停在图例上时,鼠标不会变为手形。我怎样才能实现它? 我不确定,但是这样的东西有用吗? window.setTimeout("document.body.s
我有一个 GameActivity 和一个 GameOverActivity。我将游戏积分保存在 GameActivity 中,但会在 GameOverActivity 中显示这些积分。我必须做什么?
这个问题已经有答案了: Output of System.out.println(object) (4 个回答) 已关闭 9 年前。 我不确定为什么会得到这个输出。当我尝试为我的扑克程序打印一手牌时。
我使用 carousel bootstrap 3 来使用幻灯片文本。当第 1 行文本长到底部(例如:高度 300 像素)而第 2 行文本太短(例如:仅 10 像素)时,问题是当我们单击第 # 行旁边时
如何使用 JavaScript 在 td 元素中添加样式属性 cursor? 我已经试过了: var tabla = document.getElementById("table"); var fil
有没有办法从导入的模块中获取异常处理输出并从调用程序中处理它?例如,我有一个写入 HTTP 异常的导入模块 except urllib2.HTTPError, e: sys.stderr.wri
我在这里遵循 W3C 的标准 http://www.w3.org/TR/CSS2/ui.html在这里 http://www.quirksmode.org/css/cursor.html#note .
我正在使用 Unity 构建汽车模拟器游戏。对于输入,我使用 Logitheck 方向盘 G29。现在我需要使用 Hand Controller 来加速或刹车。这是我的手控器手控器 HC1 Link
我在 chrome 浏览器中运行我的 flutter 应用程序,它会显示以下错误。 Do not call didCreateEngineInitializer by hand. Start with
我想为我的 Lable 设置一个鼠标监听器,这样当用户将鼠标放在标签上时,我可以将光标更改为 HAND_CURSOR。 我试图设置样式 css“cursor: hand;”对于这个标签,但是在运行
我正在使用 idTCPServer 来处理数据。对于新设备,我需要将套接字移交给 dll(停止 tcp 服务器从该套接字读取数据)。 Indy 或 ICS 可以实现这一点吗? [编辑]出于测试目的,我
我是一名优秀的程序员,十分优秀!