私の目標は、与えられたプログラム (4 つのクラス) を変更して、スーツがプログラムの結果に影響を与えるようにすることです。一般に、ユーザーは次のカードがより高い数値を持つかどうかを推測するよりも、カードを与えられます。ただし、スーツの順序を考慮して変更する方法についてはよくわかりません。これほど多くのコードを入力する必要があることに注意してください。これは、私を助けてくれた人々から得られた入力を、プログラムの複数のクラスに組み込む必要があるためです。これは初心者のJavaプログラムなので/
誰かがハートの 7 を受け取り、次のカードがクラブの 7 である場合、ハートはクラブよりもスーツの値が大きいため、結果は引き分けにはなりません。
スーツのランクとして: SPADES > DIAMONDS > HEARTS > CLUBS
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;
}
}