/** A general driver program that uses a deck of shuffled cards for poker. * 1. Create deck of cards. * 2. Deal 5 of the cards. * 3. SORT the cards so we'll be able to classify the hand. * 4. Create a poker hand and classify it. * 5. Allow user to replace cards. Then, re-do the poker hand. */ import java.util.Scanner; public class Driver { /** main() - Let's shuffle the deck, and deal 5 cards. The user will * then be able to replace some cards from the hand. */ public static void main(String [] args) { // We'll need to scan for input later. Scanner kbd = new Scanner(System.in); // Create a shuffled deck of cards. Show all 52 cards so we can // see that they are shuffled correctly, and also to check that // we are dealing correctly later. (In a real game of course we would // not reveal all the cards like this.) Deck d = new Deck(); System.out.printf("Here are all the cards:\n%s\n", d.toString()); // Now that the cards have been shuffled, the game would continue // from here. Let's deal 5 cards. Card [] c = new Card[5]; for (int i = 0; i < 5; ++i) { c[i] = new Card(d.getNextCard()); } // We must sort the cards! sort(c); // Let's show the user the 5 cards dealt. printArray(c); // I made the choice when implementing Hand.java to make the // poker hand have 5 separate card objects instead of an array. // So, unfortunately, the Hand constructor expects 5 objects. // Classify the hand in a Hand function // so that there is less code here. Hand h = new Hand(c[0], c[1], c[2], c[3], c[4]); System.out.printf("You have: %s\n", h.classify()); // Time to replace System.out.printf("How many cards to replace? "); int numReplacements = kbd.nextInt(); for (int replace = 0; replace < numReplacements; ++replace) { System.out.printf("Which card to replace? (1-5): "); int victim = kbd.nextInt(); // need to get the index value into the range 0-4 victim = victim - 1; c[victim] = d.getNextCard(); // sort again! sort(c); // output printArray(c); h = new Hand(c[0], c[1], c[2], c[3], c[4]); System.out.printf("You have: %s\n", h.classify()); } } /** sort() - my own sort function for the 5 cards * Note this is a 'static' function. This means you don't need to * create an object in order to run it. If your Driver.java source file * needs more functions besides main(), they will proabably be static. */ public static void sort(Card [] c) { for (int i = 0; i < c.length; ++i) for (int j = i+1; j < c.length; ++j) { if (c[i].compareTo(c[j]) < 0) { Card temp = c[i]; c[i] = c[j]; c[j] = temp; } } } /** printArray() - Again, if there is some code that is repeated, it's * a good idea to 'factor it out' and create a function for it. In * this case, it turns out that there are 2 places in the code where * we want to print out the cards. */ public static void printArray(Card [] c) { for (int i = 0; i < 5; ++i) { System.out.printf("Card # %d is %s\n", i+1, c[i]); } } }