Main;
Code:
package com.mycompany.hangman;
import java.util.Scanner;
/**
* Starts the game; once finished game asks the player to play again. Also exits
* the application here.
*
* @author Nintendo
*/
public class GameRunner {
public static void main(String[] args) {
HangMan game = new HangMan();
System.out.println("Welcome to Hangman.");
do {
game.resetGame();
game.start();
} while (replay());
System.out.println("Thank-you, for playing.");
}
/**
* @return true if user input starts with a "y" else return false.
*/
private static boolean replay() {
Scanner input = new Scanner(System.in);
System.out.println("Would you like to play again?");
String playAgain = input.nextLine().toUpperCase();
return playAgain.charAt(0) == 'Y';
}
}
Game;
Code:
package com.mycompany.hangman;
import static java.lang.System.out;
import java.util.Scanner;
/**
* The main class of the application we run the game through here.
*
* @author Nintendo
*/
public class HangMan {
private String secretWord;
private final String[] setHangMansBody = new String[5];
private final int MAX_TRIES = 5;
private int tries;
/**
* Starts the application here by deciding the games secret word.<p>
* Also checks if the player did guess the secret word or not.
*/
public void start() {
secretWord = SecretGameWords.getSecretWord();
out.println("You have " + MAX_TRIES + " tries, goodluck.\n");
if (guessedSecretWord()) {
out.println("You won!\n");
out.println("The word was: " + secretWord);
} else {
out.println("You lost!\n");
out.println("The word was: " + secretWord);
}
}
/**
* @return true if player manages to guess the secret word within 5
* tries.<p>
* false if player fails to guess the secret word.
*/
private boolean guessedSecretWord() {
Scanner input = new Scanner(System.in);
char guessedLetter = 0;
char[] hiddenWord = secretWord.toCharArray();
StringBuilder revealWord = new StringBuilder(secretWord.replaceAll("\\S", "-"));
out.println(revealWord);
while (tries < MAX_TRIES) {
out.print("Guess the word or just a letter: ");
String guessWord = input.nextLine().toUpperCase();
try {
guessedLetter = guessWord.charAt(0);
} catch (StringIndexOutOfBoundsException e) {
}
if (secretWord.indexOf(guessedLetter) != -1) {
for (int charPos = 0; charPos < hiddenWord.length; charPos++) {
if (hiddenWord[charPos] == guessedLetter) {
revealWord.setCharAt(charPos, guessedLetter);
}
}
if (revealWord.toString().equals(secretWord) || guessWord.equals(secretWord)) {
return true;
}
out.println(revealWord);
out.println("\nYou got it right!");
} else {
tries++;
out.println("You guessed wrong letter, you have " + (MAX_TRIES - tries) + " tries left.");
drawHangMan();
}
}
return false;
}
/**
* Resets the game variables to default.
*/
public void resetGame() {
tries = 0;
secretWord = null;
for (int bodyParts = 0; bodyParts < setHangMansBody.length; bodyParts++) {
setHangMansBody[bodyParts] = " | ";
}
}
/**
* Reveals hang man's body depending on failed tries.
*/
private void drawHangMan() {
switch (tries) {
case 1:
setHangMansBody[0] = " | / | ";
break;
case 2:
setHangMansBody[1] = " | (*_*) ";
break;
case 3:
setHangMansBody[2] = " | \\|/ ";
break;
case 4:
setHangMansBody[3] = " | | ";
break;
case 5:
setHangMansBody[4] = " | / \\ ";
break;
default:
resetGame();
}
out.println(" __________");
for (String revealBody : setHangMansBody) {
out.println(revealBody);
}
out.println(" | ");
out.println(" |________________ ");
}
}
Words
Code:
package com.mycompany.hangman;
import java.util.*;
/**
* Utility class that handles hangman's secret words.
*
* @author Roner Reked
*/
public class SecretGameWords {
private SecretGameWords() {
}
private static final List<String> secretWords = new ArrayList<>();
/**
* Creating a new array with the game words so we can add them to our
* secretWord list and then shuffle it.
*/
static {
String[] gameWords = {"JAVA", "COMPUTER", "UMBRELLA", "STAR",
"SLEEPY", "TRASH", "GOLD", "IMPOSTER"};
secretWords.addAll(Arrays.asList(gameWords));
Collections.shuffle(secretWords);
}
/**
* @return a new word from the secretWords list every time this method is
* called.
*/
public static String getSecretWord() {
return secretWords.get(nextWord++);
}
private static int nextWord = 0;
}
I mostly need feedback on my variable names, and javadoc basically the structure of the application. If anyone could look through it and give me some feedback on bad naming It would be really appreciated!