Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. //Created by Cody Smith
  2. //This program used a static approach to create hangman
  3.  
  4. package hangman;
  5.  
  6. import java.util.*;
  7.  
  8.  
  9. public class static_approach
  10.     {
  11.     static Scanner keyboard = new Scanner(System.in);
  12.     static Random rand = new Random();
  13.    
  14. static boolean isIn(char c, String str)
  15.     {
  16.     for (int i=0; i<str.length(); i++) {
  17.         if (str.substring(i, i+1).equals("c"));
  18.        }
  19.       return true;
  20.     }
  21. static boolean printCurrStatus(String strToGuess, String userInputs)
  22.     {
  23.     int i, p=0;
  24.     char letter;
  25.     char strLetter;
  26.     letter = '-';
  27.     char [] carray = new char[10];
  28.    
  29.    
  30.  
  31.    for (i = 0; i < strToGuess.length(); i++)
  32.    {
  33.    
  34.      carray[i]='-';
  35.      System.out.print(carray[i]);
  36.    
  37.     }
  38.    
  39.    do
  40.    {
  41.    
  42.    System.out.println("");
  43.    System.out.println("Current Status for userInputs= " + userInputs );
  44.    System.out.println("Enter next letter:");
  45.    
  46.    Scanner in = new Scanner (System.in);
  47.    String strCh= in.nextLine();
  48.    strCh = strCh.trim();
  49.    userInputs+=strCh;
  50.    
  51.     // Check if letter already have been entered by user before
  52.  
  53.         for (i = 0; i < strToGuess.length(); i++) {
  54.                  
  55.             if (strCh.length() != 0)
  56.             letter = strCh.charAt(0);
  57.             strLetter = strToGuess.charAt(i);
  58.            
  59.             if (isIn(letter, strToGuess))
  60.              if(letter== strLetter)
  61.              {
  62.                 carray[i]= letter;
  63.                 p +=1;
  64.                  }
  65.            
  66.                        
  67.         }
  68.         for (i=0; i < strToGuess.length();i++)
  69.          System.out.print(carray[i]);
  70.        
  71.    } while (p < strToGuess.length() );
  72.    System.out.println("");
  73.    System.out.println("Current Status for userInputs= " + userInputs );
  74.    //System.out.println(" P " + p + " len " + strToGuess.length());
  75.     return true;
  76.     }
  77. static String getNextWordToGuess()
  78.     {
  79.      final int num_words=10; // change this if you have a different number of words
  80.  
  81.      int num = rand.nextInt(num_words);
  82.  
  83.      // Another way to accomplish the same thing:
  84.  
  85.      // int num = (int)(num_words* Math.random());
  86.  
  87.      List<String> my_words = new LinkedList<String>();
  88.  
  89.      
  90.  
  91.         my_words.add("elephant");
  92.  
  93.         my_words.add("tiger");
  94.  
  95.         my_words.add("monkey");
  96.  
  97.         my_words.add("baboon");
  98.  
  99.         my_words.add("barbeque");
  100.  
  101.         my_words.add("giraffe");
  102.  
  103.         my_words.add("simple");
  104.  
  105.         my_words.add("zebra");
  106.  
  107.         my_words.add("porcupine");
  108.  
  109.         my_words.add("aardvark");
  110.  
  111.    
  112.  
  113.        
  114.  
  115.         Random rg = new Random();
  116.  
  117.         String randomElement="";
  118.  
  119.         int listSize = my_words.size();
  120.        //while(listSize <= 10) {
  121.  
  122.             randomElement = my_words.get(rg.nextInt(listSize));
  123.      
  124.  
  125.             System.out.println("Word is : "+ randomElement);
  126.  
  127.            // Thread.sleep(SLEEP_TIME)
  128.  
  129.         //}
  130.             return randomElement;
  131.  
  132.     }
  133. static void playGame()
  134.     {
  135.     String strToGuess = getNextWordToGuess();
  136.     String userInputs="";
  137.    
  138.     printCurrStatus(strToGuess, userInputs);
  139.     }
  140. public static void main(String[] args)
  141.     {
  142.         String response="";
  143.         do
  144.         {
  145.             playGame();
  146.             System.out.print("Do you want to play static approach Hangman again? (y or n): ");
  147.             response = keyboard.next();
  148.         } while (response.charAt(0) == 'y');
  149.        
  150.         System.out.println("Bye!");
  151.        
  152.     }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement