patternjavaMinor
Hangman game using Java 8
Viewed 0 times
javagameusinghangman
Problem
I’ve been getting used to Java and I’ve written a simple hangman game using event-driven programming in Java 8. Please let me know of any improvements regarding the readability and design of the program, along with any pointers on how to neaten up the code.
Also, any feedback regarding the use of JavaDoc style comments is welcome.
Main - the class used to start the game
Logic - contains all the game logic
```
package Hangman;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
/** Logic - Deals with the logic aspect of Hangman */
public class Logic {
private GUI gui;
private Information information;
private WordReader wordReader;
/** Logic constructor */
public Logic(){
wordReader = new WordReader();
information = new Information(wordReader);
gui = new GUI(this, information);
}
/** checkUserGuess - runs the code for when a user guesses a letter that's in the secret word */
void checkUserGuess(String guess){
if(isGuessValid(guess)) {
updateSecretWord(guess);
gui.drawSecretWord();
if(information.getLettersGuessed().size() != 0) { // No letters have been guessed by the user at the beginning
gui.drawLettersGuessed();
}
// Checks if the user has won or lost
if (isGameWon()) {
winSequence();
}else if (information.getGuessesRemaining() == 0) {
loseSequence();
}
}
}
void go(){
information.createAlphabetSet();
wordReader.readFile("words.txt");
gui.build();
setUpGame();
}
/** isGameWon - sees if the user has won the game */
private boolean isGameWon(){
for(boolean isLetterShown : information.getLettersRevealed()){
if(!isLetterShown
Also, any feedback regarding the use of JavaDoc style comments is welcome.
Main - the class used to start the game
package Hangman;
public class Main {
public static void main(String[] args){
new Logic().go();
}
}Logic - contains all the game logic
```
package Hangman;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
/** Logic - Deals with the logic aspect of Hangman */
public class Logic {
private GUI gui;
private Information information;
private WordReader wordReader;
/** Logic constructor */
public Logic(){
wordReader = new WordReader();
information = new Information(wordReader);
gui = new GUI(this, information);
}
/** checkUserGuess - runs the code for when a user guesses a letter that's in the secret word */
void checkUserGuess(String guess){
if(isGuessValid(guess)) {
updateSecretWord(guess);
gui.drawSecretWord();
if(information.getLettersGuessed().size() != 0) { // No letters have been guessed by the user at the beginning
gui.drawLettersGuessed();
}
// Checks if the user has won or lost
if (isGameWon()) {
winSequence();
}else if (information.getGuessesRemaining() == 0) {
loseSequence();
}
}
}
void go(){
information.createAlphabetSet();
wordReader.readFile("words.txt");
gui.build();
setUpGame();
}
/** isGameWon - sees if the user has won the game */
private boolean isGameWon(){
for(boolean isLetterShown : information.getLettersRevealed()){
if(!isLetterShown
Solution
Comments
any feedback regarding the use of JavaDoc style comments is welcome.
The structure of your method comments is:
But JavaDocs are actually structured like this:
The important parts are that the method name should not be part of the comment (it will be added automatically when generating a JavaDoc), and that there are annotations for method arguments and return values. You can also add additional annotations.
Many of your in-code comments are a bit redundant, for example
Structure
Misc
any feedback regarding the use of JavaDoc style comments is welcome.
The structure of your method comments is:
/** methodName - what method does */But JavaDocs are actually structured like this:
/**
* short description of what method does.
*
* Longer description, special cases, etc.
*
* @param paramName what param does
* @param anotherParam what it does
* @return what is returned
*/The important parts are that the method name should not be part of the comment (it will be added automatically when generating a JavaDoc), and that there are annotations for method arguments and return values. You can also add additional annotations.
Many of your in-code comments are a bit redundant, for example
lettersGuessed: letters the user has guessed.Structure
Informationis a very generic name. What information? It seems to just contain everything where you were unsure of where to put it: It contains the alphabet, the words, the user input, etc. I would split this up into different classes. I do see why you did this though: So that the GUI can get one object, from which it then can obtain state to be presented. Instead of creating one god object which contains all the information, you could instead add parameters to the GUI methods, and pass the information to them that way (egdrawLettersGuessed()->drawLettersGuessed(Set guessedLetters)).
- Your
Logicclass also does a bit too much: It handles the game state (updateSecretWord,isGuessValid, etc), but also the game loop (egplayAgain).
- your GUI on the other hand is structured quite well, with the separation of
DrawingPaneland the generalGUI.
Misc
- use the explicit private/public keywords instead of the default.
- the
WordReaderalways has to read a file, right? Otherwise it's useless. So I would add a constructor and read the file when constructing it.
size() != 0can be written as!isEmpty().
- instead of just crashing when the word file doesn't exist or is empty, an informative error message would be nice (
Could not find file /path/to/file/filenameandFile /path/to/file/filename is empty or malformed).
Code Snippets
/** methodName - what method does *//**
* short description of what method does.
* <p>
* Longer description, special cases, etc.
*
* @param paramName what param does
* @param anotherParam what it does
* @return what is returned
*/Context
StackExchange Code Review Q#84276, answer score: 3
Revisions (0)
No revisions yet.