Advertisement
wicastle

Untitled

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