如何在词典之间移动项目?
我现在正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请澄清你的问题
我假设字典“牌”中的值是每个面和值剩余的牌数,但这没有多大意义,因为你有多个黑桃2(我想这意味着你使用多副牌?但这意味着可以连续抽出两个黑桃 2)。
如果这就是您想要的,那么
如果您由于相同的索引键而连续抽出同一张卡,则此代码将会遇到问题。因此,我会争论将player_cards和computer_cards设置为字典的含义。如果您想允许绘制同一张卡,将它们变成列表并简单地将绘制的卡附加到它们会容易得多。
编辑:
为了使卡牌在被吸引到玩家/计算机的牌组后从通用牌组中消失,
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
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,