当前位置: 首页 > 知识库问答 >
问题:

面向对象的卡盘设计--如何使未来的卡盘更灵活

宗政天逸
2023-03-14

假设我们有一副牌的面向对象设计,如编程示例:Card,Hand,Deck。

从URL复制,这里是完整的Card类。

它足够通用,可以高度重用,所以从长远来看,设计、编写和测试它的工作是有丰厚回报的。

/**
 * An object of type Card represents a playing card from a
 * standard Poker deck, including Jokers.  The card has a suit, which
 * can be spades, hearts, diamonds, clubs, or joker.  A spade, heart,
 * diamond, or club has one of the 13 values: ace, 2, 3, 4, 5, 6, 7,
 * 8, 9, 10, jack, queen, or king.  Note that "ace" is considered to be
 * the smallest value.  A joker can also have an associated value; 
 * this value can be anything and can be used to keep track of several
 * different jokers.
 */

public class Card {

   public final static int SPADES = 0;   // Codes for the 4 suits, plus Joker.
   public final static int HEARTS = 1;
   public final static int DIAMONDS = 2;
   public final static int CLUBS = 3;
   public final static int JOKER = 4;

   public final static int ACE = 1;      // Codes for the non-numeric cards.
   public final static int JACK = 11;    //   Cards 2 through 10 have their 
   public final static int QUEEN = 12;   //   numerical values for their codes.
   public final static int KING = 13;

   /**
    * This card's suit, one of the constants SPADES, HEARTS, DIAMONDS,
    * CLUBS, or JOKER.  The suit cannot be changed after the card is
    * constructed.
    */
   private final int suit; 

   /**
    * The card's value.  For a normal card, this is one of the values
    * 1 through 13, with 1 representing ACE.  For a JOKER, the value
    * can be anything.  The value cannot be changed after the card
    * is constructed.
    */
   private final int value;

   /**
    * Creates a Joker, with 1 as the associated value.  (Note that
    * "new Card()" is equivalent to "new Card(1,Card.JOKER)".)
    */
   public Card() {
      suit = JOKER;
      value = 1;
   }

   /**
    * Creates a card with a specified suit and value.
    * @param theValue the value of the new card.  For a regular card (non-joker),
    * the value must be in the range 1 through 13, with 1 representing an Ace.
    * You can use the constants Card.ACE, Card.JACK, Card.QUEEN, and Card.KING.  
    * For a Joker, the value can be anything.
    * @param theSuit the suit of the new card.  This must be one of the values
    * Card.SPADES, Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER.
    * @throws IllegalArgumentException if the parameter values are not in the
    * permissible ranges
    */
   public Card(int theValue, int theSuit) {
      if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && 
            theSuit != CLUBS && theSuit != JOKER)
         throw new IllegalArgumentException("Illegal playing card suit");
      if (theSuit != JOKER && (theValue < 1 || theValue > 13))
         throw new IllegalArgumentException("Illegal playing card value");
      value = theValue;
      suit = theSuit;
   }

   /**
    * Returns the suit of this card.
    * @returns the suit, which is one of the constants Card.SPADES, 
    * Card.HEARTS, Card.DIAMONDS, Card.CLUBS, or Card.JOKER
    */
   public int getSuit() {
      return suit;
   }

   /**
    * Returns the value of this card.
    * @return the value, which is one of the numbers 1 through 13, inclusive for
    * a regular card, and which can be any value for a Joker.
    */
   public int getValue() {
      return value;
   }

   /**
    * Returns a String representation of the card's suit.
    * @return one of the strings "Spades", "Hearts", "Diamonds", "Clubs"
    * or "Joker".
    */
   public String getSuitAsString() {
      switch ( suit ) {
      case SPADES:   return "Spades";
      case HEARTS:   return "Hearts";
      case DIAMONDS: return "Diamonds";
      case CLUBS:    return "Clubs";
      default:       return "Joker";
      }
   }

   /**
    * Returns a String representation of the card's value.
    * @return for a regular card, one of the strings "Ace", "2",
    * "3", ..., "10", "Jack", "Queen", or "King".  For a Joker, the 
    * string is always numerical.
    */
   public String getValueAsString() {
      if (suit == JOKER)
         return "" + value;
      else {
         switch ( value ) {
         case 1:   return "Ace";
         case 2:   return "2";
         case 3:   return "3";
         case 4:   return "4";
         case 5:   return "5";
         case 6:   return "6";
         case 7:   return "7";
         case 8:   return "8";
         case 9:   return "9";
         case 10:  return "10";
         case 11:  return "Jack";
         case 12:  return "Queen";
         default:  return "King";
         }
      }
   }

   /**
    * Returns a string representation of this card, including both
    * its suit and its value (except that for a Joker with value 1,
    * the return value is just "Joker").  Sample return values
    * are: "Queen of Hearts", "10 of Diamonds", "Ace of Spades",
    * "Joker", "Joker #2"
    */
   public String toString() {
      if (suit == JOKER) {
         if (value == 1)
            return "Joker";
         else
            return "Joker #" + value;
      }
      else
         return getValueAsString() + " of " + getSuitAsString();
   }


} // end class Card

假设我想在这个设计中包含两个joker,而不干扰当前的类,如何实现呢?

共有1个答案

颛孙镜
2023-03-14

Card类中,构造函数之一是(value,suit)。小丑被定义为卡片套件之一。您可以指定任何您希望的值来跟踪小丑。

card类的默认构造函数创建一个joker。

您可以通过将新的final static int值添加到花色和值中来定义任何您希望的附加牌。

 类似资料:
  • 基本上,我在cassandra上运行两个期货查询,然后我需要做一些计算并返回值(值的平均值)。 这是我的代码: 那么问题出在哪里呢? skus.foreach 在 ListBuffer 中追加结果值。由于一切都是异步的,当我尝试在我的主数据库中获取结果时,我得到了一个错误,说我不能被零除。 事实上,由于我的Sku.findSkusByProduct返回一个Future,当我尝试计算平均值时,卷是空

  • 问题内容: 我正在设计一个简单的游戏,该游戏使用Java 2D和牛顿物理学。目前,我的主要“游戏循环”如下所示: 当指示实体更新自身时,它将根据当前施加在其上的力来调整其速度和位置。但是,我需要实体表现出其他行为。例如,如果“坏家伙”被玩家射击,则该实体应被销毁并从游戏世界中移除。 我的问题 :以面向对象的方式实现此目标的最佳方法是什么?到目前为止,我所见过的所有示例都将游戏循环整合到名为的类似的

  • 7.4 面向对象设计* 理解了面 向 对象的基 本 概念之后 , 就可以应 用 这些概念 来 进行面向 对 象 设 计(object-oriented design,简称 OOD)。 传统的程序设计方法是结构化的自顶向下设计,其思想是将软件系统分解为若干个功能, 每个功能都是对数据的一个操作过程。功能又可以划分为若干个子功能,如此反复分解下去, 直至每个功能所对应的操作过程显而易见为止。在分解功能

  • 大概这些,个人比较能叨叨叨,已offer 二面也是两个人,一个大领导,一个hr 深挖实习简历 为什么想来游戏 对实习的项目有什么看法 从设计的专业角度上怎么看借鉴 怎么做设计 平时实习的流程 实习的时候跟其他部门有没有什么问题 怎么提升自己

  • “在面向对象编程中,抽象是对用户隐藏实现细节的过程,只有功能才会提供给用户。” 我一直在试图理解抽象,有人能告诉我们如何准确地隐藏实现细节吗?使用一个程序

  • 假设我有一个非常大的对象集合(数百万个),这些对象是根据proto-wire格式序列化的。是否可以从文件中流式处理这些项目?我尝试将对象序列化为列表 我怀疑解决方案需要我知道每个序列化项的大小,然后从流中读取该大小,并将该字节跨度传递给protobuf序列化程序进行反序列化。我想确保没有一种更简单的机制来完成这项任务,这种机制不需要了解每个对象实例中可能不同的单个项目的长度。 我的另一个想法是在流