Skip to content

Instantly share code, notes, and snippets.

@Matthewacon
Last active October 20, 2017 16:18

Revisions

  1. Matthewacon revised this gist Oct 20, 2017. 1 changed file with 115 additions and 20 deletions.
    135 changes: 115 additions & 20 deletions GuessingGame.java
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,117 @@
    /**Pseudocode...
    bestGame[] = null
    worstGame[] = null
    avgGuesses=0
    totalGuesses=0
    BEGIN_METHOD calculateGuessStatistics List<Integer[]> guessStatistics
    bestGame = guessStatistics.get(0)
    worstGame = guessStatistics.get(0)
    I = 0
    sum = 0
    loop while I < guessStatistics.size()
    sum += guessStatistics.get(I)[0]
    if (I+1) = guessStatistics.size() then
    avgGuesses = Math.floor(10 * (sum / (I + 1.0)))
    totalGuesses=sum
    end if
    if guessStatistics.get(I)[0] < bestGame[0] then
    bestGame = guessStatistics.get(I)
    end if
    if guessStatistics.get(I)[0] > worstGame[0] then
    worstGame = guessStatistics.get(I)
    end if
    end loop
    END_METHOD calculateGuessStatistics
    BEGIN_METHOD main args[]
    output "Welcome to Matthew Barichello's guessing game!"
    List<Integer[]> games = {}
    GameLoop while true
    output "Please enter a number between 1 and 100:"
    guessMe = new Random().ints(1, 100).iterator().next()
    Integer guessed = null
    attempts = 0
    gameData[1] = guessMe
    GuessLoop while guessed != guessMe
    attempts = attempts + 1
    input STRGUESSED
    try
    guessed = Integer.parseInt(STRGUESSED)
    catch (NumberFormatException e) then
    output "A NUMBER between 1 and 100..."
    continue GuessLoop
    end try
    if guessed > 100 OR 1 > guessed then
    output "A number between 1 and 100..."
    continue GuessLoop
    end if
    if guessed < guessMe then
    output "Try a higher number..."
    else
    output "Try a lower number..."
    end if
    end GuessLoop
    gameData[0] = attempts
    games.add(gameData)
    CONGRATULATE_USER "Right on, and it only took you " + attempts + "guess"
    if attempts > 1 then
    CONGRATULATE_USER = CONGRATULATE_USER + "es!"
    else
    CONGRATULATE_USER = CONGRATULATE_USER + "!"
    end if
    output CONGRATULATE_USER
    output "Would you like to play another round?"
    input PLAY_AGAIN
    if PLAY_AGAIN.equalsIgnoreCase("y") then
    continue GameLoop
    else
    break GameLoop
    end if
    end GameLoop
    Main.calculateGuessStatistics(games)
    output "Well, that's alright. Here are your stats:"
    output "\tNumber of games played: " + games.size()
    output "\tTotal number of guesses: " + totalGuesses
    output "\tAverage number of guesses per game: " + (avgGuesses / 10.0) + "\n"
    output "Best game:"
    output "\tGuesses: " + bestGame[0] + "\n\tGenerated number: " + bestGame[1]
    if games.size() > 1
    output "\nWorst game: "
    output "\tGuesses: " + worstGame[0] + "\n\tGenerated number: " + worstGame[1]
    end if
    END_METHOD main
    */

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.stream.IntStream;

    public class GuessingGame {
    public class Main {
    private static Integer[] bestGame, worstGame;
    private static int avgGuesses, totalGuesses;

    public static void calculateGuessStatistics(List<Integer[]> guessStatistics) {
    //Default values, starts at the first game.
    bestGame = guessStatistics.get(0);
    worstGame = guessStatistics.get(0);
    //Calculate average number of guesses, determine best game and calculate the total number of guesses
    for (int i = 0, sum = 0; i < guessStatistics.size(); i++) {
    sum += guessStatistics.get(i)[0];
    //Average and sum
    if (i + 1 == guessStatistics.size()) {
    avgGuesses = (int) Math.floor(10 * (sum / (i + 1.0)));
    totalGuesses = sum;
    }
    //Best game
    if (guessStatistics.get(i)[0] < bestGame[0]) bestGame = guessStatistics.get(i);
    //Worst game
    if (guessStatistics.get(i)[0] > worstGame[0]) worstGame = guessStatistics.get(i);
    }
    }

    public static void main(String[] args) {
    System.out.println("Welcome to Matthew Barichello's guessing game!");
    /**The index of an <code>Integer[]</code> in the <code>List games</code> coincides with the game number.
    @@ -18,12 +125,12 @@ public static void main(String[] args) {
    List<Integer[]> games = new ArrayList<>();
    //Closes the Scanner automatically
    try (Scanner scanner = new Scanner(System.in)) {
    scanner.useDelimiter("\n");
    GameLoop:while (true) {
    System.out.println("Please enter a number between 1 and 100:");
    Integer guessMe, guessed = null, attempts = 0;
    //Declare and initialize the array that holds the information for this round
    Integer[] gameData = new Integer[2];
    scanner.useDelimiter("\n");
    //Closes the IntStream automatically
    try (IntStream is = new Random().ints(1, 100)) {
    //Get the random number
    @@ -52,29 +159,17 @@ public static void main(String[] args) {
    }
    }
    }
    //Default values, starts at the first game.
    Integer[] bestGame = games.get(0), worstGame = games.get(0);
    int avgGuesses = 0, totalGueses = 0;
    //Calculate average number of guesses, determine best game and calculate the total number of guesses
    for (int i = 0, sum = 0; i < games.size(); i++) {
    sum += games.get(i)[0];
    //Average and sum
    if (i + 1 == games.size()) {
    avgGuesses = (int) Math.floor(10 * (sum / (i + 1.0)));
    totalGueses = sum;
    }
    //Best game
    if (games.get(i)[0] < bestGame[0]) bestGame = games.get(i);
    //Worst game
    if (games.get(i)[0] > worstGame[0]) worstGame = games.get(i);
    }

    //Generate game statistics
    calculateGuessStatistics(games);

    //Print user statistics
    System.out.println("Well, that's alright. Here are your stats:");
    System.out.println("\tNumber of games played: " + games.size());
    System.out.println("\tTotal number of guesses: " + totalGueses);
    System.out.println("\tTotal number of guesses: " + totalGuesses);
    System.out.println("\tAverage number of guesses per game: " + avgGuesses / 10f + "\n");
    System.out.println("Best game:");
    System.out.println("\tGuesses: " + bestGame[0] + "\n\tGenerated number: " + bestGame[1] + "");
    System.out.println("\tGuesses: " + bestGame[0] + "\n\tGenerated number: " + bestGame[1]);
    if (games.size() > 1) {
    System.out.println("\nWorst game:");
    System.out.println("\tGuesses: " + worstGame[0] + "\n\tGenerated number: " + worstGame[1]);
  2. Matthewacon created this gist Oct 11, 2017.
    83 changes: 83 additions & 0 deletions GuessingGame.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    import java.util.Scanner;
    import java.util.stream.IntStream;

    public class GuessingGame {
    public static void main(String[] args) {
    System.out.println("Welcome to Matthew Barichello's guessing game!");
    /**The index of an <code>Integer[]</code> in the <code>List games</code> coincides with the game number.
    * ie. <code>games.get(0)</code> would return an <code>Integer[]<code/> corresponding to the first game.
    *
    * The indices in the stored <code>Integer[]<code/>s, correspond to specific statistics:
    * <code>[0]</code> is the number of guesses in each game and <code>[1]</code> is the computer generated
    * number.
    *
    */
    List<Integer[]> games = new ArrayList<>();
    //Closes the Scanner automatically
    try (Scanner scanner = new Scanner(System.in)) {
    GameLoop:while (true) {
    System.out.println("Please enter a number between 1 and 100:");
    Integer guessMe, guessed = null, attempts = 0;
    //Declare and initialize the array that holds the information for this round
    Integer[] gameData = new Integer[2];
    scanner.useDelimiter("\n");
    //Closes the IntStream automatically
    try (IntStream is = new Random().ints(1, 100)) {
    //Get the random number
    gameData[1] = (guessMe = is.iterator().next());
    GuessLoop: do {
    attempts++;
    try {
    guessed = Integer.parseInt(scanner.next());
    } catch (NumberFormatException e) {
    System.out.println("A NUMBER between 1 and 100...");
    continue GuessLoop;
    }
    if (guessed > 100 || 1 > guessed) {
    System.out.println("A number between 1 and 100...");
    continue GuessLoop;
    }
    System.out.println("Try a " + (guessed < guessMe ? "higher" : "lower") + " number...");
    } while (guessed != guessMe);
    gameData[0] = attempts;
    games.add(gameData);
    System.out.println("Right on, and it only took you " + attempts + " guess" +
    (attempts > 1 ? "es" : "") + "!");
    System.out.println("Would you like to play another round?");
    if (scanner.next().equalsIgnoreCase("y")) continue GameLoop;
    else break GameLoop;
    }
    }
    }
    //Default values, starts at the first game.
    Integer[] bestGame = games.get(0), worstGame = games.get(0);
    int avgGuesses = 0, totalGueses = 0;
    //Calculate average number of guesses, determine best game and calculate the total number of guesses
    for (int i = 0, sum = 0; i < games.size(); i++) {
    sum += games.get(i)[0];
    //Average and sum
    if (i + 1 == games.size()) {
    avgGuesses = (int) Math.floor(10 * (sum / (i + 1.0)));
    totalGueses = sum;
    }
    //Best game
    if (games.get(i)[0] < bestGame[0]) bestGame = games.get(i);
    //Worst game
    if (games.get(i)[0] > worstGame[0]) worstGame = games.get(i);
    }
    //Print user statistics
    System.out.println("Well, that's alright. Here are your stats:");
    System.out.println("\tNumber of games played: " + games.size());
    System.out.println("\tTotal number of guesses: " + totalGueses);
    System.out.println("\tAverage number of guesses per game: " + avgGuesses / 10f + "\n");
    System.out.println("Best game:");
    System.out.println("\tGuesses: " + bestGame[0] + "\n\tGenerated number: " + bestGame[1] + "");
    if (games.size() > 1) {
    System.out.println("\nWorst game:");
    System.out.println("\tGuesses: " + worstGame[0] + "\n\tGenerated number: " + worstGame[1]);
    }
    }
    }