我的目标是通过允许西装对程序的结果产生影响来修改给定的程序(4个类)。一般情况下,给用户一张卡片,然后用户继续猜测下一张卡片是否将具有更高的数值。但是,我不太确定如何修改它以考虑西装顺序。请注意,我需要输入这么多代码,因为帮助我的人产生的输入需要被合并到不止一类程序中。因为这是一个初学者 Java 程序/
花色顺序意味着如果有人收到红心 7,而下一张牌是梅花 7,结果不应该是平局,因为红心的花色值大于梅花。
由于花色排名为:黑桃 > 钻石 > 心 > 俱乐部
在 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() ;
}
}
}
甲板等级:
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;
}
}