# Help on Number Guess Game

``````Hi, Thank you for your help! I’ve done it. After the player guesses correctly, it prompts them for Y to play again and N to exit. After they press Y how would you reset the char? Aka make a new number for them to guess.

String Welcome = "Welcome to the Number Guessing Game!";
String Instruct = "The Computer will generate a random number (0 to 9) and it's your job to guess the number by using the number keys.";

int onum = int(random(0, 9)); // onum = Originial Number
char gnum = char(onum+48); // Gnum = "Guess Number" and 48 is 0 char
char input; // Made char for user input

String correct = "Congrats! You guessed correctly!";
String guess_lower = "Guess lower!";
String guess_higher = "Guess higher!";
String feedbackText = "";
String End = "";
String End1 = "Congrats, you guessed the number correctly!, Press Y to play again or N to exit";

// ------------------------------------------------------------------------------------

void setup() {
size(600, 600);
background(230, 230, 255);
println(gnum);
}

void draw() {
background(230,230,255);
textSize(20);
fill(0);
text(Welcome, 50, 100, 280, 100);
text(Instruct, 50, 170, 400, 170);
text(feedbackText, 190, 460, 270, 460);
text(End, 150, 350, 270, 350);
}

// ------------------------------------------------------------------------------------

void keyPressed() {
input = key; // Putting input char in use, collecting user input now

if (input == gnum) {
feedbackText = correct;
End = End1;
}
if (input > gnum) {
// Guess Lower
feedbackText = guess_lower;
}
if (input < gnum) {
// Guess Higher
feedbackText = guess_higher;
}
if (key == 'y' || key == 'Y') {
feedbackText = "";
End = "";

}

}
``````

Hi, I was just finishing up my number guessing game but I ran into a slight problem that I can’t seem to get past and I’m seeking for help! The code above is my number guessing game, and the thing I need help on is when it prompts the user to play again (Press Y) I want it to generate a new gnum number (gnum is my char) and make the old number not guessable/active anymore.

TL;DR

When it prompts user to play again (by Pressing) Y, I need it to make a new gnum number to guess (gnum is my char/number to guess) and making the old number unguessable/not active?

Cheers!

Hi @codinghelp,

you almost got it! Think about the changes in code below …

``````String Welcome = "Welcome to the Number Guessing Game!";
String Instruct = "The Computer will generate a random number (0 to 9) and it's your job to guess the number by using the number keys.";

int onum = int(random(0, 9)); // onum = Originial Number
char gnum = char(onum+48); // Gnum = "Guess Number" and 48 is 0 char
char input; // Made char for user input

String correct = "Congrats! You guessed correctly!";
String guess_lower = "Guess lower!";
String guess_higher = "Guess higher!";
String feedbackText = "";
String End = "";
String End1 = "Congrats, you guessed the number correctly!, Press Y to play again or N to exit";

boolean found = false;

// ------------------------------------------------------------------------------------

void setup() {
size(600, 600);
background(230, 230, 255);
println(gnum);
}

void draw() {
background(230, 230, 255);
textSize(20);
fill(0);
text(Welcome, 50, 100, 280, 100);
text(Instruct, 50, 170, 400, 170);
text(feedbackText, 190, 460, 270, 460);
text(End, 150, 350, 270, 350);
}

// ------------------------------------------------------------------------------------

void keyPressed() {
input = key; // Putting input char in use, collecting user input now
if (!found) {
if (input < '0' || input > '9') {
feedbackText = "should be a number between 0 and 9.";
} else if (input == gnum) {
feedbackText = correct;
End = End1;
found = true;
} else if (input > gnum) {
// Guess Lower
feedbackText = guess_lower;
} else if (input < gnum) {
// Guess Higher
feedbackText = guess_higher;
}
} else if (key == 'y' || key == 'Y') {
feedbackText = "Ok! Let's guess a new one :)";
End = "";
onum = int(random(0, 9)); // onum = Originial Number
gnum = char(onum+48); // Gnum = "Guess Number" and 48 is 0 char
found = false;
}
}
``````

Cheers
— mnse

PS:
now try to refine your code. Is it really necessary to use gnum or could it be made by only using onum.
Check what is necessary in global context and what could be used in local context.

1 Like