Can someone right the code for Hangman in java. The game would start off by aski
ID: 3843474 • Letter: C
Question
Can someone right the code for Hangman in java. The game would start off by asking the user if they were playing 1 person or two person hangman. If the user said 1 person then the game would print the spaces for the words just like in hangman and ask the user to guess a letter. Every time the user guessed incorrectly the game would subtract a life and add that letter to the list of incorrect letters guessed. Each time the user guessed the word right the game would print that letter in its corresponding spot. For example it would start like this
Are you playing by yourself or two player?
user enters: 1
The game has generated a word for you. It has 8 spaces. Would you like to guess the word, or would you rather guess a letter? Enter word if you want to guess word and enter letter if you want to guess letter. ( It would print out this _ _ _ _ _ _ _ _ as well, this shows the 8 spaces for the letters that user will need to guess and gives them some visualization to go along with the game.)
(The secret word is JAZZLIKE, The game would also ask if the user would like to guess the full word before he guesses a letter each time. If he decides he wants to guess the full word and gets it wrong it would use his turn and take a life away)
user enters: 1
Please only enter single alphabetical letters
user enters: ok
Please only enter single alphabetical letters
user enters: E
The letter E is correct, you have 8 lives remaining. (The game would fill the spaces in the word it was using that had an E) ---> _ _ _ _ _ _ _ E
The code would continue untill the user lost all their lives and the game said game over or until the user had guessed all the letters correctly
Please make sure the code runs without any errors. Thank you
Explanation / Answer
import java.util.*;
public class Hangman {
private static String [] words = {"geography", "cat", "yesterday", "java", "truck", "opportunity", "fish", "token", "transportation", "bottom", "apple", "cake", "remote", "pocket", "terminology", "arm", "cranberry", "tool", "caterpillar", "spoon", "watermelon", "laptop", "toe", "toad", "fundamental", "capitol", "garbage", "anticipate", "apple"};
private String secretWord;
private ArrayList<Character> correctLetters;
private ArrayList<Character> incorrectLetters;
private Scanner stdin = new Scanner(System.in);
public Hangman() {
this.secretWord = "miscellaneous";
Random randIndex = new Random();
int index = randIndex.nextInt(Hangman.words.length);
this.secretWord = Hangman.words[index];
this.correctLetters = new ArrayList<Character>();
for (int i = 0; i < this.secretWord.length(); i++)
this.correctLetters.add('_');
this.incorrectLetters = new ArrayList<Character>();
}
public void playGame() {
while (!gameOver()) {
printHangman();
for (int i = 0; i < this.correctLetters.size(); i++)
System.out.print(this.correctLetters.get(i) + " ");
System.out.print(" Wrong letters: ");
for (int i = 0; i < this.incorrectLetters.size(); i++)
System.out.print(this.incorrectLetters.get(i) + " ");
System.out.print(" Enter a lower-case letter as your guess: ");
String guess = stdin.nextLine();
handleGuess(guess.charAt(0));
}
System.out.println("The secret word was: " + secretWord);
if (gameWon()) {
System.out.println("Congratulations, you won!");
} else {
System.out.println("Sorry, you lost.");
printHangman();
}
}
private void handleGuess(char ch) {
return ch;
}
private boolean gameWon() {
return false;
}
private boolean gameLost() {
return false;
}
private boolean gameOver() {
return false;
}
private void printHangman() {
int poleLines = 6; // number of lines for hanging pole
System.out.println(" ____");
System.out.println(" | |");
int badGuesses = this.incorrectLetters.size();
if (badGuesses == 7) {
System.out.println(" | |");
System.out.println(" | |");
}
if (badGuesses > 0) {
System.out.println(" | O");
poleLines = 5;
}
if (badGuesses > 1) {
poleLines = 4;
if (badGuesses == 2) {
System.out.println(" | |");
} else if (badGuesses == 3) {
System.out.println(" | \|");
} else if (badGuesses >= 4) {
System.out.println(" | \|/");
}
}
if (badGuesses > 4) {
poleLines = 3;
if (badGuesses == 5) {
System.out.println(" | /");
} else if (badGuesses >= 6) {
System.out.println(" | / \");
}
}
if (badGuesses == 7) {
poleLines = 1;
}
for (int k = 0; k < poleLines; k++) {
System.out.println(" |");
}
System.out.println("__|__");
System.out.println();
}
public String toString() {
String s = "secret word: " + this.secretWord;
s += " correct letters: ";
for (int i = 0; i < this.correctLetters.size(); i++)
s += this.correctLetters.get(i) + " ";
s += " used letters: ";
for (int i = 0; i < this.incorrectLetters.size(); i++)
s += this.incorrectLetters.get(i) + " ";
s += " # bad letters: " + this.incorrectLetters.size();
return s;
}
private void setCurrentWord(String newWord) {
this.secretWord = newWord;
}
private void setCorrectLetters(ArrayList<Character> newGuesses) {
this.correctLetters = newGuesses;
}
private void setIncorrectLetters(ArrayList<Character> newUsedLetters) {
this.incorrectLetters = newUsedLetters;
}
private void setBadGuesses(int newBadGuesses) {
this.incorrectLetters.clear();
for (int i = 0; i < newBadGuesses; i++) {
this.incorrectLetters.add('x');
}
}
public static void main(String [] args) {
Hangman game = new Hangman();
System.out.println("The CONSTRUCTED game is: " + game);
System.out.println(" ======== END CONSTRUCTOR TEST ======== ");
if (game.gameWon())
System.out.println("Game should not be won at beginning");
String str = "miscellaneous";
game.setCurrentWord(str);
ArrayList<Character> guesses = new ArrayList<Character>();
for (int i = 0; i < str.length(); i++)
guesses.add(str.charAt(i));
game.setCorrectLetters(guesses);
if (!game.gameWon()) {
System.out.println(game);
System.out.println("Game should be won");
}
for (int i = 0; i < str.length(); i += 3)
guesses.set(i, '_');
game.setCorrectLetters(guesses);
if (game.gameWon()) {
System.out.println(game);
System.out.println("Game should NOT be won");
}
System.out.println(" ======== END gameWon TEST ======== ");
game = new Hangman();
if (game.gameLost())
System.out.println("Game should not be lost at beginning");
game.setBadGuesses(3);
if (game.gameLost()) {
System.out.println(game);
System.out.println("Game should not be lost");
}
game.setBadGuesses(7);
if (!game.gameLost()) {
System.out.println(game);
System.out.println("Game should be lost");
}
game.setBadGuesses(10);
if (!game.gameLost()) {
System.out.println(game);
System.out.println("Game should be lost");
}
System.out.println(" ======== END gameLost TEST ======== ");
System.out.println(" ======== END gameOver TEST ======== ");
game = new Hangman(); // start with a new game
System.out.println(game);
game.handleGuess('a'); // check a letter in the word
System.out.println(game);
game.handleGuess('q'); // check a letter not in the word
System.out.println(game);
game.handleGuess('m'); // check for first letter in word
System.out.println(game);
game.handleGuess('l'); // check a letter that appears more than once
System.out.println(game);
game.handleGuess('s'); // check last letter in word
System.out.println(game);
game.handleGuess('x'); // check another letter not in word
System.out.println(game);
System.out.println(" ======== END handleGuess TEST ======== ");
game = new Hangman(); /
game.playGame();
}
}