import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.util.Iterator; /* PlayTaxman.java ** This Java application allows a user to play a game of Taxman. ** Alternatively, if a command line argument is provided, it is taken ** to be the name of a file in which each line contains the "user inputs" ** for a game of Taxman. ** ** @author R. McCloskey ** @version April 2020 */ public class PlayTaxman { private static Scanner input; private static boolean echoInput; private static boolean gameAborted; public static void main(String[] args) throws FileNotFoundException { if (args.length != 0) { Scanner fileInput = new Scanner(new File(args[0])); echoInput = true; while (fileInput.hasNextLine()) { String line = fileInput.nextLine(); input = new Scanner(line); playOneGame(); System.out.println(); System.out.println("-----------------------------------------"); } } else { input = new Scanner(System.in); echoInput = false; gameAborted = false; playOneGame(); } } public static void playOneGame() { System.out.println("Welcome to the Taxman Game."); final int N = getInt("Enter the maximum income amount: "); Taxman taxman = new Taxman(N); gameAborted = false; while (!gameAborted && taxman.hasLegalChoices()) { reportState(taxman); int inc = getInt("\nChoose a number to add as income: "); if (!gameAborted) { if (taxman.isLegalChoice(inc)) { taxman.chooseAsIncome(inc); System.out.printf("%d added to income.\n", inc); } else { System.out.println("*** ERROR ***: invalid choice; try again"); } } } if (gameAborted) { System.out.println("*** GAME ABORTED ***"); } else { System.out.println("\nNo choices remain."); System.out.println("Before paying residual taxes:"); reportState(taxman); taxman.payResidualTaxes(); System.out.println("\nAfter paying residual taxes:"); reportState(taxman); reportOutcome(taxman); } } /* Displays the current state of the game, including total ** income earned, taxes paid, and the remaining active numbers. */ private static void reportState(Taxman taxman) { System.out.printf("Income: %s\n", taxman.incomesImage()); System.out.printf("Taxes: %s\n", taxman.taxesImage()); System.out.println("Current score:"); System.out.printf(" Income: %d; Taxes Paid: %d\n", taxman.incomeEarned(), taxman.taxesPaid()); System.out.print("\nActive numbers: "); System.out.println(taxman.activeNumbersImage()); } /* Reports the outcome (win/lose/draw) of the given game ** (which is assumed to be over). ** pre: !taxman.hasLegalChoices() */ private static void reportOutcome(Taxman taxman) { if (taxman.incomeEarned() > taxman.taxesPaid()) { System.out.println("\nYou won!"); } else if (taxman.incomeEarned() < taxman.taxesPaid()) { System.out.println("\nYou lost."); } else { System.out.println("\nGame ended in a draw."); } } /* Displays the given prompt and returns the user's response, ** interpreted to be an integer value. */ private static int getInt(String prompt) { int response = -1; System.out.print(prompt); if (!input.hasNextInt()) { gameAborted = true; } else { response = input.nextInt(); if (echoInput) { System.out.printf("%d\n", response); } } return response; } }