HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Hangman game in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javagamehangman

Problem

I'm hoping someone can look over my code and tell me if there is anything I can improve. I am not very experienced with Java. I am hoping for some feedback as to avoid developing any bad habits which can turn into bad and slow code.

It can also be found here.

```
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.util.InputMismatchException;
import java.lang.Runtime;
public class MinOppgave3 {

static Scanner input = new Scanner(System.in);
static boolean checkLoop = true;
static char[] word;
static char[] temp;

static String hangmanWord = "TemporaryString";
static File f = new File("words.txt");
static String[] wordsFromFile;
static int missCount = 0;
// Game board for hangman
static char[][] board = {
{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},
{'_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '|'},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '|', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', ' ', '|', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
{'|', '_', '|', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'},
};

public static void main(String[] args) throws IOExcept

Solution

Missing Functionality

As I know hangman, the letters that were guessed are written down. You should also save them and print them out each time, otherwise the user has to remember this.

Bug

If I play a game, lose, and then print out all words, the hangman board is printed as well.

Game Loop

Use a game loop instead of your repeated calls to playHangman. Your approach is hard to grasp, especially since you not only call playHangman from playHangman, but also from checkWordWithCharacter and checkWordFull. It would look something like this:

while (!gameOver) {
    char guess getInput();
    processInput(guess);
    printBoard();
}


I would also rather use a while loop for the menu than using your recursive approach.

Single Responsibility

Functions as well as classes should only have one responsibility. You only have one class, so it obviously does everything.

Your playHangman function for example handles the game loop, gets and checks user input, applies the input, updates the game, and prints.

Don't Repeat yourself

You have the functionality of counting how often a character appears in a string twice. Just extract it to a method.

OOP

Java is an object oriented language, so try to make use of this.

Using static is always a sign of bad design. There are situations where it does make sense, but every time you use it, you should think about it.

Try to create more classes. It's better to create too many than too few. At least I would expect a board class, a class handling the word management (reading and writing to a file, etc), a class handling user input, a class handling output, a game class, and maybe even a word class.

Type of countHits

You don't care how often the guessed character is in the word, only if it was in it at least once. So I would make this a boolean.

Variable Names

  • countHits is not only the wrong type, it's also not a good name. guessInWordCount or something would be better.



  • temp might be ok in very limited scope, but not as a class field. Same goes for f.



  • inpChar is cryptic. use inputChar.



  • you use x quite a lot. For loop variables, i would be customary as default (except when you have a good reason), and for return statements something that represents the value, for example selectedWord.



Comments

For method names, use JavaDoc style comments.

Style

  • be consistent with your curly brackets. Either they go all on the same line, or all on a new line. Same goes for else statements.



  • also be consistent with spaces (before and after { and ().



Use any IDE to fix this easily.

Misc

  • inputCharacter.length() 0 is equal to inputCharacter.length() == 1.



  • you can used enhanced for loops: for(int x = 0; x for (String word : wordsFromFile) {.



  • no need to save return values in temporary variables if they are not used.



  • use private and public for fields and methods.



  • don't assign values that are never used (eg inputCharacter = " ", menuChoice = 0, or inpChar = '\0').



  • instead of toLowerCase().equals, you can use equalsIgnoreCase.



  • You are not properly closing your FileWriter`.

Code Snippets

while (!gameOver) {
    char guess getInput();
    processInput(guess);
    printBoard();
}

Context

StackExchange Code Review Q#63207, answer score: 4

Revisions (0)

No revisions yet.