Advertisement
Inksaver

Hangman Java class: Main (1 of 3)

Sep 4th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package org.inksaver.java;
  2. /*
  3.     Hangman console version Java
  4.     You will need all 3 class files:
  5.     Main.java       http://pastebin.com/nbuy3y90
  6.     Hangman.java        http://pastebin.com/chsL0dbY
  7.     Dictionary.java     http://pastebin.com/BKkvSap3
  8.     get dictionary.txt from github or elsewhere call it "dictionary.txt" in same folder as src
  9.     https://github.com/dmcguinness/Hangman/blob/master/dictionary.txt
  10.     The methods used here may not be the most efficient, but have been chosen to
  11.     match as far as possible the code used in the Lua and Python versions,
  12.     so they can be directly compared and contrasted.
  13. */
  14. import java.util.Random;
  15. import java.util.Scanner;
  16.  
  17. public class Main {
  18.     static String version = "1.0";
  19.     //note a single Random object is reused here
  20.     static Random randomGenerator = new Random();
  21.     static Scanner input = new Scanner(System.in);
  22.    
  23.     //Program starts here:
  24.     public static void main(String[] args) {
  25.         System.out.println("Welcome to Hangman version " + version);
  26.         //create an instance of the Dictionary class
  27.         Dictionary wordlist = new Dictionary();
  28.         String answer = "";
  29.         if (wordlist.getReport() == "OK"){//Dictionary.txt file found
  30.             while (true) {
  31.                 //create instance of Hangman as a game object
  32.                 Hangman game = new Hangman();
  33.                 printMenu(wordlist, game); // print introduction and get word from dictionary object
  34.                 play(game); // play the game
  35.                 game.printStage(); // print final stage
  36.                
  37.                 if (game.getFail()){
  38.                     System.out.println("Sorry you did not guess the word");
  39.                     System.out.println("it was: " + game.getWord());
  40.                 }
  41.                 else{
  42.                     System.out.println("Well Done!");
  43.                 }
  44.                 System.out.println("Do you want to play again? Y/N > ");
  45.                 answer = input.nextLine();
  46.                 if (!answer.contains("y") && !answer.contains("Y")){ // any key to quit, Y to continue
  47.                     System.out.println("Thank you for playing Hangman!");
  48.                     break;
  49.                 }
  50.             }  
  51.         }
  52.         else {
  53.             System.out.println(wordlist.getReport()); //Tell player the error reported.
  54.         }
  55.         input.close();
  56.     }
  57.    
  58.     private static void play(Hangman game){
  59.         String guess = "";
  60.         int ascii;
  61.         while (true){
  62.             game.printStage();
  63.             System.out.println("Letters Used:" + game.getLettersChosen());
  64.             System.out.println("Letters Available: " + game.getLettersAvailable());    
  65.             System.out.println();
  66.             while (true){
  67.                 System.out.println("Choose a letter: ");
  68.                 guess = input.nextLine();
  69.                
  70.                 if (guess.length() == 1){
  71.                     ascii = guess.toCharArray()[0];
  72.                     if (ascii > 64 && ascii < 91 || ascii > 96 && ascii < 123){
  73.                         guess = guess.toUpperCase();
  74.                         break;
  75.                     }
  76.                 }
  77.             }
  78.                
  79.             game.checkGuess(guess);
  80.             if (game.getCompleted()){
  81.                 break;
  82.             }
  83.         }
  84.     }
  85.  
  86.     private static void printMenu(Dictionary wordlist, Hangman game){  
  87.         int randomInt;
  88.         do {
  89.             randomInt = randomGenerator.nextInt();
  90.         } while (randomInt < 5 || randomInt >= 15);
  91.  
  92.         game.setWord(wordlist.getWord(randomInt)); // get a word from the dictionary
  93.         System.out.println("Guess the " + game.getWordLength() + " letter word I have chosen");
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement