Question: I have the following Java code: Deck.java: public class Deck { private Card[] deck; private int cardsUsed; public Deck() { deck = new Card [52];
I have the following Java code:
Deck.java:
public class Deck {
private Card[] deck; private int cardsUsed;
public Deck() { deck = new Card [52]; int cardCt = 0; for (int suit = 0; suit<=3; suit++) { for (int value = 1; value <=10; value++) { deck[cardCt] = new Card(value,suit); cardCt++; } } } 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];
}
}
Card.java:
public class Card { public final static int SPADES = 0; public final static int HEARTS = 1; public final static int CLUBS = 2; public final static int DIAMONDS = 3; public final static int ACE = 1; public final static int JACK = 10; public final static int QUEEN = 10; public final static int KING = 10; private final int suit; private final int value; public Card(int theValue, int theSuit) { if (theSuit != SPADES && theSuit != HEARTS && theSuit != DIAMONDS && theSuit != CLUBS) throw new IllegalArgumentException("Illegal playing card suit"); if (theValue < 1 || theValue > 10) 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 CLUBS: return "Clubs"; default: return "Diamonds"; } } public String getValueAsString() { 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() { return getValueAsString() + " of " + getSuitAsString(); } }
DiamondRush.java:
import textio.TextIO; public class DiamondRush { public static void main(String[] args) {
System.out.println("This game is all about high value and Diamonds!"); System.out.println("You will be dealt a random card."); System.out.println("If the card is a diamond, or value over 10..."); System.out.println("You win!"); System.out.println("Otherwise, you lose."); System.out.println(); System.out.print("Would you like to play? "); String play = TextIO.getlnString(); if (play == ("No") || play == ("N")) { System.out.print("Maybe next time!"); } else if (play != "Yes" || (play != "Y") || (play != "No" || play != "N")) { System.out.print("Please enter yes or no: "); play = TextIO.getlnString(); } if (play == ("Yes") || play == ("Y")) { deal(); { } } }// end of Main class
private static void deal() { int wins = 0; int losses = 0; Deck deck = new Deck(); Card currentCard; deck.shuffle(); currentCard = deck.dealCard(); System.out.println("The card is the " + currentCard);
if (currentCard.getValue() > 9) { System.out.println("Congratulations! You win!"); wins++; System.out.println("You won " + wins + " times."); System.out.println("You lost " + losses + " times."); } else if (currentCard.getSuit() == 3) { System.out.println("Congratulations! You win!"); wins++; System.out.println("You won " + wins + " times."); System.out.println("You lost " + losses + " times."); }
else { System.out.println("I'm sorry, you lose."); losses++; System.out.println("You won " + wins + " times."); System.out.println("You lost " + losses + " times."); } System.out.print("Play again?"); { String playAgain = TextIO.getlnString(); if (playAgain != "Yes" || (playAgain != "Y") || (playAgain != "No" || playAgain != "N")); System.out.print("Please enter yes or no:"); playAgain = TextIO.getlnString(); if (playAgain == ("No") || playAgain == ("N")); System.out.print("Maybe next time!"); System.exit(0); if (playAgain == ("Yes") || playAgain == ("Y")); deal(); } } }// end DiamondRush class
My goal is to have the program ask to play, run through the program one time, print if won or loss while keeping count of the win/loss, then ask if wants to play again. If yes to play again, run through main loop again, otherwise cancel program.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
