All pastes #808278 Raw Edit

Hangman

public text v1 · immutable
·published 2007-12-07 13:01 UTC
rendered paste body
import java.io.*;
import java.util.*;

class UserInput {

	char  input;
	// Get user input

	public char getInput() {

	BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
	try {
		do {
			System.out.print("Please enter your guess: ");
			input = (char) inp.read();
			} while (input < 'a' && input > 'Z');
		} catch (IOException exc) { System.out.println("Failed to read input");}
	return input;
	}
}

// set the word for the user to enter

class Word {
	String word;

	public String getWord() {
	try {
		BufferedReader in = new BufferedReader(new FileReader(new File("words.txt")));

		ArrayList<String> list = new ArrayList<String>();

		while (true){
			String line = in.readLine();
			if (line == null)
			break;
			list.add(line);
			}

		int size = list.size();
		int random = (int)(Math.random() * size);
		word = list.get(random);
		} catch (IOException exc) { System.out.println("Error Reading File"); }
	return word;
	}
}

// Check if the letters entered by the user are contained in the word.
class WordCheck {

	public void WrdChk() {

		UserInput ui = new UserInput();
		Word wrd = new Word();
		PlayAgain pa = new PlayAgain();

		// set amount of user tries
		int tries = 8;

		// create hidden word stringbuffer
		StringBuffer hidden_word = new StringBuffer();
		StringBuffer used_letters = new StringBuffer();
		String word = wrd.getWord();

		// hide word
		for(int i = 0 ; i < word.length(); i++) {
		hidden_word.append("-");
		}
	boolean has_tries = true;


	System.out.println("Hello Welcome to Hangman!");
	System.out.println("You have " + tries + " tries to guess all the letters in the word.");
	System.out.println("If you fail, you get hanged!! ");
	System.out.println("The word is: " + hidden_word);
	System.out.println();

	// if user still has tries
	for(;has_tries;) {
	// if the user has guessed to word execute the code below
	if(hidden_word.toString().equals(word.toString())) {
		System.out.println("Congratulations! You guessed the word '" + word + "' correctly");
		pa.PlayAgain();
		return;
		}
	// Get users guess

	char input = ui.getInput();

	// Check if the letter is found in the used_letters String bufffer, if it isn't add it to the string, if it is do nothing.

	//new Char(input).toString();
	if(used_letters.toString().contains("" + input)) {
		System.out.println("You have entered this letter before!");
		} else { used_letters.append(input + "");
	}

	// if the word is true add the letter to the hidden_letter string buffer
	boolean is_found = false;
	for(int i = 0; i < word.length(); i++) {
		if(input == word.charAt(i)) {
			is_found = true;
			hidden_word.setCharAt(i, input);
			}
		}
	// if the letter is not found remove a try and inform the user how many tries they have left.
	// if the user is right let the user know and print the hidden word.
	if(!is_found) {
		tries--;
		if(tries == 0) {
			has_tries = false;
			System.out.println("\nGame Over!!! You got hung!\n");
			System.out.println("The word was: '" + word + "' better luck next time.");
			pa.PlayAgain();
			return;
			}
		System.out.println("You guessed wrong, you have " + tries + " remaining");
		System.out.println("Word so far: " + hidden_word);
		System.out.println("\t\t\t\tCurrently used letters: " + used_letters);
		} else { System.out.println("Letter found!");
		System.out.println("Word so far: " + hidden_word);
		System.out.println("\t\t\t\tCurrently used letters: " + used_letters);
		}
	}
}
}

class PlayAgain {

	char input;

	void PlayAgain() {

		WordCheck wcc = new WordCheck();
		BufferedReader inp2 = new BufferedReader(new InputStreamReader(System.in));

		System.out.print("\nWould you like to play again? 'y' for yes, 'n' for no: ");
		try {
		input = (char) inp2.read();
		} catch (IOException exc) { System.out.println("Failed to read input"); }

	if(input == 'y') {
		System.out.println();
		wcc.WrdChk();
		} else return;
	}
}

class HangMan2 {
	public static void main(String[] args) {

		WordCheck wc = new WordCheck();

		wc.WrdChk();

		}
	}