From 9924b7f61e0a48bcd863e34619861dc122f0b69d Mon Sep 17 00:00:00 2001 From: alexdowd Date: Wed, 7 Feb 2018 09:27:48 +0000 Subject: [PATCH 1/2] Guess by Alex and Diego --- org/asl/socketserver/games/Guess.java | 76 +++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 org/asl/socketserver/games/Guess.java diff --git a/org/asl/socketserver/games/Guess.java b/org/asl/socketserver/games/Guess.java new file mode 100644 index 0000000..624ec68 --- /dev/null +++ b/org/asl/socketserver/games/Guess.java @@ -0,0 +1,76 @@ +package org.asl.socketserver.games; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.PrintWriter; +import java.util.Scanner; + +import org.asl.socketserver.MenuInfo; +import org.asl.socketserver.Servable; +@MenuInfo(authors = { +"Alex Collins" }, version = "Fall, 2017", title = "Hidden Word", description = "Mastermind meets Hangman.") + +public class Guess implements Servable { + public static void main(String[] args) { + } + + private static int pickRandomNumber(int limit) { + + return (int) (Math.random() * limit); + } + + private static boolean lowOrHigh(int random, int theGuess) { + if (theGuess > random) { + return true; + } else { + return false; + } + } + + private static boolean isCorrect(int random, int theGuess) { + + if (random == theGuess) { + return true; + } else { + return false; + } + } + + @Override + public void serve(BufferedReader input, PrintWriter out) throws IOException { + // TODO Auto-generated method stub + + out.println( + "This is a guessing game. I'll think of a number between 0 and any number you want. Then you have to guess what it is."); + + out.println("Enter a number: "); + int l = Integer.parseInt(input.readLine()); + int rand = pickRandomNumber(l); + int count = 0; + + System.out.println("I'm thinking of a number between 0 and " + l + ". Now make a guess:"); + int g = Integer.parseInt(input.readLine()); + //input.close(); + + while (true) { + if (isCorrect(rand, g) == true) { + count++; + out.println("You got it in " + count + " guesses!"); + break; + } else { + if (lowOrHigh(rand, g) == true) { + out.println("Too high. Try a smaller answer."); + g = Integer.parseInt(input.readLine()); + count++; + } else { + out.println("Too low. Try a bigger answer."); + g = Integer.parseInt(input.readLine()); + count++; + } + } + } + } + +} + +// find out how to get it to loop until the user gets it right \ No newline at end of file From 6add89c6007cbcc2e528c28844e0645898f09dbb Mon Sep 17 00:00:00 2001 From: alexdowd Date: Wed, 7 Feb 2018 09:28:15 +0000 Subject: [PATCH 2/2] Guess by Alex and Diego --- org/asl/socketserver/games/Guess.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/org/asl/socketserver/games/Guess.java b/org/asl/socketserver/games/Guess.java index 624ec68..5854081 100644 --- a/org/asl/socketserver/games/Guess.java +++ b/org/asl/socketserver/games/Guess.java @@ -8,7 +8,7 @@ import org.asl.socketserver.MenuInfo; import org.asl.socketserver.Servable; @MenuInfo(authors = { -"Alex Collins" }, version = "Fall, 2017", title = "Hidden Word", description = "Mastermind meets Hangman.") +"Alex Dowd" }, version = "Winter, 2018", title = "Guess", description = "Guess the number.") public class Guess implements Servable { public static void main(String[] args) {