Card shuffling game

7
Card Shuffling Game package card; 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 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() {

description

Card Shuffing Game in Java

Transcript of Card shuffling game

Page 1: Card shuffling game

Card Shuffling Game

package card;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 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"; } }

Page 2: Card shuffling game

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(); }} // end class Card

package card;public class Deck { private Card[] deck; private int cardsUsed; public Deck() { this(false); // Just call the other constructor in this class. } public Deck(boolean includeJokers) { if (includeJokers) deck = new Card[54]; else deck = new Card[52];

Page 3: Card shuffling game

int cardD = 0; for ( int suit = 0; suit <= 3; suit++ ) { for ( int value = 1; value <= 13; value++ ) { deck[cardD] = new Card(value,suit); cardD++; } } 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); } } // end class Deck

package card; import java.util.Scanner;import java.io.*;public class HighLow {Scanner input=new Scanner(System.in); public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.println("This program lets you play the simple card game,");

Page 4: Card shuffling 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 game = 0; int sum = 0; double averageScore; boolean plays; do { int scoreThisGame; scoreThisGame = play(); sum += scoreThisGame; game++; System.out.println("Play again?\n ''True'' for Yes\n '' false'' for No"); plays=input.nextBoolean(); } while (plays); averageScore = ((double)sum) / game; System.out.println(); System.out.println("You played " + game + " games."); System.out.printf("Your average score was %1.3f.\n", averageScore); } // end main() private static int play() { Deck deck = new Deck(); Card currentCard; Card nextCard; int correctGuesses ; char guess; String a; deck.shuffle(); correctGuesses = 0; currentCard = deck.dealCard(); System.out.println("The first card is the " + currentCard); while (true) { System.out.println("Will the next card be higher (H) or lower (L)? "); do { Scanner input=new Scanner(System.in); a= input.nextLine(); guess=a.charAt(0); 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);

Page 5: Card shuffling game

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; } // end play() } // end class