/** * Rolls a 6-sided die on behalf of every command-line argument, * printing the argument and its die roll value on its own line to * system output, and proclaiming the winner(s) and showing the * total of all of the rolls to the screen <br /> <br /> * * uses: class GameDie <br /> <br /> * * @author: Sharon Tuttle * @version: 2021-12-03 */ public class DiceRoller { // data fields private static final int NUM_DIE_SIDES = 6; /** * rolls a 6-sided dice on behalf of every command-line argument, * printing argument and its dice roll value on its own line to * system output, and proclaiming the winner(s) and showing the * total of all of the rolls to the screen * * @param args contains the players for this game instance */ public static void main(String[] args) { GameDie myDie = new GameDie(NUM_DIE_SIDES); // if called with no arguments, complain and exit if (args.length == 0) { System.err.println("Usage: DiceRoller should be called with " + "at least one command-line argument"); System.exit(1); } // if get here --- MUST be at least one command line argument // set up variables needed to keep track of everything String[] players = new String[args.length]; int[] playerRolls = new int[args.length]; int maxRoll = 0; // impossibly low, so WILL be reset int latestRoll; int sumOfRolls = 0; System.out.println(""); // roll die for each player, keeping track of rolls so // can determine winner(s) for (int i = 0; i < args.length; i++) { players[i] = args[i]; latestRoll = myDie.roll(); playerRolls[i] = latestRoll; sumOfRolls += latestRoll; System.out.println(args[i] + "'s roll is: " + latestRoll); // do we have a new max-roll-so-far? if (latestRoll > maxRoll) { maxRoll = latestRoll; } } // now determine the winner(s) System.out.println("\n...and the winner(s) is/are..."); for (int i=0; i < args.length; i++) { if (playerRolls[i] == maxRoll) { System.out.println(" " + players[i] ); } } System.out.println("... with a roll of " + maxRoll + ",\n"); System.out.println("and the total of all the rolls is " + sumOfRolls + "\n"); } }