Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Hangman1 {
  4.     Scanner keyboard = new Scanner(System.in);
  5.     Random rand = new Random();
  6.    
  7.     // determines if character c is inside String str. True returned if
  8.     // inside. call isIn inside printCurrStatus
  9.    
  10.     // see comments in hint for printCurrStatus
  11.    
  12.     boolean isIn(char c, String str)
  13.     {      
  14.         int length = str.length();
  15.         boolean bool = false;
  16.        
  17.         for(int i = 0; i < length; i++)
  18.         {
  19.             if(c == str.charAt(i))
  20.                 return true;
  21.             else
  22.                 bool = false;
  23.         }
  24.        
  25.         return bool;
  26.  
  27.     }
  28.    
  29.     // If userInput contains "ard" and strToGuess contains "aardvark"
  30.     // then the routine prints output that looks like:
  31.    
  32.     // Current Status for userInputs=ard
  33.     // a a r d _ a r _
  34.    
  35.     // this routine returns true if all letters are guessed, otherwise
  36.     // false is returned
  37.    
  38.     // HINT: It is useful to have for loop that goes through each of char
  39.     // in strToGuess. Call isIn for each character
  40.     //(note the second parameter would be userInputs)
  41.    
  42.     // If isIn returns true, print out character, if isIn
  43.     // returns false then print out '_'
  44.     // you can use variable like:
  45.     // boolean success = true;
  46.     // Whenever you output atleast one '_' you can set success = false;
  47.     // code can return variable success and it will return true if
  48.     // the user has picked all letters
  49.    
  50.     boolean printCurrStatus(String strToGuess, String userInputs)
  51.     {
  52.         boolean success = false, is = true;
  53.         int length = strToGuess.length();
  54.        
  55.         System.out.print("Current Status for userInputs=" + userInputs);
  56.         System.out.println("");
  57.        
  58.        
  59.         for (int i = 0; i < length; i++)
  60.         {
  61.            
  62.             success = isIn(strToGuess.charAt(i), userInputs);
  63.            
  64.             if (success == true) {
  65.                 System.out.print(strToGuess.charAt(i));
  66.                 System.out.print(" ");
  67.             }
  68.             else
  69.             {
  70.                 System.out.print("_");
  71.                 System.out.print(" ");
  72.                 is = false;
  73.             }
  74.         }
  75.     return is; 
  76.     }
  77.    
  78.     // this routine returns random string from list of words
  79.     // use array list????
  80.    
  81.     String getNextWordToGuess()
  82.     {
  83.         final int num_words = 10;
  84.        
  85.         int num = rand.nextInt(num_words);
  86.         // int num = (int)(num_words* Math.random());
  87.        
  88.         String[] words = new String[] {"elephant", "tiger", "monkey", "baboon", "barbeque",
  89.                 "giraffe", "simple", "zebra", "porcupine", "aardvark"};
  90.        
  91.         return words[num];
  92.        
  93.     }
  94.    
  95.     // this next routine plays the hangman game. it calls
  96.     // getNextWordToGuess to
  97.     // get the word that should be guessed. It then has a loop which
  98.     // outputs:
  99.     //"Enter next letter"
  100.    
  101.     // A string named userInputs stores all letters selected already
  102.     // then the routine printCurrStatus is called to print out current
  103.     // status of
  104.     // the guessed word. If printCurrStatus returns true, we are done.
  105.    
  106.     void playGame()
  107.     {
  108.         String userInputs = "";
  109.         String strToGuess = getNextWordToGuess();
  110.        
  111.         while (printCurrStatus(strToGuess, userInputs) == false)
  112.         {
  113.             System.out.println("");
  114.             System.out.print("Enter next letter: ");
  115.             userInputs = userInputs + keyboard.next();
  116.             System.out.println("");
  117.         }
  118.  
  119.     }
  120.  
  121.    
  122.     public static void main(String[] args)
  123.     {
  124.         Hangman1 hangman = new Hangman1();
  125.        
  126.         String response="";
  127.         do
  128.         {
  129.             hangman.playGame();
  130.             System.out.println("");
  131.             System.out.println("");
  132.             System.out.println("Congratulations! You guessed the word!");
  133.             System.out.print("Do you want to play object oriented Hangman again? (y or n): ");
  134.             response = hangman.keyboard.next();
  135.         } while (response.charAt(0) == 'y');
  136.        
  137.         System.out.println("Bye");
  138.        
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement