如何制作卡片套装(订单)对所得卡片产生影响

时间:2013-10-15 19:52:18

标签: java class constructor

我的目标是修改给定的程序(4个类)通过允许诉讼对程序的结果产生影响。通常,用户被给予卡而不是用户进行猜测下一张卡是否具有更高的数值。但是,我不太确定如何修改它以考虑套装订单。请注意我需要提出这个代码,因为帮助我的人的结果输入需要被合并到不仅仅是一个类的程序中。因为这是一个初学者Java程序/

套装订单意味着如果有人收到7个心脏,而下一张牌是7个俱乐部,结果不应该是平局,因为Hearts比俱乐部具有更大的套装价值。

由于诉讼排名为:SPADES> DIAMONDS> HEARTS>会

在Highlow类中,有一行显示为:

if(nextCard.getValue() == currentCard.getValue()) {

其中表示相同的值,但这需要更改。 我已经尝试过补偿学习compareto()方法,然而,经过数小时的研究后,我意识到理解我的代码对我来说太复杂了,坦白地说甚至理解它。此外,我已经尝试注意如何将套装的案例简单地用作字符串,并且认为它们可以通过总和列表方法来安排,然后进一步检查和打印。然而,这种想法无济于事,因为我觉得好像我在思考这个练习的全部内容。课程如下,任何输入都会很受欢迎。

    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;


  private final int suit; 

  private final int value;
  public static void main (String [] args){
  }
  public Card() {
    suit = JOKER;
    value = 1;
  }

  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;
  }

  public int getSuit() {
    return suit;
  }

  public int getValue() {
    return value;
  }

  public String getSuitAsString() {
    switch ( suit ) {
      case SPADES:   return "Spades";
      case HEARTS:   return "Hearts";
      case DIAMONDS: return "Diamonds";
      case CLUBS:    return "Clubs";
      default:       return "Joker";
    }
  }

  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";
      }
    }
  }   
  public String toString() {
    if (suit == JOKER) {
      if (value == 1)
        return "Joker";
      else
        return "Joker #" + value;
    }
    else {

        return getValueAsString() + " of " + getSuitAsString() ;
      }
    }

}

Deck CLass:

public class Deck {
   private Card[] deck;
   private int cardsUsed;

   public Deck() {
      this(false);  
   }

   public Deck(boolean includeJokers) {
      if (includeJokers)
         deck = new Card[54];
      else
         deck = new Card[52];
      int cardCt = 0; // How many cards have been created so far.
      for ( int suit = 0; suit <= 3; suit++ ) {
         for ( int value = 1; value <= 13; value++ ) {
            deck[cardCt] = new Card(value,suit);
            cardCt++;
         }
      }
      if (includeJokers) {
         deck[52] = new Card(1,Card.JOKER);
         deck[53] = new Card(2,Card.JOKER);
      }
      cardsUsed = 0;
   }

   public void shuffle() {
      for ( int i = deck.length-1; i > 0; i-- ) {
         int rand = (int)(Math.random()*(i+1));
         Card temp = deck[i];
         deck[i] = deck[rand];
         deck[rand] = temp;
      }
      cardsUsed = 0;
   }

   public int cardsLeft() {
      return deck.length - cardsUsed;
   }

   public Card dealCard() {
      if (cardsUsed == deck.length)
         throw new IllegalStateException("No cards are left in the deck.");
      cardsUsed++;
      return deck[cardsUsed - 1];
   }
   public boolean hasJokers() {
      return (deck.length == 54);
   }
} 

手类

import java.util.ArrayList;

public class Hand {

   private ArrayList hand;   

   public Hand() {
      hand = new ArrayList();
   }

   public void clear() {
      hand.clear();
   }

   public void addCard(Card c) {
      if (c == null)
         throw new NullPointerException("Can't add a null card to a hand.");
      hand.add(c);
   }

   public void removeCard(Card c) {
      hand.remove(c);
   }

   public void removeCard(int position) {
      if (position < 0 || position >= hand.size())
         throw new IllegalArgumentException("Position does not exist in hand: "
               + position);
      hand.remove(position);
   }

   public int getCardCount() {
      return hand.size();
   }

   public Card getCard(int position) {
      if (position < 0 || position >= hand.size())
         throw new IllegalArgumentException("Position does not exist in hand: "
               + position);
       return (Card)hand.get(position);
   }

   public void sortBySuit() {
      ArrayList newHand = new ArrayList();
      while (hand.size() > 0) {
         int pos = 0;  // Position of minimal card.
         Card c = (Card)hand.get(0);  // Minimal card.
         for (int i = 1; i < hand.size(); i++) {
            Card c1 = (Card)hand.get(i);
            if ( c1.getSuit() < c.getSuit() ||
                    (c1.getSuit() == c.getSuit() && c1.getValue() < c.getValue()) ) {
                pos = i;
                c = c1;
            }
         }
         hand.remove(pos);
         newHand.add(c);
      }
      hand = newHand;
   }

   public void sortByValue() {
      ArrayList newHand = new ArrayList();
      while (hand.size() > 0) {
         int pos = 0;  // Position of minimal card.
         Card c = (Card)hand.get(0);  // Minimal card.
         for (int i = 1; i < hand.size(); i++) {
            Card c1 = (Card)hand.get(i);
            if ( c1.getValue() < c.getValue() ||
                    (c1.getValue() == c.getValue() && c1.getSuit() < c.getSuit()) ) {
                pos = i;
                c = c1;
            }
         }
         hand.remove(pos);
         newHand.add(c);
      }
      hand = newHand;
   }
}

主程序类

import java.io.*;

public class HighLow {

  public static void main(String[] args) {

    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));  // allow input

    System.out.println("This program lets you play the simple card game,");
    System.out.println("HighLow.  A card is dealt from a deck of cards.");
    System.out.println("You have to predict whether the next card will be");
    System.out.println("higher or lower.  Your score in the game is the");
    System.out.println("number of correct predictions you make before");
    System.out.println("you guess wrong.");
    System.out.println();

    int gamesPlayed = 0;     // Number of games user has played.
    int sumOfScores = 0;     // The sum of all the scores from 
    //      all the games played.
    double averageScore;     // Average score, computed by dividing
    //      sumOfScores by gamesPlayed.
    boolean playAgain;       // Record user's response when user is 
    //   asked whether he wants to play 
    //   another game.
    do {
      int scoreThisGame;        // Score for one game.
      scoreThisGame = play();   // Play the game and get the score.
      sumOfScores += scoreThisGame;
      gamesPlayed++;
      System.out.println("Play again? ");
      playAgain = TextIO.getlnBoolean();
    } while (playAgain);

    averageScore = ((double)sumOfScores) / gamesPlayed;

    System.out.println();
    System.out.println("You played " + gamesPlayed + " games.");
    System.out.printf("Your average score was %1.3f.\n", averageScore);

  }  // end main()

  private static int play() {

    Deck deck = new Deck();  // Get a new deck of cards, and 
    Card currentCard;  // The current card, which the user sees.
    Card nextCard;   // The next card in the deck.  The user tries
    int correctGuesses ;  // The number of correct predictions the
    char guess;   // The user's guess.  'H' if the user predicts that
    deck.shuffle();  // Shuffle the deck into a random order before
    correctGuesses = 0;
    currentCard = deck.dealCard();
    System.out.println("The first card is the " + currentCard);
    while (true) {  // Loop ends when user's prediction is wrong.

      /* Get the user's prediction, 'H' or 'L' (or 'h' or 'l'). */

      TextIO.put("Will the next card be higher (H) or lower (L)?  ");
      do {
        guess = TextIO.getlnChar();
        guess = Character.toUpperCase(guess);
        if (guess != 'H' && guess != 'L') 
          System.out.println("Please respond with H or L:  ");
      } while (guess != 'H' && guess != 'L');

      nextCard = deck.dealCard();
      System.out.println("The next card is " + nextCard);

      if(nextCard.getValue() == currentCard.getValue()) {

        System.out.println("The value is the same as the previous card.");
        System.out.println("You lose on ties.  Sorry!");
        break;  // End the game.
      }
      else if (nextCard.getValue() > currentCard.getValue()) {
        if (guess == 'H') {
          System.out.println("Your prediction was correct.");
          correctGuesses++;
        }
        else {
          System.out.println("Your prediction was incorrect.");
          break;  // End the game.
        }
      }
      else {  // nextCard is lower
        if (guess == 'L') {
          System.out.println("Your prediction was correct.");
          correctGuesses++;
        }
        else {
          System.out.println("Your prediction was incorrect.");
          break;  // End the game.
        }
      }
      currentCard = nextCard;
      System.out.println();
      System.out.println("The card is " + currentCard);

    } // end of while loop

    System.out.println();
    System.out.println("The game is over.");
    System.out.println("You made " + correctGuesses 
                         + " correct predictions.");
    System.out.println();    
    return correctGuesses;
  }  
} 

2 个答案:

答案 0 :(得分:0)

为什么不实施:

public int getSuitValue() {
    return suit+value;
  }

在比较中使用getSuitValue而不是getValue。更高的套装将永远胜利。

您还必须更改套装的int值,以便HEARTS&gt;会

答案 1 :(得分:0)

在主要方法的这种情况下

if(nextCard.getValue() == currentCard.getValue()) {

    System.out.println("The value is the same as the previous card.");
    System.out.println("You lose on ties.  Sorry!");
    break;  // End the game.
}

只需添加另一个if语句,然后比较套装差异并显示相应的输出

if(nextCard.getValue() == currentCard.getValue()) {
    if(nextCard.getSuit() > currentCard.getSuit()) {
        System.out.println("Some text");
        break;  // End the game.
    }
    else if(nextCard.getSuit() < currentCard.getSuit()){
       System.out.println("Some text");
       break;  // End the game.
    }
    else
       System.out.println("Some how you got the same card");
}

更好的方法是在你的卡片类中实现compareTo(Card)方法,如此

public int compareTo(Card c)
{
    if((this.value - c.value) != 0)
        return this.value - c.value;
    else 
        return this.suit - c.suit
}

然后调用nextCard.compareTo(currentCard),如果返回值为0则相等,如果为负数(< 0),则nextCard低于currentCard。如果是肯定的(> 0)则nextCard高于currentCard