Skip to content

Instantly share code, notes, and snippets.

@Matthewacon
Created October 11, 2017 15:55
Show Gist options
  • Save Matthewacon/1153815e5b70e2e0f59f7653afc5d6fb to your computer and use it in GitHub Desktop.
Save Matthewacon/1153815e5b70e2e0f59f7653afc5d6fb to your computer and use it in GitHub Desktop.
My second assignment in ICS3UH
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]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment