It randomly gets a word from an array of String
s. 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", "hedgehogs", "hornets",
"kangaroos" };
private String secretWord;
char[] hangman;
public void initializeGame() {
setWord();
setRow();
misses.delete(0, misses.length());
System.out.println("\nNew game started! Type \"exit\" to stop game or \"new\" to reset !\nOnly the first letter in youre input will be considered a guess.\nYou have 8 guesses !\n");
}
private void setWord() {
secretWord = wordArray[random.nextInt(wordArray.length)];
}
private void setRow() {
hangman = new char[secretWord.length()];
for (int i = 0; i < secretWord.length(); i++) {
hangman[i] = '_';
}
}
public void showRow() {
for (int i = 0; i < hangman.length; i++)
System.out.print(hangman[i] + " ");
}
public void showMisses() {
System.out.println(misses);
}
private boolean isCharInWord(char guess) {
for (int i = 0; i < hangman.length; i++) {
if (secretWord.charAt(i) == guess)
return true;
}
return false;
}
private void changeRow(char guess) {
for (int i = 0; i < hangman.length; i++)
if (secretWord.charAt(i) == guess)
hangman[i] = secretWord.charAt(i);
}
private void addToMisses(char guess) {
misses.append(guess + " ");
}
public void checkRow(char guess) {
if (isCharInWord(guess)) {
changeRow(guess);
} else
addToMisses(guess);
}
public boolean isLooser() {
if (misses.length() / 2 > 7)
return true;
return false;
}
public boolean IsWinner() {
for (int i = 0; i < hangman.length; i++)
if (hangman[i] == '_')
return false;
return true;
}
public String getSecretWord() {
return secretWord;
}
}