Advertisement
Foxxything

Untitled

Dec 9th, 2022
1,039
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.75 KB | None | 0 0
  1. /** Required package class namespace */
  2. package Foxx;
  3.  
  4. import java.awt.Color;
  5. import java.awt.Font;
  6. import javax.swing.Icon;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JOptionPane;
  9. import javax.swing.JTextArea;
  10.  
  11.  
  12. /*
  13.  * GolfIsBad - description
  14.  *
  15.  * @author Foxx Azalea Pinkerton
  16.  * @since Dec 6, 2022
  17.  * @teacher Mr.Wachs
  18.  */
  19. public class GolfIsBad {
  20.  
  21.   static final String TITLE = "Golf Simulator";
  22.   static final Font FONT = new Font("Comic Sans MS", Font.PLAIN, 12);
  23.   static final Color BACKGROUND = new Color(238, 238, 238);
  24.   static final Color FOREGROUND = new Color(0, 0, 0);
  25.  
  26.   static final int PLAYERS = 4;
  27.   static final int HOLES   = 9;
  28.  
  29.   /**
  30.    * Main method for the project
  31.    *
  32.    * @param args the command line arguments
  33.    */
  34.   public static void main(String[] args) {
  35.     start();
  36.     program();
  37.     end();
  38.   }
  39.  
  40.   /**
  41.    * Starts the application and welcomes user with a dialog
  42.    */
  43.   private static void start() {
  44.     // Call output to generate a dialog and attach the title global variable
  45.     output("Welcome to " + TITLE);
  46.   }
  47.  
  48.   /**
  49.    * Ends the application with a dialog and terminates the application
  50.    */
  51.   private static void end() {
  52.     // Call output dialog and attach global title then terminate the app
  53.     output("Thanks for using " + TITLE);
  54.     System.exit(0);                                 // Terminates the app
  55.   }
  56.  
  57.   /**
  58.    * Ask the user a yes and no question, in a confirm dialog box
  59.    *
  60.    * @param text the yes no question to ask in the dialog
  61.    * @return true (yes), false (no)
  62.    */
  63.   private static boolean yesNo(String text) {
  64.     // Create graphical display area with formatted text to put in dialog
  65.     JTextArea area = formatArea(text);
  66.     // Store the user's response in a variable from what they clicked on
  67.     // when the dialog appears only showing "yes" and "no" buttons for
  68.     // the user to choose from  
  69.     int response = JOptionPane.showConfirmDialog(null, area, TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE);
  70.     if (response == JOptionPane.YES_OPTION) {
  71.       return true;
  72.     } else {
  73.       return false;
  74.     }
  75.   }
  76.  
  77.   /**
  78.    * Asks the user for a string input in a input dialog box
  79.    *
  80.    * @param text the text for the dialog box
  81.    * @return a valid string
  82.    */
  83.   private static String input(String text) {
  84.     // Create graphical display area with formatted text to put in dialog
  85.     JTextArea area = formatArea(text);
  86.     // Store the user's response in a variable from what they typed into
  87.     // a input dialog
  88.     String value = JOptionPane.showInputDialog(null, area, TITLE, JOptionPane.PLAIN_MESSAGE);
  89.     // Create an error message if they user did not enter a value correctly
  90.     final String ERROR = "Error, please enter again\n\n";
  91.     // Force a loop if the user left the dialog empty and clicked "ok" or
  92.     // they clicked "cancel" or the "X"
  93.     while (value == null || value.equals("")) {
  94.       // Recreate the graphical display area
  95.       area = formatArea(ERROR + text);
  96.       value = JOptionPane.showInputDialog(null, area, TITLE, JOptionPane.PLAIN_MESSAGE);
  97.     }
  98.     return value;           // Once they have entered a value, return it
  99.   }
  100.  
  101.   /**
  102.    * Asks the user for a number (integer) in a input dialog box
  103.    *
  104.    * @param text the text for the dialog box
  105.    * @return a valid integer
  106.    */
  107.   private static int inputInteger(String text) {
  108.     String value = input(text);     // Get user's response
  109.     // Create an error message if they user did not enter a value correctly
  110.     final String ERROR = "Error, please enter again\n\n";
  111.     // Force a loop if the user did not enter a valid integer
  112.     while (isInteger(value) == false) {
  113.       value = input(ERROR + text);
  114.     }
  115.     int number = Integer.parseInt(value);       // Convert to integer
  116.     return number;                              // Return number value
  117.   }
  118.  
  119.   /**
  120.    * Asks the user for a number (double) in a input dialog box
  121.    *
  122.    * @param text the text for the dialog box
  123.    * @return a valid integer
  124.    */
  125.   private static double inputDouble(String text) {
  126.     String value = input(text);     // Get user's response
  127.     // Create an error message if they user did not enter a value correctly
  128.     final String ERROR = "Error, please enter again\n\n";
  129.     // Force a loop if the user did not enter a valid double
  130.     while (isDouble(value) == false) {
  131.       value = input(ERROR + text);
  132.     }
  133.     double number = Double.parseDouble(value);      // Convert to double
  134.     return number;                                  // Return number value
  135.   }
  136.  
  137.   /**
  138.    * Checks the value to see if it contains numerical characters or a "-"
  139.    *
  140.    * @param value the string of characters to check
  141.    * @return is a number (true) or not (false)
  142.    */
  143.   private static boolean isInteger(String value) {
  144.     // Convert string into array of char values (built-in string method)
  145.     char[] characters = value.toCharArray();
  146.     // Loop (traverse) through this array from start to end
  147.     for (int i = 0; i < characters.length; i++) {
  148.       // Read in the character at this index
  149.       char character = characters[i];
  150.       // Use built-in method to see if this character is a digit (0-9)
  151.       if (Character.isDigit(character) == false) {
  152.         // Character was not a digit, but is it the negative sign
  153.         if (characters[i] != '-') {
  154.           return false;       // Not a valid character for a integer
  155.         }
  156.       }
  157.     }
  158.     return true;                    // All characters valid for a integer
  159.   }
  160.  
  161.   /**
  162.    * Checks the value to see if it contains numerical characters or a "-" or a
  163.    * "."
  164.    *
  165.    * @param value the string of characters to check
  166.    * @return is a number (true) or not (false)
  167.    */
  168.   private static boolean isDouble(String value) {
  169.     // Convert string into array of char values (built-in string method)
  170.     char[] characters = value.toCharArray();
  171.     // Loop (traverse) through this array from start to end
  172.     for (int i = 0; i < characters.length; i++) {
  173.       // Read in the character at this index
  174.       char character = characters[i];
  175.       // Use built-in method to see if this character is a digit (0-9)
  176.       if (Character.isDigit(character) == false) {
  177.         // Character was not a digit, but is it the negative sign or
  178.         // the decimal symbol
  179.         if (characters[i] != '.'
  180.                 && characters[i] != '-') {
  181.           return false;       // Not a valid character for a double
  182.         }
  183.       }
  184.     }
  185.     return true;                    // All characters valid for a double
  186.   }
  187.  
  188.   /**
  189.    * Outputs text in a more visual graphical dialog
  190.    *
  191.    * @param text the text to display
  192.    */
  193.   private static void output(String text) {
  194.     // Create graphical display area with formatted text to put in dialog
  195.     JTextArea area = formatArea(text);
  196.     // Add the display area to the dialog to show the user
  197.     JOptionPane.showMessageDialog(null, area, TITLE,
  198.             JOptionPane.PLAIN_MESSAGE);
  199.   }
  200.  
  201.   /**
  202.    * Outputs text in a more visual graphical dialog
  203.    *
  204.    * @param text the text to display
  205.    * @param imageName the name of the image to display
  206.    */
  207.   private static void output(String text, String imageName) {
  208.     // Create graphical display area with formatted text to put in dialog
  209.     JTextArea area = formatArea(text);
  210.     // Create a icon picture from the name (and path) to an image file
  211.     Icon picture = new ImageIcon(imageName);
  212.     // Add the display area and icon to the dialog to show the user
  213.     JOptionPane.showMessageDialog(null, area, TITLE,
  214.             JOptionPane.PLAIN_MESSAGE, picture);
  215.   }
  216.  
  217.   /**
  218.    * Sets up a fancy display area for the text to display
  219.    *
  220.    * @param text the string of text to format
  221.    * @return the formatted text area for display
  222.    */
  223.   private static JTextArea formatArea(String text) {
  224.     // Create a graphics object to display the graphical things
  225.     JTextArea area = new JTextArea();
  226.     // Assign the global variable font, colors, and passed text to the area
  227.     area.setFont(FONT);
  228.     area.setBackground(BACKGROUND);
  229.     area.setForeground(FOREGROUND);
  230.     area.setText(text);
  231.     return area;
  232.   }
  233.  
  234.   /**
  235.    * Ask the user if they want to play again, in a dialog box
  236.    *
  237.    * @return true (yes, play again), false (no)
  238.    */
  239.   private static boolean playAgain() {
  240.     // Send the yesNo method the play again message
  241.     return yesNo("Do you want to play again?");
  242.   }
  243.  
  244.   /**
  245.    * Determines if a number if odd or even
  246.    *
  247.    * @param number the number to check
  248.    * @return true (if even), false (if odd)
  249.    */
  250.   private static boolean isEven(int number) {
  251.     if (number % 2 == 0) {
  252.       return true;   // Number divides evenly by zero
  253.     } else {
  254.       return false;  // Number does not divide by zero
  255.     }
  256.   }
  257.  
  258.   /**
  259.    * Checks to make sure the number is in the range
  260.    *
  261.    * @param number the number to check
  262.    * @param low lowest in the range
  263.    * @param high highest in the range
  264.    * @return in range (true) or not (false)
  265.    */
  266.   private static boolean inRange(int number, int low, int high) {
  267.     if (number >= low && number <= high) {
  268.       return true;
  269.     }
  270.     return false;
  271.   }
  272.  
  273.   /**
  274.    * Generate a random number (a double) in a range
  275.    *
  276.    * @param low the lowest double in the range
  277.    * @param high the highest double in the range
  278.    * @return random double in the range
  279.    */
  280.   private static double random(double low, double high) {
  281.     return (high - low + 1d) * Math.random() + low;
  282.   }
  283.  
  284.   /**
  285.    * Generate a random number (an integer) in a range
  286.    *
  287.    * @param low the lowest integer in the range
  288.    * @param high the highest integer in the range
  289.    * @return random integer in the range
  290.    */
  291.   private static int random(int low, int high) {
  292.     return (int) (random((double) low, (double) high));
  293.   }
  294.  
  295.   /**
  296.    * Generates a random character in a range
  297.    *
  298.    * @param low lowest character in the range
  299.    * @param high highest character in the range
  300.    * @return random character in range
  301.    */
  302.   private static char random(char low, char high) {
  303.     return (char) random((int) low, (int) high);     // cast to int and back  
  304.   }
  305.  
  306.   /**
  307.    * Presents an input dialog with a drop down selection of options
  308.    *
  309.    * @param text the text to show in the dialog
  310.    * @param options the array of option to have in the drop down
  311.    * @return the option they choose
  312.    */
  313.   private static String choices(String text, String[] options) {
  314.     // Create graphical display area with formatted text to put in dialog
  315.     JTextArea area = formatArea(text);
  316.     // Add display area to dialog to show user which gives a drop-down
  317.     // in the dialog and returns an "object"
  318.     Object object = JOptionPane.showInputDialog(null, area, TITLE,
  319.             JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
  320.     if (object == null) {
  321.       return "";                 // User selects "X"
  322.     } else {
  323.       return object.toString();  // Return user's choice
  324.     }
  325.   }
  326.  
  327.   /**
  328.    * Generates an array of random integers in the range between low and high
  329.    *
  330.    * @param low the lowest number in the range
  331.    * @param high the highest number in the range
  332.    * @param size the size to make the array
  333.    * @return an array of random integers
  334.    */
  335.   private static int[] random(int low, int high, int size) {
  336.     int[] numbers = new int[size];      // Create empty array of passed size
  337.     for (int i = 0; i < size; i++) {    // Traverse array
  338.       numbers[i] = random(low, high);  // Assign random value to each index
  339.     }
  340.     return numbers;                     // Return completed array
  341.   }
  342.  
  343.   /**
  344.    * Generates an array of random doubles in the range between low and high
  345.    *
  346.    * @param low the lowest number in the range
  347.    * @param high the highest number in the range
  348.    * @param size the size to make the array
  349.    * @return an array of random doubles
  350.    */
  351.   private static double[] random(double low, double high, int size) {
  352.     double[] numbers = new double[size]; // Create empty array
  353.     for (int i = 0; i < size; i++) {    // Traverse array
  354.       numbers[i] = random(low, high);  // Assign random value to each index
  355.     }
  356.     return numbers;                     // Return completed array
  357.   }
  358.  
  359.   /**
  360.    * Generates an array of random characters in the range between low and high
  361.    *
  362.    * @param low the lowest number in the range
  363.    * @param high the highest number in the range
  364.    * @param size the size to make the array
  365.    * @return an array of random characters
  366.    */
  367.   private static char[] random(char low, char high, int size) {
  368.     char[] numbers = new char[size];    // Create empty array
  369.     for (int i = 0; i < size; i++) {    // Traverse array
  370.       numbers[i] = random(low, high);  // Assign random value to each index
  371.     }
  372.     return numbers;                     // Return completed array
  373.   }
  374.  
  375.   /**
  376.    * Generates a matrix of random integers in the range between low and high
  377.    *
  378.    * @param low the lowest number in the range
  379.    * @param high the highest number in the range
  380.    * @param rows the number of rows (down) to make the matrix
  381.    * @param columns the number of columns (across) to make the matrix
  382.    * @return an array of random integers
  383.    */
  384.   private static int[][] random(int low, int high, int rows, int columns) {
  385.     int[][] matrix = new int[rows][columns];    // Create empty matrix
  386.     for (int row = 0; row < rows; row++) {                  // Travel rows
  387.       for (int column = 0; column < columns; column++) {  // and columns
  388.         matrix[row][column] = random(low, high);        // Assign value
  389.       }
  390.     }
  391.     return matrix;                              // Return completed matrix
  392.   }
  393.  
  394.   /**
  395.    * Generates a matrix of random doubles in the range between low and high
  396.    *
  397.    * @param low the lowest number in the range
  398.    * @param high the highest number in the range
  399.    * @param rows the number of rows (down) to make the matrix
  400.    * @param columns the number of columns (across) to make the matrix
  401.    * @return an array of random doubles
  402.    */
  403.   private static double[][] random(double low, double high, int rows, int columns) {
  404.     double[][] matrix = new double[rows][columns];    // Create empty matrix
  405.     for (int row = 0; row < rows; row++) {                  // Travel rows
  406.       for (int column = 0; column < columns; column++) {  // and columns
  407.         matrix[row][column] = random(low, high);        // Assign value
  408.       }
  409.     }
  410.     return matrix;                              // Return completed matrix
  411.   }
  412.  
  413.   /**
  414.    * Generates a matrix of random characters in the range between low and high
  415.    *
  416.    * @param low the lowest number in the range
  417.    * @param high the highest number in the range
  418.    * @param rows the number of rows (down) to make the matrix
  419.    * @param columns the number of columns (across) to make the matrix
  420.    * @return an array of random characters
  421.    */
  422.   private static char[][] random(char low, char high, int rows, int columns) {
  423.     char[][] matrix = new char[rows][columns];    // Create empty matrix
  424.     for (int row = 0; row < rows; row++) {                  // Travel rows
  425.       for (int column = 0; column < columns; column++) {  // and columns
  426.         matrix[row][column] = random(low, high);        // Assign value
  427.       }
  428.     }
  429.     return matrix;                              // Return completed matrix
  430.   }
  431.  
  432.   /**
  433.    * Formats the array into a string that could be outputted
  434.    *
  435.    * @param array the array to format
  436.    * @return a string of formatted text
  437.    */
  438.   private static String toString(int[] array) {
  439.     String text = "[";                          // Variable to store array
  440.     for (int i = 0; i < array.length - 1; i++) {  // Traverse array
  441.       text += array[i] + ",";                 // Attach index to variable
  442.     }
  443.     text += array[array.length - 1] + "]";        // Attach last index
  444.     return text;
  445.   }
  446.  
  447.   /**
  448.    * Formats the array into a string that could be outputted
  449.    *
  450.    * @param array the array to format
  451.    * @return a string of formatted text
  452.    */
  453.   private static String toString(double[] array) {
  454.     String text = "[";                          // Variable to store array
  455.     for (int i = 0; i < array.length - 1; i++) {  // Traverse array
  456.       text += array[i] + ",";                 // Attach index to variable
  457.     }
  458.     text += array[array.length - 1] + "]";        // Attach last index
  459.     return text;
  460.   }
  461.  
  462.   /**
  463.    * Formats the array into a string that could be outputted
  464.    *
  465.    * @param array the array to format
  466.    * @return a string of formatted text
  467.    */
  468.   private static String toString(char[] array) {
  469.     String text = "[";                          // Variable to store array
  470.     for (int i = 0; i < array.length - 1; i++) {  // Traverse array
  471.       text += array[i] + ",";                 // Attach index to variable
  472.     }
  473.     text += array[array.length - 1] + "]";        // Attach last index
  474.     return text;
  475.   }
  476.  
  477.   /**
  478.    * Formats the matrix into a string that could be outputted
  479.    *
  480.    * @param matrix the array to format
  481.    * @return a string of formatted text
  482.    */
  483.   private static String toString(int[][] matrix) {
  484.     String text = "";                          // Variable to store array
  485.     for (int row = 0; row < matrix.length; row++) {
  486.       for (int column = 0; column < matrix[row].length; column++) {
  487.         text += matrix[row][column] + " \t";
  488.       }
  489.       text += "\n";
  490.     }
  491.     return text;
  492.   }
  493.  
  494.   /**
  495.    * Formats the matrix into a string that could be outputted
  496.    *
  497.    * @param matrix the array to format
  498.    * @return a string of formatted text
  499.    */
  500.   private static String toString(double[][] matrix) {
  501.     String text = "";                          // Variable to store array
  502.     for (int row = 0; row < matrix.length; row++) {
  503.       for (int column = 0; column < matrix[row].length; column++) {
  504.         text += matrix[row][column] + " \t";
  505.       }
  506.       text += "\n";
  507.     }
  508.     return text;
  509.   }
  510.  
  511.   /**
  512.    * Formats the matrix into a string that could be outputted
  513.    *
  514.    * @param matrix the array to format
  515.    * @return a string of formatted text
  516.    */
  517.   private static String toString(char[][] matrix) {
  518.     String text = "";                          // Variable to store array
  519.     for (int row = 0; row < matrix.length; row++) {
  520.       for (int column = 0; column < matrix[row].length; column++) {
  521.         text += matrix[row][column] + " \t";
  522.       }
  523.       text += "\n";
  524.     }
  525.     return text;
  526.   }
  527.  
  528.   /**
  529.    * main game loop
  530.    */
  531.   private static void program() {
  532.     int[][] scores = new int[PLAYERS][HOLES];
  533.     final String OPTIONS[] = {
  534.       "Play a rounds of golf",
  535.       "Find the winner of a hole",
  536.       "Quit"
  537.     };
  538.    
  539.     do {
  540.       String scoreCard = generateScoreCard(scores);
  541.       String choice    = choices(scoreCard, OPTIONS);
  542.       if (choice.equals(OPTIONS[0])) playRound(scores);
  543.       else if (choice.equals(OPTIONS[1])) findWinner(scores);
  544.       else if (choice.equals(OPTIONS[2])) end();
  545.     } while (true);
  546.   }
  547.  
  548.   /**
  549.    * makes formatted text for the score card
  550.    * @param scores
  551.    * @return
  552.    */
  553.   private static String generateScoreCard(int[][] scores) {
  554.     String text = "\t";
  555.     for (int hole = 0; hole < HOLES; hole++) text += "\tHole " + (hole+1);
  556.    
  557.     text += "\n";
  558.     for (int player = 0; player < PLAYERS; player++) {
  559.       text += "Player " + (player+1) + ":\t";
  560.       for (int hole = 0; hole < HOLES; hole++) text += scores[player][hole] + "\t";
  561.       text += "\n";
  562.     }
  563.     text += "\nPlease make a selection from the options below. \n";
  564.     return text;
  565.   }
  566.  
  567.   /**
  568.    * Simulates a game
  569.    *
  570.    * @param scores
  571.    */
  572.   private static void playRound(int[][] scores) {
  573.     for (int player = 0; player < PLAYERS; player++) {
  574.       for (int hole = 0; hole < HOLES; hole++) {
  575.         scores[player][hole] = random(1, 9);
  576.       }
  577.     }
  578.     output("Round has been simulated!");
  579.   }
  580.  
  581.   /**
  582.    * Finds the winner of the game
  583.    * @param scores
  584.    */
  585.   private static void findWinner(int[][] scores) {
  586.     int hole = inputInteger("Enter the hole number");
  587.     if (!inRange(hole, 1, HOLES)) output("Hole " + hole + " is invalid!");
  588.     else {
  589.       hole--;
  590.       boolean tie = false;
  591.       int winner = 0;
  592.       int lowest = scores[0][hole];
  593.       for (int player = 1; player < PLAYERS; player++) {
  594.         if (scores[player][hole] < lowest) {
  595.           winner = player;
  596.           lowest = scores[player][hole];
  597.           tie = false;
  598.         } else if (scores[player][hole] == lowest) {
  599.           tie = true;
  600.         }
  601.       }
  602.       hole++;
  603.       winner++;
  604.       if (tie == true) output("Tie score of " + lowest + " on hole " + hole);
  605.       else output("Player " + winner + " won hole " + hole + " with a score of " + lowest);
  606.     }
  607.   }
  608.  
  609. }
  610.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement