如何决定应该为问题陈述构建的类?

2024-05-16 23:34:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用类和其他oops概念为blackjack编写代码。目前,我被困在如何决定它将有什么类

以下是21点的规则:

1.创建一副52张卡片

2.洗牌

3.请玩家下注

4.确保玩家的赌注不超过其可用筹码

5.发两张牌给庄家,发两张牌给玩家

6.仅显示经销商的一张卡,另一张卡保持隐藏状态

7.出示玩家的两张牌

8.询问玩家是否希望击球,然后再拿一张牌

9.如果玩家的手没有破裂(超过21),询问他们是否愿意再次击球

10.如果有玩家站着,则玩庄家的牌。经销商将始终命中,直到经销商的值达到或超过17

11.确定胜利者并相应调整玩家的筹码

12.询问球员是否愿意再次比赛

我不熟悉编码和oops,请帮忙

注意:这不是家庭作业问题,因为github上有很多解决方案,如果需要,我可以复制提交。我只想学习面向对象编程和类。我不寻求解决方案,我在寻求一个正确的思考过程


Tags: 代码概念编码规则状态玩家解决方案球员
2条回答

你应该考虑你在游戏中需要的任何物体,并考虑它们的属性和动作,例如牌应该有一套衣服和一个值,这些是牌的属性。牌组应该包含每一张牌,并且能够洗牌,所以在你的牌组类中创建一个方法来完成这个功能

在这里,我将留下一个链接,以便您能够更好地理解python中的OOP

https://realpython.com/python3-object-oriented-programming/

这是一个艰难的过程;通常,第一步是识别项目描述中的名词,这为您提供了一个起点,从何处考虑您将为代码提供的形状,以及您如何看待这些对象的交互

根据您的描述,我们可以列出以下名词:

牌组、玩家、赌注、筹码、牌、庄家、手牌、赢家、玩家的藏品。

它们可能代表,也可能不代表21点表示中的有用对象。有些可能是你现在需要的明显物品(牌组、牌、手、玩家、庄家);有些可以合并(筹码、赌注、藏匿);有些可能在普通应用程序中不需要(赢家、下注、隐藏),而是由数据结构(如列表、向量、哈希表等)代替

  1. Create a deck of 52 cards

  2. Shuffle the deck

  3. Ask the Player for their bet

  4. Make sure that the Player’s bet does not exceed their available chips

  5. Deal two cards to the Dealer and two cards to the Player

  6. Show only one of the Dealer’s cards, the other remains hidden

  7. Show both of the Player’s cards

  8. Ask the Player if they wish to Hit, and take another card

  9. If the Player’s hand doesn’t Bust (go over 21), ask if they’d like to Hit again.

  10. If a Player Stands, play the Dealer’s hand. The dealer will always Hit until the Dealer’s value meets or exceeds 17

  11. Determine the winner and adjust the Player’s stash accordingly

  12. Ask the Player if they’d like to play again

相关问题 更多 >