Advertisement
Guest User

Untitled

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