Guest User

Untitled

a guest
Apr 22nd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.45 KB | None | 0 0
  1. //Johnnie Hernandez
  2. //This program will play the game hangman!
  3. import java.util.*;
  4. import java.io.*;
  5.  
  6. public class Hangman {
  7.     private static String path;
  8.     public static Scanner keyboard;
  9.     public static int MAX_LETTERS=20;
  10.     public static int GUESSES=8;
  11.     public static int MAX_WORDS=15000;
  12.  
  13.  
  14.     public static void main(String[] args) {
  15.         path = "E:\\Users\\Johnnie\\workspace\\Assignment_12\\dictionary.txt";
  16.         keyboard= new Scanner(System.in);
  17.  
  18.         boolean isWordFound=true;
  19.         String[] dictionaryData=new String[MAX_WORDS];
  20.         char[] wordForHangman=new char[MAX_LETTERS];
  21.         String randomWord;
  22.         String[] guessedLetters=new String[GUESSES];
  23.  
  24.         try{dictionaryData=readFile();}
  25.         catch(IOException e){
  26.             System.out.println(e.getMessage());
  27.             System.out.println("the file dictionary can not be opened");
  28.         }
  29.         displayDirections();
  30.         clearRenderScreen();
  31.         randomWord=chooseWord(dictionaryData);
  32.  
  33.         int lengthOfWord=randomWord.length();      
  34.         wordForHangman=new char[lengthOfWord];     
  35.         wordForHangman=randomWord.toCharArray();
  36.  
  37.         char[] lettersToBeTrue= new char[lengthOfWord];
  38.         for(int i=0;i<lengthOfWord;i++){lettersToBeTrue[i]='*';}    //This loop will create the array of "*"
  39.         for(int i=0;i<guessedLetters.length;i++){guessedLetters[i]=" ";} //This clears the array so not to print any null values to screen
  40.         displayCharArray(wordForHangman, lengthOfWord);
  41.         isWordFound=guessALetter(wordForHangman, lengthOfWord,lettersToBeTrue,guessedLetters,isWordFound);
  42.         if(isWordFound){System.out.println("***you win***");}
  43.         else{System.out.println("*****you lose*****\n sorry better luck next time!");}
  44.  
  45.     }
  46.  
  47.     /**
  48.      * This is a method that will read and store the file under variable path into an String[array]
  49.      * @return
  50.      * @throws IOException
  51.      */
  52.     public static String[] readFile() throws IOException{
  53.         FileReader fr = new FileReader(path);
  54.         BufferedReader textReader= new BufferedReader(fr);
  55.         int numberOfLines=readLines();
  56.         String []dictionaryData= new String[numberOfLines];
  57.         for(int i=0;i<numberOfLines; i++){
  58.             dictionaryData[i]=textReader.readLine();
  59.         }
  60.         textReader.close();
  61.         String[] temp=new String[numberOfLines];
  62.         temp=dictionaryData;
  63.         return temp;
  64.     }
  65.  
  66.     /**
  67.      * This is a method that will read the number of lines of the variable path
  68.      * @return
  69.      * @throws IOException
  70.      */
  71.     public static int readLines() throws IOException{
  72.         FileReader dictionary = new FileReader(path);
  73.         BufferedReader bf= new BufferedReader(dictionary);
  74.         String aLine;
  75.         int numberOfLines=0;
  76.         while((aLine=bf.readLine()) != null){
  77.             numberOfLines++;
  78.         }
  79.         bf.close();
  80.         return numberOfLines;
  81.     }
  82.  
  83.     /**
  84.      * This is a test method for displaying the full contents of the array    
  85.      * @param dictionaryData
  86.      */
  87.     public static void displayArray(String[] dictionaryData){
  88.         for(int i=0;i<dictionaryData.length;i++){
  89.             System.out.println(dictionaryData[i]);
  90.         }
  91.     }
  92.     /**
  93.      * this will choose one random word from our dictionary and make it avaiable for the game
  94.      * @param dictionaryData
  95.      * @param randomInt
  96.      * @return
  97.      */
  98.     public static String chooseWord(String[] dictionaryData){
  99.         Random randomIntGen= new Random();
  100.         int randomInt=randomIntGen.nextInt(dictionaryData.length-1);
  101.         String temp=dictionaryData[randomInt];
  102.         return temp;
  103.     }
  104.  
  105.     /**
  106.      * this method simply displays the directions and is put in a method to erase clutter in main
  107.      */
  108.     public static void displayDirections(){
  109.         System.out.println("\t\t\t H A N G M A N\n\n"
  110.                 +"This is a word guessing game.  A word will be selected at random\n" +
  111.                 "and kept hidden. You will try to figure out the secret word by\n" +
  112.                 "guessing letters which you think are in the word.  You will guess\n" +
  113.                 "one leter at a time.  If the letter you guess is correct, the\n" +
  114.                 "position(s) of the letter in the secret word will be shown.\n" +
  115.                 "You will be allowed 8 wrong guesses.  If you guess incorrectly 8\n" +
  116.                 "times, you lose the game.  If you guess all of the letters in the\n" +
  117.                 "word, you win.\n\n" + "Press enter to continue");
  118.         keyboard.nextLine();
  119.     }
  120.     /**
  121.      * This method will take the users guess and then check to see if it is a hit or miss,
  122.      * misses will be added to the guessedLetters array
  123.      * hits will replace '*' in the lettersTrue
  124.      * @param wordForHangman
  125.      * @param lengthOfWord
  126.      * @param lettersToBeTrue
  127.      * @param guessedLetters
  128.      */
  129.     public static boolean guessALetter(char[] wordForHangman,int lengthOfWord,char[] lettersToBeTrue,String[] guessedLetters,boolean isWordFound){
  130.         String temp;
  131.         boolean winOrlose=isWordFound;
  132.         int guessCounter=0;
  133.         while(isWordFoundYet(lettersToBeTrue,lengthOfWord)){
  134.             boolean    isGuessTrue=false;
  135.             char guess;
  136.  
  137.  
  138.             System.out.println("The word so far:");
  139.             displayCharArray(lettersToBeTrue, lengthOfWord);
  140.             displayStringArray(guessedLetters);
  141.             System.out.println("\nMake a guess");
  142.             temp=keyboard.next();
  143.             guess=temp.charAt(0);
  144.  
  145.             for(int i=0;i<lengthOfWord;i++){
  146.                 if(guess==wordForHangman[i]){
  147.                     lettersToBeTrue[i]=guess;
  148.                     isGuessTrue=true;    
  149.                 }
  150.             }
  151.             if(!isGuessTrue){
  152.                 guessedLetters[guessCounter]=Character.toString(guess);
  153.                 guessCounter++;
  154.  
  155.                 if(guessCounter>=8){
  156.                     winOrlose=false;
  157.                     return winOrlose;
  158.                 }
  159.             }
  160.             clearRenderScreen();
  161.         }
  162.  
  163.         return winOrlose;
  164.     }
  165.     /**
  166.      * this will test to see if the word has been completely found yet
  167.      * @param guessedLetters
  168.      * @param lengthOfWord
  169.      * @return
  170.      */
  171.     public static boolean isWordFoundYet(char[] guessedLetters, int lengthOfWord){
  172.         boolean temp=false;
  173.         for(int i=0;i<lengthOfWord;i++){if(guessedLetters[i]=='*'){temp=true;}}
  174.         return temp;
  175.     }
  176.     /**
  177.      * this array simply displays the letters found and '*" for all non found values
  178.      * @param lettersToBeTrue
  179.      * @param lengthOfWord
  180.      */
  181.     public static void displayCharArray(char[]lettersToBeTrue, int lengthOfWord){
  182.         for(int i=0;i<lengthOfWord;i++){
  183.             System.out.print(lettersToBeTrue[i]+" ");
  184.         }
  185.         System.out.print("\n");
  186.     }
  187.     /**
  188.      * this array simply displays the letters guessed
  189.      * @param guessedLetters
  190.      */
  191.     public static void displayStringArray(String[] guessedLetters){
  192.         System.out.println("These are the letters you have guessed:");
  193.         for(int i=0;i<guessedLetters.length;i++){
  194.             System.out.print(guessedLetters[i]+" ");
  195.         }
  196.     }
  197.     /**
  198.      * This will print 80 enter keys in order to give a new blank screen
  199.      */
  200.     public static void clearRenderScreen(){
  201.         for(int i=0;i<80;i++)
  202.             System.out.println("\n");
  203.     }
  204. }
Add Comment
Please, Sign In to add comment