如何在词典之间移动项目?

发布于 2025-01-12 12:36:02 字数 554 浏览 0 评论 0原文

我现在正在开发一个 BlackJack 程序,我想在一个字典中制作一副完整的牌,例如

"card" : points
cards = {
"2S" : 2,
"2H" : 2,
"2D" : 2,
"2C" : 2,
"3S" : 3,
"3H" : 3,
"3D" : 3,
"3C" : 3
.
.
.
"JS" : 10,
"JH" : 10,
"JD" : 10,
"JC" : 10,
}

......然后通过交易将随机牌添加到其他字典(玩家或计算机)并从主牌中删除或者只是转到:

player_cards = {}
computer_cards = {}

如何将随机项目从 cards 字典移动到其他字典?发牌后,牌将从主牌中消失,并出现在玩家和计算机上。

i'm working now on a BlackJack program, and i want to make a full deck of cards in one dictionary, like..

"card" : points
cards = {
"2S" : 2,
"2H" : 2,
"2D" : 2,
"2C" : 2,
"3S" : 3,
"3H" : 3,
"3D" : 3,
"3C" : 3
.
.
.
"JS" : 10,
"JH" : 10,
"JD" : 10,
"JC" : 10,
}

... and then with deal add random cards to other dictionaries (players or computer) and remove from main deck or just move to:

player_cards = {}
computer_cards = {}

How can i move random items from cards dictionary to other dictionary? that after the deal, the cards will disappear from the main deck and appear for player and for computer.

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

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

发布评论

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

评论(2

苦妄 2025-01-19 12:36:02
import random
random_card = random.choice(list(cards))
player_cards[random_card] = cards[random_card]
import random
random_card = random.choice(list(cards))
player_cards[random_card] = cards[random_card]
客…行舟 2025-01-19 12:36:02

请澄清你的问题

我假设字典“牌”中的值是每个面和值剩余的牌数,但这没有多大意义,因为你有多个黑桃2(我想这意味着你使用多副牌?但这意味着可以连续抽出两个黑桃 2)。

如果这就是您想要的,那么

import random 
drawn=random.choice([k for k in cards for x in range(cards[k])])

#If it is the player's turn
player_cards[drawn]=1

#If it is the computer's turn
player_cards[drawn]=1

如果您由于相同的索引键而连续抽出同一张卡,则此代码将会遇到问题。因此,我会争论将player_cards和computer_cards设置为字典的含义。如果您想允许绘制同一张卡,将它们变成列表并简单地将绘制的卡附加到它们会容易得多。

编辑:

为了使卡牌在被吸引到玩家/计算机的牌组后从通用牌组中消失,

cards[drawn]-=1

Please clarify your question

I assume the values in the dictionary "cards" are the number of cards remaining for each face and value, but that does not make much sense since you have multiple 2 of spades (I suppose this means you use multiple decks? But that would mean it is possible to draw two 2 of spades consecutively).

If this is what you want, then

import random 
drawn=random.choice([k for k in cards for x in range(cards[k])])

#If it is the player's turn
player_cards[drawn]=1

#If it is the computer's turn
player_cards[drawn]=1

This code would run into problems if you draw the same card consecutively due to same index key. I would therefore argue the meaning of setting player_cards and computer_cards as dictionaries. If you want to allow drawing the same card, turning them into lists and simply appending drawn cards to them would be a lot easier.

Edit:

For the card to disappear from the general deck after being drawn to the player's/computer's deck,

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