3

I am becoming a bit more confident with my programming skills, so I've decided to restart a card game I'd previously began. The point of this program is now that I have a decent grasp of program flow, variables, conditions and so on, I want to deepen my understanding of OOP

So I need some advise on object oriented design

My card game will have 5 classes:

  1. Main
  2. Card
  3. Deck (has-a Card ArrayList)
  4. Player (has-a Card ArrayList of Card objects received from a Deck object)
  5. Dealer

I am wondering if it would be proper OOP to make the Dealer class an interface. All players should be able to play the role of dealer, but needless to say, there is only one dealer per round of cards. Is it ok to make the Player class implement the methods a dealer can perform (such as dealGame()) even when 7 out of 8 Player objects in any given round won't be making use of their implemented methods, and furthermore, are NOT TO BE CONSIDERED A DEALER at the time? Or would it be better to make the dealGame() method belong to the Deck class and call the deck to deal a game? Sorry if this is a dumb question, but I'm a bit sketchy on the principles of OOP and want some advise to learn to do it right the first time.

I also thought of making Dealer extend Player but I think that would be wrong because I need players to assume a role of Dealer on the fly, rather than be declared as a Dealer object in an unchangeable fashion. In this case - if Dealer extends Player - I think I would need to declare all players of the game as Dealers.

So basically I'm asking:

  1. If you were making a card game with these 5 classes, would you make the Dealer class an interface and the rest regular classes, and why or why not?
  2. Am I generally on the right track with OOP or am I completely lost?
4

3 回答 3

4

你有很好的方法,但仍有一些工作要做:)。所有的课程都很好,但德拉尔不是。

首先 - 如果玩家一轮是庄家,第二轮是庄家,我看不到扩展类或接口的点。

事实上,一开始,我不会让任何玩家成为庄家,这会让事情变得复杂。

处理这个问题的最简单和最好的方法是创建 5.Game。游戏有甲板和所有玩家,它的工作方式与经销商类似。它等待玩家响应,它应该显示卡片等等。

如果您真的想让玩家成为经销商,那将更加复杂:)。好吧,您还需要 Game 类,因为Game必须决定谁作为经销商开始,它应该剥夺或给予玩家经销商的权利(好吧,我不明白它的意义,但如果你想要...... :))。

以及如何做到这一点?有变量GamePlayer dealer如果应该有类似的registerDealer(Player player)方法。然后你需要某种接口在 和 之间进行Game通信Player。如果你想对PlayerALL THE RESPONSIBILITY 负责Game,你可以使用Visitor pattern. (它允许dealer“潜入”游戏实例内部并做任何公共方法可以做的事情)。

否则,您可以让Game询问dealer要做什么并等待玩家输入。它应该通过调用类似的方法dealer.askWhatToDo()并使用返回值来完成。它允许验证输入......但是,如果我是对的,扑克中的有效输入总是只有一个,所以我看不出有什么区别,如果Game它自己决定做什么......

我看到的唯一原因是如果dealer可以洗牌或提高基数或类似的东西,那么它可以完成......或者如果你想模拟扑克游戏,扑克玩家和经销商可以做无效的事情就像在真实生活 :)。

于 2013-10-23T23:04:05.277 回答
1

这在很大程度上取决于程序是如何工作的,你期望每个类能够做什么,以及每个对象的生命周期是如何工作的(例如,玩家能否在以后的回合中成为庄家)。

我对您所写的第一印象是您需要一个基类“人”,“玩家”和“经销商”从该基类继承,但这只是对有限信息的猜测。

于 2013-10-23T23:01:46.567 回答
1

庄家并不是真正的特殊类型的玩家。一次只有一个 Player 实例,具有特殊责任。我认为你在正确的轨道上,但你可能想为你正在玩的特定游戏创建一个类。

假设您正在编写一个扑克游戏。在这种情况下,庄家偶尔会影响游戏的顺序(取决于风格)。但如果你在玩战争,庄家只是发牌的人,你真的不在乎。在那种情况下,“谁是庄家”和“庄家应该如何处理游戏”的概念是游戏本身的责任,而不是玩家的责任。

于 2013-10-23T23:03:44.757 回答