/CS 302/Labs/HangmanLab/Hangman.java

https://bitbucket.org/crwilcox/university-of-wisconsin-undergraduate-projects · Java · 161 lines · 67 code · 27 blank · 67 comment · 21 complexity · de14039698caab6fb3a951d10df14bda MD5 · raw file

  1. import java.util.Random;
  2. import java.util.Scanner;
  3. public class Hangman {
  4. // DECLARE DATA MEMBERS HERE AND INITIALIZE THE ARRAY OF WORDS
  5. private static String[] words = {"two", "three", "four", "bus", "farva", "hangman", "class"};
  6. private String chosenWord;
  7. private char[] correct;
  8. private char[] incorrect = new char[7];
  9. private int guesses=0;
  10. public String toString(){
  11. }
  12. /**
  13. * constructor
  14. */
  15. public Hangman() {
  16. Random randomWord=new Random();
  17. int wordNumber= randomWord.nextInt(words.length);
  18. chosenWord= words[wordNumber];
  19. correct = new char[chosenWord.length()];
  20. for(int x=0; x<chosenWord.length(); x++){
  21. correct[x]='_';}
  22. }
  23. //////////////////////////////////////////////////////////////////////
  24. // PUBLIC METHODS
  25. //////////////////////////////////////////////////////////////////////
  26. /**
  27. * playGame
  28. *
  29. * Play one game of hangman until
  30. * the user wins (guesses all of the letters in the secret word)
  31. * or loses (guesses 7 incorrect letters):
  32. * draw the current Hangman
  33. * print the correct guesses so far
  34. * print the incorrect guesses so far
  35. * ask the user to guess a letter
  36. * read and process the guess
  37. */
  38. public void playGame() {
  39. // FILL IN CODE HERE
  40. Scanner user= new Scanner(System.in);
  41. }
  42. //////////////////////////////////////////////////////////////////////
  43. // PRIVATE METHODS
  44. //////////////////////////////////////////////////////////////////////
  45. /**
  46. * gameWon
  47. *
  48. * Return true if the user has won the game;
  49. * otherwise, return false.
  50. *
  51. * @return true if the user has won, false otherwise
  52. */
  53. private boolean gameWon() {
  54. for(int x=0; x<chosenWord.length(); x++){
  55. if (correct[x]=='_'){
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * gameLost
  63. *
  64. * Return true if the user has lost the game;
  65. * otherwise, return false.
  66. *
  67. * @return true if the user has lost, false otherwise
  68. */
  69. private boolean gameLost() {
  70. if(guesses>=7) return true;
  71. return false;
  72. }
  73. /**
  74. * gameOver
  75. *
  76. * Return true if the user has either won or lost the game;
  77. * otherwise, return false.
  78. *
  79. * @return true if the user has won or lost, false otherwise
  80. */
  81. private boolean gameOver() {
  82. return(gameWon() || gameLost());
  83. }
  84. /**
  85. * handleGuess
  86. *
  87. * If the guessed letter (parameter ch) is in the secret word
  88. * then add it to the array of correct guesses and tell the user
  89. * that the guess was correct;
  90. * otherwise, add the guessed letter to the array of incorrect guesses
  91. * and tell the user that the guess was wrong.
  92. *
  93. * @param ch the guessed letter
  94. */
  95. private void handleGuess(char ch) {
  96. // FILL IN CODE HERE
  97. for(int x=0; x<chosenWord.length(); x++){
  98. if(chosenWord.charAt(x)==ch){
  99. correct[x]=ch;}
  100. }
  101. }
  102. /**
  103. * printHangman
  104. *
  105. * Print the Hangman that corresponds to the given number of
  106. * wrong guesses so far.
  107. *
  108. * @param numWrong number of wrong guesses so far
  109. */
  110. private void printHangman(int numWrong) {
  111. System.out.println(" ____");
  112. System.out.println(" | |");
  113. if (numWrong > 0) {
  114. System.out.println(" | O");
  115. if (numWrong == 2) {
  116. System.out.println(" | |");
  117. } else if (numWrong == 3) {
  118. System.out.println(" | \\|");
  119. } else if (numWrong >= 4) {
  120. System.out.println(" | \\|/");
  121. }
  122. if (numWrong == 5) {
  123. System.out.println(" | /");
  124. } else if (numWrong == 6) {
  125. System.out.println(" | / \\");
  126. }
  127. }
  128. for (int k = 6 - numWrong; k > 0; k--) {
  129. System.out.println(" |");
  130. }
  131. System.out.println("__|__");
  132. System.out.println();
  133. }
  134. }