patternjavaModerate
Number-guessing game with a cheat code
Viewed 0 times
numberwithgamecheatguessingcode
Problem
As my first Java program, I have written a number-guessing game. I would appreciate any suggestions on how I can clean it up or streamline it. I don't mean cut down on the general tasks executed within the program, but rather if there were better methods/classes/syntax I could have used to make it better. I took a few snippets from a tutorial.
```
import java.util.Scanner;
import java.util.Random;
class Game {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//generate a random number
//take user input
//check the input act appropriately
boolean running = true;
boolean guessed = true;
boolean realInt = true;
int number = 0;
int input = 0;
String firstName=" ";
System.out.print("want to play the game? Yes/No: ");
firstName = sc.next();
while (running) {
if (guessed && (("yes".equals(firstName)) | ("cheater".equals(firstName)))){
System.out.println("I have chosen a random value");
System.out.println("Have a go at guessing it");
Random rand = new Random();
number = rand.nextInt(10) + 1;
guessed = false;
}
if(("yes".equals(firstName)) | ("cheater".equals(firstName)))
{
if(sc.hasNextInt()) {
realInt=true;
input = sc.nextInt();
}
else{
System.out.println("That is not an integer.");
running = false;
}
}
else
running = false;
if((((input != number) && realInt)&&(input != -1))){
System.out.println("Plsease try again");
}
//below line sets up when to stop the program, when -1 is entered
if (input == -1){
running = false;
}
else{
guessed = input == number;
}
//below lines are cheat codes
if (input == -5){
System.out.println("Answer: " + number);
}
if ("cheater".equals(firstName) && (input != number)){
Sy
```
import java.util.Scanner;
import java.util.Random;
class Game {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//generate a random number
//take user input
//check the input act appropriately
boolean running = true;
boolean guessed = true;
boolean realInt = true;
int number = 0;
int input = 0;
String firstName=" ";
System.out.print("want to play the game? Yes/No: ");
firstName = sc.next();
while (running) {
if (guessed && (("yes".equals(firstName)) | ("cheater".equals(firstName)))){
System.out.println("I have chosen a random value");
System.out.println("Have a go at guessing it");
Random rand = new Random();
number = rand.nextInt(10) + 1;
guessed = false;
}
if(("yes".equals(firstName)) | ("cheater".equals(firstName)))
{
if(sc.hasNextInt()) {
realInt=true;
input = sc.nextInt();
}
else{
System.out.println("That is not an integer.");
running = false;
}
}
else
running = false;
if((((input != number) && realInt)&&(input != -1))){
System.out.println("Plsease try again");
}
//below line sets up when to stop the program, when -1 is entered
if (input == -1){
running = false;
}
else{
guessed = input == number;
}
//below lines are cheat codes
if (input == -5){
System.out.println("Answer: " + number);
}
if ("cheater".equals(firstName) && (input != number)){
Sy
Solution
Depends on how you want to improve it, there's number of ways to go. For instance:
Procedural vs OO code - Extendability
If you wish to extend your game, add new functionalities, make it one game among many that users can play, you'd better switch from current programming style to more modularized one.
Right now that code is contained within "main" method. It's quite common for first programs, to be like that. It's rather procedural programming - you give the computer instruction after instruction. While easy to follow, it's problematic when you try to expand such code or maintain it. It's also mind-numbing to read, making it hard to modify properly.
You may want to modularize it somewhat:
This is obviously very short answer to very large topic, but do ask if you're interested. You would then have to model the program using objects.
For me that would be number 1. Even over syntax.
Syntax & Cleaning
Tools help
Intellij IDEA has a menu called Analyze. If you select Inspect code there, you'll notice a few pointers, along with explanation.
Additionally, there is Checkstyle and FindBugs, they also offer lots of pointers.
Heed earlier advice
Giving credit where it's due, I also completely agree with sje397's comment. Let me quote it here:
Fix indenting. Fix variable names (
name). You don't even need to enter the loop if the answer is 'no' or
the value us 'cheater'. The
can use boolean vars to store the results of your calls to
That's a start :)
Less sparse code
Less empty lines. Your code is very sparse which makes it harder to read.
Initializing many variables need not happen in different lines.
Rename
It's not just a number, it's a game answer.
Read on String comparison
You're using
Yoda conditionals?
Some folks don't like Yoda-conditionals.
Choose your own flavour.
DRY
Keep your code DRY, that is Don't Repeat Yourself. Often you use same conditions in your flow-control instructions (if, while...).
Cheat codes
You have two cheat codes. One is to input -5, the other is to answer starting question with "cheater". Both serve same purpose, the game reveals the number. If so, why not collapse to ifs into one? Because the code executed in is the same.
Checking user input
Note: this code is changed using pointers above.
You may want to shorten them to functions. This is easy to do in IDE. Select
Then
becomes
Usability
Here is my game log:
Firstly... please. If I'm to read that word that often, please, write it well. ;-)
Also, how can I quit? I'm bored, I want to quit.
When I guessed right, don't just skip to next game, let me know it wasn't an error in the code, that I "won". ;-)
Finally, random value is a very broad term. Decimals? Negatives? 1:10? -100 gazillions : 98 gazillions? When I read this, I had unpleasant feeling about it, how long do I need to guess?
Testability
You may want to add tests or verifications that your game progresses as you intend it to. Using
Procedural vs OO code - Extendability
If you wish to extend your game, add new functionalities, make it one game among many that users can play, you'd better switch from current programming style to more modularized one.
Right now that code is contained within "main" method. It's quite common for first programs, to be like that. It's rather procedural programming - you give the computer instruction after instruction. While easy to follow, it's problematic when you try to expand such code or maintain it. It's also mind-numbing to read, making it hard to modify properly.
You may want to modularize it somewhat:
- group instructions together into procedures / functions
- more related procedures into classes
- leave in main as few things as you can
This is obviously very short answer to very large topic, but do ask if you're interested. You would then have to model the program using objects.
For me that would be number 1. Even over syntax.
Syntax & Cleaning
Tools help
Intellij IDEA has a menu called Analyze. If you select Inspect code there, you'll notice a few pointers, along with explanation.
Additionally, there is Checkstyle and FindBugs, they also offer lots of pointers.
Heed earlier advice
Giving credit where it's due, I also completely agree with sje397's comment. Let me quote it here:
Fix indenting. Fix variable names (
firstName isn't holding a firstname). You don't even need to enter the loop if the answer is 'no' or
the value us 'cheater'. The
| symbol is 'binary or' - you want ||. Youcan use boolean vars to store the results of your calls to
equals.That's a start :)
Less sparse code
Less empty lines. Your code is very sparse which makes it harder to read.
Initializing many variables need not happen in different lines.
boolean running, guessed, realInt;
running = guessed = realInt = true;Rename
number to answer.It's not just a number, it's a game answer.
Read on String comparison
You're using
equals for it. There's also equalsIgnoreCase which in most cases is preferred.Yoda conditionals?
Some folks don't like Yoda-conditionals.
"value".equals(variable) -> variable.equals("value"). Choose your own flavour.
DRY
Keep your code DRY, that is Don't Repeat Yourself. Often you use same conditions in your flow-control instructions (if, while...).
Cheat codes
You have two cheat codes. One is to input -5, the other is to answer starting question with "cheater". Both serve same purpose, the game reveals the number. If so, why not collapse to ifs into one? Because the code executed in is the same.
Checking user input
Note: this code is changed using pointers above.
|->||
equals->equalsIgnoreCase
- less parens, cut those redundant
You may want to shorten them to functions. This is easy to do in IDE. Select
("yes".equalsIgnoreCase(firstName) || "cheater".equalsIgnoreCase(firstName)) and - if you use Eclipse, press Alt+T, this opens refactoring window. Choose extract method, name your method 'yesOrCheater' and proceed. In Intellij the shortcut is Ctrl+Alt+M.Then
if (guessed && ("yes".equalsIgnoreCase(firstName) || "cheater".equalsIgnoreCase(firstName))) {becomes
if (guessed && yesOrCheater(firstName)) {Usability
Here is my game log:
want to play the game? Yes/No: yes
I have chosen a random value
Have a go at guessing it
5
Plsease try again
4
Plsease try again
65
# [some tries later]
Plsease try again
3
I have chosen a random value
Have a go at guessing it
2
[...]
Plsease try again
5
I have chosen a random value
Have a go at guessing it
Firstly... please. If I'm to read that word that often, please, write it well. ;-)
Also, how can I quit? I'm bored, I want to quit.
When I guessed right, don't just skip to next game, let me know it wasn't an error in the code, that I "won". ;-)
Finally, random value is a very broad term. Decimals? Negatives? 1:10? -100 gazillions : 98 gazillions? When I read this, I had unpleasant feeling about it, how long do I need to guess?
Testability
You may want to add tests or verifications that your game progresses as you intend it to. Using
assert with JVM flag, or some test library (TestNG over JUnit, my personal preference). But that's also a large topic, perhaps for next time.Code Snippets
boolean running, guessed, realInt;
running = guessed = realInt = true;if (guessed && ("yes".equalsIgnoreCase(firstName) || "cheater".equalsIgnoreCase(firstName))) {if (guessed && yesOrCheater(firstName)) {Context
StackExchange Code Review Q#102301, answer score: 11
Revisions (0)
No revisions yet.