From a8f1e050921754b8b8bb171fd5ed143129ad0089 Mon Sep 17 00:00:00 2001 From: Rob Hallam <0504004h@student.gla.ac.uk> Date: Wed, 20 Sep 2023 11:52:38 +0100 Subject: [PATCH] (feat) Implement Lab 6 exercise 5 - number guessing game (AI) user guessing when we get stdin working --- week1.java | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/week1.java b/week1.java index f3a2717..6f8332a 100644 --- a/week1.java +++ b/week1.java @@ -7,8 +7,8 @@ class runLabs { public static void main(String[] args) { new Lab4(); new Lab5(); + new Lab7(); new Lab6(); - // new Lab7(); } } @@ -254,11 +254,11 @@ class Lab6 extends Lab { new six1(); System.out.println(String.format("%n")); new six3(); - new six5(); new six6(); new six7(); new six8(); new six9(); + new six5(); } /** @@ -289,10 +289,75 @@ class Lab6 extends Lab { } } + /** + * Number guessing game + * Random number between 0 and 99, user guesses and is told higher / lower / correct + */ class six5 extends Exercise { + private Random random = new Random(); + public six5() { super(5); - System.out.println(String.format("TODO / NOT IMPLEMENTED until we get stdin working")); + System.out.println(String.format("TODO: get stdin working to user can play")); + numberGuessingGame(); + // System.out.println(String.format("Testing randomBetween...")); + // int max = 100; + // int min = 50; + // for (int i=0; i<125; i++) { + // System.out.printf("%d ", randomBetween(min, max)); + // } + // System.out.println(String.format("done.")); + } + + public void numberGuessingGame() { + final boolean SUPER_ADVANCED_AI = true; + final int HIGHEST = 99; + final int MAX_GUESSES = 10; + + int chosenNumber = random.nextInt(HIGHEST+1); + int guess = random.nextInt(HIGHEST+1); + // super advanced AI section + int maxPossible = HIGHEST+1; + int minPossible = 0; + + String guesses = "guess"; // for correct plurals + + System.out.printf("Guessing...\t"); + for (int i = 0; i < MAX_GUESSES; i++ ) { + System.out.printf("%d", guess); + if (guess == chosenNumber) { + System.out.println(String.format("\nYou guessed right after %d %s! It was indeed %d", + i+1, guesses, chosenNumber)); + return; + } + guesses = "guesses"; // didn't get it on the first try + if (guess < chosenNumber) { + System.out.printf("↑\t"); + if (guess > minPossible) { minPossible = guess; } + } else { + System.out.printf("↓\t"); + if (guess < maxPossible) { + maxPossible = guess; + } + } + + // The better approach is to guess halfway between minPossible and maxPossible + // but that's kinda boring and robotic + if (SUPER_ADVANCED_AI) { + guess = randomBetween(minPossible, maxPossible); + } + // TODO change the behaviour so it doesn't guess the same number in a row + } + + System.out.println(String.format("\nThe answer was: %d. Better luck next time!", chosenNumber)); + + } + + public int randomBetween(int min, int max) { + if (min < 0) { min = 0; } + if (max == 0) { return 0; } + // System.out.printf(" random.nextInt(%d - %d) + %d ", max, min, min); + return (random.nextInt(max - min) + min); } } /**