Skip to content

Instantly share code, notes, and snippets.

Created September 13, 2016 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/b87ebdb206befd6f18ba5274de6b0897 to your computer and use it in GitHub Desktop.
Save anonymous/b87ebdb206befd6f18ba5274de6b0897 to your computer and use it in GitHub Desktop.
/*
Created by Zachary Wander on 9/7/2016.
*/
package com.zachary.numbergame;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Random;
import static java.lang.Thread.sleep;
public class numbergame {
//main function
public static void main(String[] args) {
while (true) { //keep game going
try {
Scanner in = new Scanner(System.in); //setup user input
//intro
System.out.println("Welcome to the Number Games.");
System.out.println("Do you want to:");
System.out.println("A) Guess a randomly generated number (1-100)?");
System.out.println("B) Have the computer guess a number you come up with, within a range you specify?");
System.out.println("C) Quit?");
System.out.println("Choice:");
String choice = in.nextLine(); //take user input
//handle choice and function passing
if (choice.toUpperCase().equals("A")){
humanGuess();
}
else if (choice.toUpperCase().equals("B")){
systemGuess();
}
else if (choice.toUpperCase().equals("C")){
System.out.println("BYE!");
break;
}
else {
System.out.println("Something happened!"); //so I know if there's an error
}
}
finally {} //this needs to be here for something
}
}
//the number guessing function
public static void humanGuess() {
Scanner in = new Scanner(System.in); //setup user input
int number = (int) (Math.random() * 100) + 1; //generate a random number from 1-100
int guess; //define guess variable
int counter = 0; //to count the guesses
//intro
System.out.println("Guess a number between 1 and 100 (inclusive)");
System.out.println("You have 7 guesses.");
System.out.println(number); //DEBUG PURPOSES REMOVE
while (true) {
try {
if (counter != 0) { //only tell the guess count after the user has guessed
System.out.println("You have guessed: " + counter + " time(s).");
}
//if number of guesses is reached, quit
if (counter >= 7) {
System.out.println("Sorry, you've used all your guesses.");
System.out.println("The number was: " + number);
System.out.println("Press Enter to continue."); //pretty self explanatory
in.nextLine();
break;
}
System.out.println("Your guess:"); //get guess
guess = in.nextInt();
in.nextLine(); //this fixes something when exiting
if ((guess < 1) || (guess > 100)) { //if the guess is out of bounds, tell the user
System.out.println("\n");
System.out.println("Read the instructions.");
System.out.println("Press Enter to continue."); //pretty self explanatory
}
if (guess > number) {
System.out.println("It's lower."); //if the guess is too high, tell user
} else if (guess < number) {
System.out.println("It's Higher"); //if the guess is too low, tell user
} else if (guess == number) {
counter = counter + 1; //since it counts up at the end of the loop
System.out.println("Correct!");
System.out.println("Guesses:" + counter);
System.out.println("Press Enter to continue."); //pretty self explanatory
in.nextLine(); //I think this needs to be here
break;
}
else {}
counter = counter + 1; //raise the guess counter
} catch (InputMismatchException e) {//if user doesn't enter an integer, complain
System.out.println("Unusable input. Please use integers.");
System.out.println("Press Enter to continue."); //pretty self explanatory
in.nextLine(); //dump the bad input
break;
}
}
}
public static void systemGuess() {
Scanner in = new Scanner(System.in); //setup user input
System.out.println("Think of a number and specify a range (integers only).");
//define range variables
int numlow = 0;
int numhigh = 0;
//prompt for range
System.out.println("Enter the lowest number in the range.");
if (in.hasNextInt()) {
numlow = in.nextInt();
} else { //catch non-ints
System.out.println("Unusable input. Please use integers.");
System.out.println("Press Enter to continue."); //pretty self explanatory
in.nextLine(); //I think this needs to be here
in.nextLine(); //dump the bad input
}
System.out.println("Enter the highest number in the range.");
if (in.hasNextInt()) {
numhigh = in.nextInt();
} else { //catch non-ints
System.out.println("Unusable input. Please use integers.");
System.out.println("Press Enter to continue."); //pretty self explanatory
in.nextLine(); //I think this needs to be here
in.nextLine(); //I think this needs to be here
in.nextLine(); //dump the bad input
}
if (numhigh < numlow) { //catch if range is reversed
System.out.println("Smaller number first.");
System.out.println("Press Enter to continue."); //pretty self explanatory
in.nextLine(); //I think this needs to be here
in.nextLine(); //dump the bad input
}
System.out.println("Range: " + numlow + " - " + numhigh + "\n\n"); //tell the user the range they specified
//instructions
System.out.println("Time to guess your number. You will see a guess printed.");
System.out.println("If it is too high, type \"lower.\"");
System.out.println("If it is too low, type \"higher.\"");
System.out.println("If it is correct, type \"correct.\" \n");
//make sure the user has read instructions
System.out.println("Hit Enter to continue.");
in.nextLine();
in.nextLine(); //Is there a better way to do this?
guessLoop(numhigh, numlow); //call the guessing function
//gameover confirmation
System.out.println("Hit Enter to go back to the menu.");
in.nextLine();
}
public static int randomInt(int numhigh, int numlow) {
//create a random guess
Random randomNum = new Random();
int randNum = randomNum.nextInt((numhigh - numlow) + 1) + numlow; //generate number from given range
return randNum; //return the random guess
}
public static void guessLoop(int numhigh, int numlow) {
Scanner in = new Scanner(System.in); //setup user input
int counter = 0;
while(true) {
try {
int guess = randomInt(numhigh, numlow); //call the random number function
System.out.println("Guess: " + guess); //tell user the guess
System.out.println("Is the number: ");
String response = in.nextLine();
if (response.toUpperCase().equals("LOWER")) {
numhigh = guess - 1;
} else if (response.toUpperCase().equals("HIGHER")) {
numlow = guess + 1;
} else if (response.toUpperCase().equals("CORRECT")) {
System.out.println("Great!");
System.out.println("It took " + (counter + 1) + " guesses to find the number.");
break;
}
counter += 1;
} finally{}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment