Advertisement
Guest User

Untitled

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