我正在实现一个简单的“玩你的卡片”(否则称为更高/更低)的游戏。如果你在规则非常简单之前还没有遇到过它。使用一套卡(例如心)。一次抽出一张卡,目的是正确猜测下一张卡的面值是高于还是低于先前抽取的卡的面值。Java - 简单游戏的设计建议
游戏的逻辑并不特别复杂,我并不担心这一点。我想出了一个设计,但我并不完全满意。有几个方面我确信它可以得到改进,这就是我希望得到您的建议。下面是这个类的接口(用于额外的理解意见,不实评论):
public interface PlayYourCardsRight {
/**
* Get the number of cards remaining with higher face values than the previously
* drawn card
* @return
*/
public abstract int getNumberCardsHigher();
/**
* Get the number of cards remaining with lower face values than the previously
* drawn card
* @return
*/
public abstract int getNumberCardsLower();
/**
* Get all cards that have already been drawn in the order they were drawn in
*
*/
public abstract List<Card> getPlayedCards();
/**
* Simple prediction algorithm - if there are more cards left in the deck with
* lower face values than the previous card, then predict 'Lower', if there
* are more cards left in the deck with higher face values then predict
* 'Higher', if there are equal numbers of higher/lower cards pick 'higher' or 'lower'
* at random
*
* Prediction is an Enum (Higher/Lower/None)
*
*/
public abstract Prediction getPrediction();
/*
* Draw the next card at random
*/
public abstract void nextRound();
/**
* Specifiy what the next card should be
*
* @param card
*/
public abstract void nextRound(Card card);
}
正如你可以看到它是所有相当自我解释和简单。这里是我的问题:
我不希望构造函数自动绘制一张卡片。这意味着最初没有“先前绘制的卡”。我在Prediction
枚举中有一个NO PREDICTION
值,但由于没有“先前绘制的牌”,所以getNumberCardsHigher()
和getNumberCardsLower()
方法不能返回理智值(当套牌中的所有牌都被绘制时,它们不能返回理智值)。
显然,我可以简单地抛出一个异常,但这看起来像是矫枉过正 - 尤其是随后所有对这些方法的调用都必须被包装在try/catch中。我也不满意返回一个负值,因为如果有人忘记/不能检查它们,很容易导致一些错误。
欢迎任何建议!
在这种情况下,我认为这两种方法都不会返回'card.count'。这个解决方案表明,有13张牌更高,13张牌更低,这意味着总共26张牌,即使在一套西装和游戏中只有13张牌。即使我们知道发生了什么,想象将这些信息呈现给用户。这似乎是一个非常不满意的行为。 – Peter 2010-07-02 16:45:57
@Peter:我的推理如下:一张卡比卡片的缺失更低和更高。这是两个相反的状态在虚无概念上是平等的情况之一。这对我来说似乎并不直观,但这只是我的看法。当没有卡时,两者都应该返回0,所以它们在该阶段也是相等的。在开始时,它们应该返回相反的0,在这种情况下card.count。 – JRL 2010-07-02 17:05:03