Assignment Series #A12 – Journeyman’s Piece

Project Hangman
For once, we’ll do something a bit more fun: we will create a little game of hangman. Here are the
requirements:
- We need the ability to provide a list of all possible words. It would be nice if this could be configured,
but you can also hard-code it. - The user will be given a short introductory text
- A word will be selected randomly from the wordlist (have a look at java.util.Random)
- The user will be presented with a representation showing how many characters the word has
- The program will prompt the user to enter a single character
- If the character is part of the word, the representation will be updated to show the position(s) of the
character
– Make sure you ignore the case of the character the user enters
• If the character is not part of the word, the ascii-art representation of a hanging person should be updated
(e.g. arm added,. . . )
– The game end after 6 wrong guesses (head, body, arm 1, arm 2, leg 1, leg 2)

Some additional thoughts:
- I’ll work with char arrays
- I’ll use a large dictionary file from here: http://www.gwicks.net/dictionaries.htm
- The program will pick a random word from dictionary
- I’ll work with a loop (get a letter, check the letter, update…)
The code
Step 1: get a random word from dictionary
Step 2: work with a loop (get a letter, check the letter, update…)
hangman.java
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Hangman {
public static void main(String[] args) throws Exception {
// Start Hangman
System.out.println("Welcome to Hangman!");
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("Please start by typing a letter");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("You have to guess a random word from a dictionary");
System.out.println("For each round you can type one letter");
System.out.println("You have 6 lives to guess the word");
System.out.println("For each wrong letter you lose a live");
System.out.println("God luck :)");
//Load dictionary
File dictionary = new File("swiss.txt");
Scanner textScanner = new Scanner(dictionary);
Scanner input = new Scanner(System.in);
ArrayList<String> words = new ArrayList<>();
while(textScanner.hasNextLine()) {
words.add(textScanner.nextLine());
}
String hidden_text = words.get((int)(Math.random()*words.size()));
char[] textArray = hidden_text.toCharArray();
char[] myAnswers = new char[textArray.length];
for(int i = 0; i < textArray.length; i++) {
myAnswers[i] = '?';
}
boolean finished = false;
int lives = 6;
while(finished == false) {
System.out.println("************************");
String letter = input.next();
//Input validation, no numbers and only one letter allowed
while(letter.length() !=1 || Character.isDigit(letter.charAt(0))) {
System.out.println("Error Inut - Try again");
letter = input.next();
}
// check if letter is in the word
boolean found = false;
for(int i = 0; i < textArray.length; i++) {
if(letter.charAt(0) == textArray[i]) {
myAnswers[i] = textArray[i];
found = true;
}
}
if(!found) {
lives--;
System.out.println("Wrong letter");
}
boolean done = true;
for(int i = 0; i < myAnswers.length; i++) {
if(myAnswers[i] == '?') {
System.out.println(" _");
done = false;
}
else {
System.out.println(" " + myAnswers[i]);
}
}
System.out.println("\n" + "Lives left: " + lives);
drawHangman(lives);
// check if game ends
if(done) {
System.out.println("Congrats you found the word");
finished = true;
}
if(lives <=0) {
System.out.println("You are dead! :((");
finished = true;
}
}
}
public static void drawHangman(int l) {
if(l == 6) {
System.out.println("|----------");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 5) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 4) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| |");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 3) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 2) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 1) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else{
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
}
}
Code explained step by step
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
public class Hangman {
public static void main(String[] args) throws Exception {
// Start Hangman, print introduction
System.out.println("Welcome to Hangman!");
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("Please start by typing a letter");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("You have to guess a random word from a dictionary");
System.out.println("For each round you can type one letter");
System.out.println("You have 6 lives to guess the word");
System.out.println("For each wrong letter you lose a live");
System.out.println("God luck :)");
// Create new file object called dictionary based on the file swiss.txt
File dictionary = new File("swiss.txt");
// Create new scanner object based on the file object dictionary
Scanner textScanner = new Scanner(dictionary);
// Standard java scanner object to read in user input from the console
Scanner input = new Scanner(System.in);
// Create new array list which only holds string objects
ArrayList<String> words = new ArrayList<>();
// Continue running trough while the file has another line
while(textScanner.hasNextLine()) {
// add the next line of the file into the array list words
words.add(textScanner.nextLine());
}
// get a random word out of the array list words
// math.random returns a number between 0 and 1, not including 1
String hidden_text = words.get((int)(Math.random()*words.size()));
// create a array of chars based on the string hidden_text
char[] textArray = hidden_text.toCharArray();
// create an empty char array with the same length as the text array
char[] myAnswers = new char[textArray.length];
//fill the entire myAnswers Array with questionmarks
for(int i = 0; i < textArray.length; i++) {
myAnswers[i] = '?';
}
// Create boolean with the value false
boolean finished = false;
// Create integer with the value 6
int lives = 6;
// Continue repeating the following code until the boolean finished is no longer false
while(finished == false) {
System.out.println("************************");
// save the user input from the console into the string letter
String letter = input.next();
//Input validation, no numbers and only one letter allowed
while(letter.length() !=1 || Character.isDigit(letter.charAt(0))) {
// repeat the following lines until both conditions are matched
System.out.println("Error Inut - Try again");
letter = input.next();
}
// check if letter is in the word
boolean found = false;
// go trough every letter of the textArray
for(int i = 0; i < textArray.length; i++) {
// is the current letter of the text array = to the first letter of the letter string
if(letter.charAt(0) == textArray[i]) {
//set the letter at index i of myAnswers = the letter at index i of textArray
myAnswers[i] = textArray[i];
//set the boolean found = true
found = true;
}
}
// if the letter was not found so the boolean is false
if(!found) {
// decrease the value of variable lives by one
lives--;
// print out Wrong letter
System.out.println("Wrong letter");
}
// Create the variable done and set it = true
boolean done = true;
// go trough the entire myAnwers Array letter by letter
for(int i = 0; i < myAnswers.length; i++) {
// if the current letter is = ?
if(myAnswers[i] == '?') {
// print out _
System.out.println(" _");
// set variable done = false
done = false;
}
// if the current letter is not equal to ?
else {
// print out the current letter preceded by one space
System.out.println(" " + myAnswers[i]);
}
}
// print out a new line then the text Lives left and the value of the variable lives
System.out.println("\n" + "Lives left: " + lives);
// call the function drawHangman and pass it to the current value of the variable lives
drawHangman(lives);
// check if the variable done is = true
// You win, game will end
if(done) {
System.out.println("Congrats you found the word");
finished = true;
}
// check if the variable lives is less than or equal to 0
// You lose, game will end
if(lives <=0) {
System.out.println("You are dead! :((");
finished = true;
}
}
}
// display the number of lives with a hangman picture :P
public static void drawHangman(int l) {
if(l == 6) {
System.out.println("|----------");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 5) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 4) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| |");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 3) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 2) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else if(l == 1) {
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
else{
System.out.println("|----------");
System.out.println("| O");
System.out.println("| -|-");
System.out.println("| /|");
System.out.println("|");
System.out.println("|");
System.out.println("|");
}
}
}
PDF Report:

