/* Java application that presents an example use of the DieBlackJack class. ** The use provides the "goal" (as a positive integer value) as either a ** run argument or in response to the prompt. ** ** CMPS 134 Fall 2019 ** Authors: P.M.J & R.W.M. */ //import java.util.*; import java.util.Scanner; public class DieBlackJackClient { public static void main(String[] args) { // Create a DieBlackJack object having the goal specified by // either a run argument or, absent one, the user. int goal = getIntArg(args,0,"Enter goal:>"); DieBlackJack DBJ = new DieBlackJack(goal); // Show the initial status of the DieBlackJack object. printStatus(DBJ, "Initially: "); //System.out.println("<<<<<<<<<<<<<<<<<"); // Have the DieBlackJack object keep taking turns as long as // the shouldTakeTurn() method says it is advisable to do so. // Show the object's status after each turn. System.out.println("Now playing ..."); while(DBJ.shouldTakeTurn()) { int rollValue = DBJ.takeTurn(); printStatus(DBJ, "+" + rollValue + " = "); } // Show the status of the DieBlackJack object at this point. //System.out.println(">>>>>>>>>>>>>>>>>"); //printStatus(DBJ, " "); //System.out.println(">>>>>>>>>>>>>>>>>"); // Show the relationship to the goal (short, equal, or exceeded). printRelationToGoal(DBJ); // Now have the DieBlackJack object take one more turn, just // to see what happens. (If it had already reached or exceeded // its goal, the takeTurn() method will throw an exception, // which will be caught here.) printHorizontalLine(); System.out.println("Taking one more turn ..."); try { int rollValue = DBJ.takeTurn(); printStatus(DBJ, "+" + rollValue + " = "); } catch (Exception e) { System.out.println(e); // +"\nContinuing ...\n"); //printStatus(DBJ, " "); } printHorizontalLine(); printStatus(DBJ, "Finally: "); // Finally, show the //System.out.println(">>>>>>>>>>>>>>>>>"); printRelationToGoal(DBJ); //System.out.println(DBJ.atGoal() + ", " + DBJ.exceededGoal()); System.out.println("Done!!!"); } private static void printStatus(DieBlackJack dbj, String prefix) { System.out.println(prefix + dbj.getRollTotal() + " of " + dbj.getGoal()); // + ": " + dbj.toString()); } /* Prints a message indicating the given DieBlackJack object's ** relationship to its goal. */ private static void printRelationToGoal(DieBlackJack dbj) { if (dbj.atGoal()) { System.out.println("At goal!"); } else if (dbj.exceededGoal()) { System.out.println("Exceeded goal"); } else { System.out.println("Short of goal"); } } /* Prints a horizontal line composed of hyphens. */ private static void printHorizontalLine() { System.out.println("------------------------"); } /* Returns args[index] converted to an int, assuming that args[] ** has length at least index+1 (so that such an element exists). ** Otherwise, it prints the given prompt and returns the user's ** response, converted to an int. */ private static int getIntArg(String args[], int index, String prompt) { int result = 0; if(args.length > index) { result = Integer.parseInt(args[index]); } else { System.out.print(prompt); result = new Scanner(System.in).nextInt(); } return result; } }