Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.inksaver.java;
- /*
- Hangman console version Java
- You will need all 3 class files:
- Main.java http://pastebin.com/nbuy3y90
- Hangman.java http://pastebin.com/chsL0dbY
- Dictionary.java http://pastebin.com/BKkvSap3
- get dictionary.txt from github or elsewhere call it "dictionary.txt" in same folder as src
- https://github.com/dmcguinness/Hangman/blob/master/dictionary.txt
- The methods used here may not be the most efficient, but have been chosen to
- match as far as possible the code used in the Lua and Python versions,
- so they can be directly compared and contrasted.
- */
- import java.util.Random;
- import java.util.Scanner;
- public class Main {
- static String version = "1.0";
- //note a single Random object is reused here
- static Random randomGenerator = new Random();
- static Scanner input = new Scanner(System.in);
- //Program starts here:
- public static void main(String[] args) {
- System.out.println("Welcome to Hangman version " + version);
- //create an instance of the Dictionary class
- Dictionary wordlist = new Dictionary();
- String answer = "";
- if (wordlist.getReport() == "OK"){//Dictionary.txt file found
- while (true) {
- //create instance of Hangman as a game object
- Hangman game = new Hangman();
- printMenu(wordlist, game); // print introduction and get word from dictionary object
- play(game); // play the game
- game.printStage(); // print final stage
- if (game.getFail()){
- System.out.println("Sorry you did not guess the word");
- System.out.println("it was: " + game.getWord());
- }
- else{
- System.out.println("Well Done!");
- }
- System.out.println("Do you want to play again? Y/N > ");
- answer = input.nextLine();
- if (!answer.contains("y") && !answer.contains("Y")){ // any key to quit, Y to continue
- System.out.println("Thank you for playing Hangman!");
- break;
- }
- }
- }
- else {
- System.out.println(wordlist.getReport()); //Tell player the error reported.
- }
- input.close();
- }
- private static void play(Hangman game){
- String guess = "";
- int ascii;
- while (true){
- game.printStage();
- System.out.println("Letters Used:" + game.getLettersChosen());
- System.out.println("Letters Available: " + game.getLettersAvailable());
- System.out.println();
- while (true){
- System.out.println("Choose a letter: ");
- guess = input.nextLine();
- if (guess.length() == 1){
- ascii = guess.toCharArray()[0];
- if (ascii > 64 && ascii < 91 || ascii > 96 && ascii < 123){
- guess = guess.toUpperCase();
- break;
- }
- }
- }
- game.checkGuess(guess);
- if (game.getCompleted()){
- break;
- }
- }
- }
- private static void printMenu(Dictionary wordlist, Hangman game){
- int randomInt;
- do {
- randomInt = randomGenerator.nextInt();
- } while (randomInt < 5 || randomInt >= 15);
- game.setWord(wordlist.getWord(randomInt)); // get a word from the dictionary
- System.out.println("Guess the " + game.getWordLength() + " letter word I have chosen");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement