package class10_2_ArrayLists; import java.util.Random; import java.util.Scanner; public class Card { private static final int MAX_VALUE = 13; private static int nextValue = 7; private static Random generator = new Random(); // defaults to false private static boolean[] isChosen = new boolean[MAX_VALUE]; private int value; private String suit; /** * Create next card in sequence */ public Card() { this(nextValue,"Hearts"); isChosen[nextValue-1] = true; // TODO: Fix this so it doesn't run forever // if we run out of cards! while( isChosen[nextValue-1] ) { /* nextValue has already been selected */ nextValue = generator.nextInt(MAX_VALUE)+1; } } /** * Create a card from a string representation formatted like these examples: * Ace of Spades * 4 of Hearts * King of Diamonds * * Spelled-out numbers (e.g. Four) are not allowed. * @param displayName the string to be converted to a Card object. */ public Card(String displayName) { Scanner stringScanner = new Scanner(displayName); String stringNumber = stringScanner.next(); if(stringNumber.equals("Ace")) { this.value = 1; } else if(stringNumber.equals("King")) { System.out.println("DEBUG: Here."); System.out.println("DEBUG: We are setting this.value = 13 for King."); this.value = 13; System.out.println("DEBUG: this.value: "+this.value); } else if(stringNumber.equals("Queen")) { this.value = 12; } else if(stringNumber.equals("Jack")) { this.value = 11; } else { Scanner valueScanner = new Scanner(stringNumber); if(!valueScanner.hasNextInt()) { System.out.println("Warning: "+stringNumber+" is an unrecognized non-numeric card value. Using Ace"); this.value = 1; } else { int value = valueScanner.nextInt(); if(value < 0 || value > MAX_VALUE) { System.out.println("Warning: Card.Card: Invalid card value. Using Ace instead."); this.value = 1; } else { this.value = value; } } } if(!stringScanner.hasNext() || !stringScanner.next().equals("of")) { System.out.println("Warning: Second word of type expected to be \"of\". Continuing..."); } if(!stringScanner.hasNext()) { System.out.println("Suite missing. Using Hearts"); this.suit = "Hearts"; } else { String suit = stringScanner.next(); if(!suit.equals("Hearts") && !suit.equals("Diamonds") &&!suit.equals("Clubs") && !suit.equals("Spades")) { System.out.println("Warning: Card.Card. Invalid suit. Using Hearts"); this.suit = "Hearts"; } else { this.suit = suit; } } } /** * Create a card with the specified value and suit * * @param value the value * @param suit the suit */ public Card(int value, String suit) { if(value < 1 || value > MAX_VALUE) { System.out.println("Warning: Card.Card: invalid value. Using Ace instead"); this.value = 1; } else { this.value = value; } if(!suit.equals("Hearts") && !suit.equals("Diamonds") &&!suit.equals("Clubs") && !suit.equals("Spades")) { System.out.println("Warning: Card.Card. Invalid suit. Using Hearts"); this.suit = "Hearts"; } else { this.suit = suit; } } /** * Return a string in a format like * * Ace of Diamonds * 5 of Hearts * King of Spades * etc. * @return the formatted string. */ public String toString() { String valueString; if(value < 0) { System.out.println("Warning: Card.toString: Invalid value!"); valueString = null; } else if(value == 1) { valueString = "Ace"; } else if(value <= 10) { valueString = Integer.toString(value); } else if(value == 11) { valueString = "Jack"; } else if(value == 12) { valueString = "Queen"; } else if(value == 13) { valueString = "King"; } else { System.out.println("Warning: Card.toString: Invalid value: "+value); valueString = null; } return valueString+" of "+suit; } /** * Test the Card class * @param ignored */ public static void main(String[] ignored) { // Card c = new Card("Bob"); // c.toString(); // testNumericConstructor(); testStringConstructor(); } /** * Test the constructor taking numeric arguments. */ public static void testNumericConstructor() { Card c; c = new Card(1,"Spades"); System.out.println(c); c = new Card(5,"Diamonds"); System.out.println(c); c = new Card(11,"Hearts"); System.out.println(c); c = new Card(15,"Clubs"); System.out.println(c); c = new Card(13,"Scissors"); System.out.println(c); } /** * Test the constructor using a formatted string argument. */ public static void testStringConstructor() { System.out.println("The following should be good:"); Card c; c = new Card("Ace of Spades"); System.out.println(c); c = new Card("2 of Diamonds"); System.out.println(c); c = new Card("5 of Hearts"); System.out.println(c); c = new Card("King of Spades"); System.out.println(c); System.out.println(); System.out.println("The following should not be good:"); c = new Card("King o' Spades"); System.out.println(c); c = new Card("Five of Clubs"); // Unsupported System.out.println(c); c = new Card("5 of Cubes"); // Unsupported System.out.println(c); } /** * @return true if the two cards are equal, and false if they are not * they are equal if they have the same value and suit. */ public boolean equals(Object o) { Card c = (Card) o; boolean result; // if(c == null) { // result = false; // } else { // result = // } return c != null && this.value == c.value & this.suit.equals(c.suit); } }