## Jupyter at Bryn Mawr College

Public notebooks: /services/public/dblank / CS110 Intro to Computing / 2017-Fall / Notebooks
In [7]:
int guessCount;
String stage;
int guessMin;
int guessMax;

void setup() {
size(600, 100);
guessCount = 0;
textFont(createFont("Arial", 20));
stage = "start";
guessMin = 0;
guessMax = 1000000;
}

void draw() {
background(0);
if (stage.equals("start")) {
// display an intro message
text("Welcome to AI! Think of a number (" + guessMax +  ") then press SPACE", 10, 50);
} else if (stage.equals("play")) {
// ask user for for feed back
text("Here is my guess: " +  currentGuess() + ".... press L or H or C", 10, 50);
} else {
// computer won or lost
text("GAME OVER! It took me " + guessCount + " tries!", 10, 50);
}
}

int currentGuess() {
return int((guessMin + guessMax) / 2.0);
}

void keyPressed() {
if (key == 'h' || key == 'H') {
// guess higher!
guessMin = currentGuess();
guessCount++;
} else if (key == 'l' || key == 'L') {
// guess lower!
guessMax = currentGuess();
guessCount++;
} else if (key == 'c' || key == 'C') {
stage = "game over";
} else if (key == ' ') {
// start playing!
stage = "play";
}
}

Sketch #7: