Skip to content

Instantly share code, notes, and snippets.

@Matthewacon
Last active October 20, 2017 16:18
Show Gist options
  • Save Matthewacon/c0bb4e0682bbbb1e78074a5614e056f4 to your computer and use it in GitHub Desktop.
Save Matthewacon/c0bb4e0682bbbb1e78074a5614e056f4 to your computer and use it in GitHub Desktop.
ICS3UH Second assignment...
/**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 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.
* 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)) {
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];
//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;
}
}
}
//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: " + 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]);
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