patternjavaMinor
Simple Hangman game
Viewed 0 times
gamesimplehangman
Problem
It randomly gets a word from an array of
Some advice would be welcome, such as what I should change, avoid and so on.
Main class:
Game class
```
import java.util.Random;
public class Game {
Random random = new Random();
StringBuffer misses = new StringBuffer("");
private final String[] wordArray = { "baboons", "beavers", "cats",
"chickens", "choughs", "dolphins", "eagles", "elephants",
"flamingoes", "giraffes", "grasshoppers", "h
Strings. It prints out a '_' char for every letter of the word, displays the ones you have already guessed and displays the ones you have missed.Some advice would be welcome, such as what I should change, avoid and so on.
Main class:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Game game = new Game();
Scanner keyboard = new Scanner(System.in);
String input;
char guess;
game.initializeGame();
do {
System.out.print("\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\nWord: ");
game.showRow();
System.out.print("\n\nMisses: ");
game.showMisses();
System.out.print("\nGuess: ");
input = keyboard.next();
if (input.equalsIgnoreCase("exit"))
break;
else if (input.equalsIgnoreCase("new"))
game.initializeGame();
else {
guess = input.charAt(0);
game.checkRow(guess);
if (game.isLooser()) {
System.out
.println("\nGame Over!\nYou have missed 8 times!\nThe word was "
+ game.getSecretWord()
+ " ! \nThe game will now reset!\n");
game.initializeGame();
}
else if (game.IsWinner()) {
System.out
.println("\nCongratulations!\nYou have guessed the word!\nThe game will now reset!\n");
game.initializeGame();
}
}
} while (true);
System.out.println("\nThank you for playing !");
keyboard.close();
}
}Game class
```
import java.util.Random;
public class Game {
Random random = new Random();
StringBuffer misses = new StringBuffer("");
private final String[] wordArray = { "baboons", "beavers", "cats",
"chickens", "choughs", "dolphins", "eagles", "elephants",
"flamingoes", "giraffes", "grasshoppers", "h
Solution
Remember that any function of the type
can be shortened to:
You can apply this advice to:
Also I think that you can simplify the condition to:
The function:
Can benefit from built-ins:
As well as this one:
Brace yourself
Please always use braces, doing so increases readability and reduces stupid-bugs probability.
A bit of generality
and then in the initialization I would call:
I think my version is more immediate to understand.
(By the way, I can see that it is an array, so you can remove that from the name, a simple
def function(arg):
if condition(arg):
return True
return Falsecan be shortened to:
def function(arg):
return condition(arg)You can apply this advice to:
public boolean isLooser() {
if (misses.length() / 2 > 7)
return true;
return false;
}Also I think that you can simplify the condition to:
misses.length() > 14The function:
public boolean IsWinner() {
for (int i = 0; i < hangman.length; i++)
if (hangman[i] == '_')
return false;
return true;
}Can benefit from built-ins:
public boolean IsWinner() {
return hangman.indexOf('_') == -1; // '_' never occurs
}As well as this one:
private boolean isCharInWord(char guess) {
return secretWord.indexOf(guess) != -1;
}Brace yourself
Please always use braces, doing so increases readability and reduces stupid-bugs probability.
A bit of generality
setWord is specific to this game, I would do:public void randomChoice(array) {
return array[random.nextInt(array.length)];
}and then in the initialization I would call:
public void initializeGame() {
secretWord = randomChoice(wordArray);
// ...
}I think my version is more immediate to understand.
(By the way, I can see that it is an array, so you can remove that from the name, a simple
words would be better).Code Snippets
def function(arg):
if condition(arg):
return True
return Falsedef function(arg):
return condition(arg)public boolean isLooser() {
if (misses.length() / 2 > 7)
return true;
return false;
}misses.length() > 14public boolean IsWinner() {
for (int i = 0; i < hangman.length; i++)
if (hangman[i] == '_')
return false;
return true;
}Context
StackExchange Code Review Q#96351, answer score: 4
Revisions (0)
No revisions yet.