使用字典尝试搜索值和键(扑克游戏)

发布于 2024-10-20 18:12:29 字数 435 浏览 7 评论 0原文

我正在尝试使用 C# 创建一个基本的扑克游戏。

到目前为止,我已经使用这三行随机发牌了:

Suit suit = (Suit)randomCardSelector.Next(4);
Value value = (Value)randomCardSelector.Next(13);
Card abc = new Card(suit, value);

现在我想要实现的是,只发那些尚未发牌的牌。现在,为了做到这一点,我创建了两个“字典”集合,一个称为“deck”,一个称为“dealt”。我考虑过最初将所有牌添加到牌组中,然后当在桌子上发牌时,应将它们从牌组中删除并使用牌的唯一键添加到发牌中。

您认为解决这个问题的最佳方法是什么?我基本上不想要重复的牌,例如 Ace Hearts 和 Ace Hearts 组合位于同一副牌中。我怎样才能防止这种情况发生?

I am trying to create a basic poker game using c#.

So far I have got the cards randomly dealt using these three lines:

Suit suit = (Suit)randomCardSelector.Next(4);
Value value = (Value)randomCardSelector.Next(13);
Card abc = new Card(suit, value);

Now what I am trying to achieve is that the dealing of cards only of those who haven't been dealt already. Now to do this I have created two 'dictionary' collections, one called 'deck' and one called 'dealt'. I thought about adding all the cards to deck originally and then when dealt on the table they shall be deleted from deck and added to dealt using the cards unique key.

What do you believe will be the best way of going about this? I basically do not want duplicate cards, example Ace Hearts and Ace Hearts combination being in the same deck. How can I prevent this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苏大泽ㄣ 2024-10-27 18:12:29

为什么不直接预先生成一张牌列表,然后洗牌并将它们推入堆栈。

当您发牌时,只需从牌堆中弹出下一张牌即可。

还有另一个问题这里 了解如何在 C# 中打乱列表。

Why not just have a list of cards pre-generated, then shuffle the deck and push them onto a stack.

When you are dealing the cards simply pop the next card off the stack.

There is another SO question here on how to shuffle a list in C#.

无力看清 2024-10-27 18:12:29

我认为最简单的方法是:

A:重写 GetHashCode() 函数,并使用卡值和卡牌创建哈希。
B:实现 IEquatable 并比较花色和数值。

然后,只需一个包含所有发牌的 HashSet 即可(无需字典),并让您的牌生成器检查该牌是否已存在。

I think the simplest way would be to:

A: Override the GetHashCode() function, and create a hash using the card value and card suit.
B: Implement IEquatable<Card> and compare both the suits and the values.

Then, just have a single HashSet<Card> which contains all dealt cards (no need for a dictionary), and have your card generator check whether or not the card already exists.

星星的軌跡 2024-10-27 18:12:29

您的 Dictionary 方法应该可行,但我认为最简单的方法是生成一个 List 卡片,最初按某种自然顺序排列,然后将它们洗牌。然后,只需记录您已从列表中发出了多少张牌即可。当谈到洗牌时,您(以及尚未读过的其他人)应该阅读此关于洗牌陷阱的优秀文章

Your Dictionary approach should work, but I think the simplest way will be to generate a List of cards, initially in some natural order, and then shuffle them. Then, simply keep track of how many cards from the list you have dealt out. When it comes to shuffling the cards, you (and everyone else who hasn't read it yet) should read this excellent article on the pitfalls of shuffling.

白芷 2024-10-27 18:12:29

这不是通常的做法。我见过的大多数纸牌游戏都有 Deck 结构,通常是 0..51 范围内的值的数组 [0..51]。对于每个值,花色为 (cardValue / 13),价值为 (cardValue % 13)。您最初为新游戏创建牌组,方法是将数组初始化为 0..51,然后将每个位置的牌与另一个随机位置的牌随机交换。完成此操作后,您只需从一端挑选牌并根据需要“发牌”给玩家。

This is not how it's usually done. Most card games that I've seen have a Deck structure, which is usually an array[0..51] of values in the 0..51 range. For each value, suit is (cardValue / 13) and value is (cardValue % 13). You initally create the deck for a new game by initializing your array to 0..51 and then randomly exchanging the card at each position with a card at another, random position. Once you've done this, you just pick cards from one end and 'deal' to the players as needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文