Skip to content
Permalink
Browse files

First version of Age Guesser in tutorial

Includes first tests of AgeGuesser.
  • Loading branch information
dackerman committed May 3, 2016
1 parent e6569a0 commit df6a3ee4cdcf4a635ca2e1afbad91199a36aa268
@@ -0,0 +1,28 @@
package com.dacklabs.ageguesser;

import static com.dacklabs.ageguesser.HintDirection.HIGHER;

public class AgeGuesser {
private int lowerBound = 0;
private int upperBound = 100;
private int hints = 0;

public int guess() {
int range = upperBound - lowerBound;
return range / 2 + lowerBound;
}

public void addHint(HintDirection direction) {
hints++;
int lastGuess = guess();
if (HIGHER.equals(direction)) {
lowerBound = lastGuess;
} else {
upperBound = lastGuess;
}
}

public int hintsReceived() {
return hints;
}
}
@@ -0,0 +1,5 @@
package com.dacklabs.ageguesser;

public enum HintDirection {
HIGHER, LOWER
}
@@ -0,0 +1,32 @@
package com.dacklabs.ageguesser;

import java.util.Scanner;

import static com.dacklabs.ageguesser.HintDirection.HIGHER;
import static com.dacklabs.ageguesser.HintDirection.LOWER;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("I will guess your age, and you tell me if it was higher, lower, or correct.");
AgeGuesser guesser = new AgeGuesser();
boolean correct = false;
while (!correct) {
int guess = guesser.guess();
System.out.println(String.format("Are you %d years old? (possible answers: higher, lower, correct)", guess));
String answer = scanner.nextLine().toLowerCase();

if (answer.contains("higher")) {
guesser.addHint(HIGHER);
} else if (answer.contains("lower")) {
guesser.addHint(LOWER);
} else if (answer.contains("correct")) {
correct = true;
} else {
System.out.println(String.format("I didn't understand \"%s\". Try again", answer));
}
}
System.out.println(String.format("Hooray! I only needed %d hints!", guesser.hintsReceived()));
}
}
@@ -0,0 +1,46 @@
package com.dacklabs.ageguesser;

import org.junit.Test;

import static com.dacklabs.ageguesser.HintDirection.HIGHER;
import static com.dacklabs.ageguesser.HintDirection.LOWER;
import static org.junit.Assert.*;

public class AgeGuesserTest {

@Test
public void ageGuesserAlwaysStartsAt50() {
AgeGuesser guesser = new AgeGuesser();

int guess = guesser.guess();

assertEquals("Guess was not 50!", 50, guess);
}

@Test
public void ageGuesserHalfsEachGuessAfterLowerHint() {
AgeGuesser guesser = new AgeGuesser();

guesser.addHint(LOWER);
assertEquals(25, guesser.guess());

guesser.addHint(LOWER);
assertEquals(12, guesser.guess());

guesser.addHint(LOWER);
assertEquals(6, guesser.guess());
}

@Test
public void ageGuesserCountsTheNumberOfHintsItGets() {
AgeGuesser guesser = new AgeGuesser();

assertEquals(0, guesser.hintsReceived());

guesser.addHint(LOWER);
assertEquals(1, guesser.hintsReceived());

guesser.addHint(HIGHER);
assertEquals(2, guesser.hintsReceived());
}
}

0 comments on commit df6a3ee

Please sign in to comment.
You can’t perform that action at this time.