patternjavaMinor
Hangman in Java
Viewed 0 times
javahangmanstackoverflow
Problem
I have the following code in Java:
```
import java.util.ArrayList;
import java.util.Random;
public class HangmanFunctions {
private static final String[] WORDS = {"jazz", "buzz", "hajj", "fuzz", "jinx",
"jazzy", "fuzzy", "faffs", "fizzy", "jiffs",
"jazzed", "buzzed", "jazzes", "faffed", "fizzed",
"jazzing", "buzzing", "jazzier", "faffing", "fuzzing"};
private String word;
private ArrayList hiddenWord = new ArrayList();
private ArrayList lettersGuessed = new ArrayList();
public HangmanFunctions() {
setHiddenWord();
}
public ArrayList getLettersGuessed() {
return lettersGuessed;
}
private void setWord() {
word = WORDS[new Random().nextInt(19)];
}
public String getWord() {
return word;
}
private void setHiddenWord() {
setWord();
for (char letter: word.toCharArray()) {
hiddenWord.add('-');
}
}
public ArrayList getHiddenWord() {
return hiddenWord;
}
private ArrayList getIndexesOf(char letter) {
ArrayList instances = new ArrayList();
for (int i = word.indexOf(letter); i >= 0; i = word.indexOf(letter, i + 1)) {
instances.add(i);
}
return instances;
}
public void revealLetter(char letter) {
for (int i: getIndexesOf(letter)) {
hiddenWord.set(i, word.charAt(i));
}
}
public void addGuess(char letter) {
lettersGuessed.add(letter);
}
private boolean isGuessed(char letter) {
return lettersGuessed.indexOf(letter) != -1;
}
public Results getResult(char letter) {
if (!isGuessed(letter)) {
if (getIndexesOf(letter).size() > 0) {
return Results.CORRECT;
}
else {
return Results.INCORRECT
HangmanFunctions class:```
import java.util.ArrayList;
import java.util.Random;
public class HangmanFunctions {
private static final String[] WORDS = {"jazz", "buzz", "hajj", "fuzz", "jinx",
"jazzy", "fuzzy", "faffs", "fizzy", "jiffs",
"jazzed", "buzzed", "jazzes", "faffed", "fizzed",
"jazzing", "buzzing", "jazzier", "faffing", "fuzzing"};
private String word;
private ArrayList hiddenWord = new ArrayList();
private ArrayList lettersGuessed = new ArrayList();
public HangmanFunctions() {
setHiddenWord();
}
public ArrayList getLettersGuessed() {
return lettersGuessed;
}
private void setWord() {
word = WORDS[new Random().nextInt(19)];
}
public String getWord() {
return word;
}
private void setHiddenWord() {
setWord();
for (char letter: word.toCharArray()) {
hiddenWord.add('-');
}
}
public ArrayList getHiddenWord() {
return hiddenWord;
}
private ArrayList getIndexesOf(char letter) {
ArrayList instances = new ArrayList();
for (int i = word.indexOf(letter); i >= 0; i = word.indexOf(letter, i + 1)) {
instances.add(i);
}
return instances;
}
public void revealLetter(char letter) {
for (int i: getIndexesOf(letter)) {
hiddenWord.set(i, word.charAt(i));
}
}
public void addGuess(char letter) {
lettersGuessed.add(letter);
}
private boolean isGuessed(char letter) {
return lettersGuessed.indexOf(letter) != -1;
}
public Results getResult(char letter) {
if (!isGuessed(letter)) {
if (getIndexesOf(letter).size() > 0) {
return Results.CORRECT;
}
else {
return Results.INCORRECT
Solution
Overview
Separate the game's entry point, model, the view, and the controller into different classes:
Identify the responsibilities and behaviours of the classes.
HangmanGame
Responsibilities:
HangmanModel
Responsibilities:
HangmanView
Responsibilities:
HangmanController
Responsibilities:
Implementation
Once you've identified the classes, their responsibilities, and their collaborators, think about how you would implement the actions described above. For example:
Once you are satisfied that you have thought about most of the possible actions that each class will need to make the game, then consider the attributes each class needs and how the implementation will work.
Procedural Programming vs. OOP
The
A set of functions is not an object-oriented class.
Separate the game's entry point, model, the view, and the controller into different classes:
- HangmanGame - The main entry point to the application.
- HangmanModel - Contains information about the word being guessed.
- HangmanView - Displays the results of the model.
- HangmanController - Updates the model based on events from the view.
Identify the responsibilities and behaviours of the classes.
HangmanGame
Responsibilities:
- Creates instances of the required classes (see also Spring)
- Exits the application.
- Creates a new game.
HangmanModel
Responsibilities:
- Load words.
- Select a word to guess.
- Retrieves positions that a letter is found (or throws an exception).
- Tracks incorrect guesses.
HangmanView
Responsibilities:
- Creates a new UI for the view.
- Updates the drawing of the hangman.
- Updates the letters being displayed.
- Notifies listeners of guesses.
- Notifies listeners of when the user requests to quit.
- Animates the hanging.
HangmanController
Responsibilities:
- Listens for events from the view.
- Tells the view when the game is over.
- Tells the view when another guess is made.
- Tells the view where to put the letters.
Implementation
Once you've identified the classes, their responsibilities, and their collaborators, think about how you would implement the actions described above. For example:
public class HangmanModel {
public void load( File dictionary ) { ... }
public int[] guess( char ch ) { ... }
public void badGuess() { ... }
public void pickWord() { ... }
}
public class HangmanGame {
public void initialize() { ... }
public void quit() { ... }
public void newGame() { ... }
}
public class HangmanView {
public void initialize() { ... }
public void drawHangman( HangmanStatus status ) { ... }
public void drawWord( String word[] ) { ... }
public void notifyGuess() { ... }
public void notifyQuit() { ... }
public void animateHanging() { ... }
}Once you are satisfied that you have thought about most of the possible actions that each class will need to make the game, then consider the attributes each class needs and how the implementation will work.
Procedural Programming vs. OOP
The
HangmanFunctions class is not object-oriented. When writing object-oriented code try to think in terms of the objects in the system. For example, you could further split HangmanView into two classes: HangmanUI and AnimatedHangman. The HangmanUI would represent the user interface (ASCII text in DOS vs. Swing vs. AWT) wherein the user interacts with the game. The AnimatedHangman would abstract drawing and animating the hangman figure in the game.A set of functions is not an object-oriented class.
Code Snippets
public class HangmanModel {
public void load( File dictionary ) { ... }
public int[] guess( char ch ) { ... }
public void badGuess() { ... }
public void pickWord() { ... }
}
public class HangmanGame {
public void initialize() { ... }
public void quit() { ... }
public void newGame() { ... }
}
public class HangmanView {
public void initialize() { ... }
public void drawHangman( HangmanStatus status ) { ... }
public void drawWord( String word[] ) { ... }
public void notifyGuess() { ... }
public void notifyQuit() { ... }
public void animateHanging() { ... }
}Context
StackExchange Code Review Q#26898, answer score: 6
Revisions (0)
No revisions yet.