Guest User

Untitled

a guest
Apr 23rd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. import java.util.*;
  2. /***********************************************************
  3. * WordGuess game is a Hangman like game for two (or more)
  4. * players. This is a two player version
  5. *
  6. * @author Nick Bennett
  7. * @version V1 Shell to get you started
  8. ***********************************************************/
  9.  
  10. public class WordGuess
  11. {
  12. private Scanner keyboard; // use for all keyboard entryo
  13.  
  14. private WordBank wordList; // the dictionary to use
  15.  
  16. /* alternate instance variable for the players
  17. deactivate the individual players if you choose to
  18. use this version. */
  19. private Player[] players;
  20.  
  21.  
  22. /*********************************************************
  23. * Explicit value constructor that builds a random dictioinary
  24. *
  25. * @param player1 The name of player1
  26. * @param player2 The name of player2
  27. ********************************************************/
  28. public WordGuess(String player1, String player2)
  29. {
  30. this.wordList = new WordBank();
  31. this.players = new Player[2];
  32. players[0] = new Player(player1);
  33. players[1] = new Player(player2);
  34. }
  35.  
  36. /*********************************************************
  37. * Explicit value constructor that builds a seeded dictioinary
  38. *
  39. * @param player1 The name of player1
  40. * @param player2 The name of player2
  41. * @param seed The seed to pass to WordBank constructor
  42. ********************************************************/
  43. public WordGuess(String player1, String player2, long seed)
  44. {
  45. this.wordList = new WordBank(seed);
  46. players[0] = new Player(player1);
  47. players[1] = new Player(player2);
  48. }
  49.  
  50. /*********************************************************
  51. * play game plays the game until one player reaches 10 wins
  52. *********************************************************/
  53. public void playGame()
  54. {
  55. /*
  56. keyboard = new Scanner(System.in);
  57. int numPlayers = 2;
  58. int score = 0;
  59. int guess;
  60. int loop = 0;
  61. int counter = 0;
  62. String pGuess = "";
  63. String word = wordList.getWord();
  64. char[] usedLetters = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',' ', ' ',
  65. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
  66. */
  67. System.out.printf("Welcome to Word Guess %s and %s\n\n", players[0].getName(), players[1].getName());
  68.  
  69.  
  70. /////////////////////////////////////////////////////////////////////////////////////////
  71. keyboard = new Scanner(System.in);
  72. String word = wordList.getWord();
  73. String pGuess = ""; // User input read into here
  74. char[] usedLetters;
  75. int i; // General counter we will use in all for() loops
  76. int compareflag1 = 0; // Flag to see if we should run the checking loop (did the user lose?)
  77. int compareflag2 = 0
  78. int guessvalue; // to determine if we should switch players or not
  79. int counter = 0; // keep track of last array element in usedLetters[]
  80.  
  81.  
  82. // we can start a do-loop right before the prompting
  83. // for user to guess a letter. we can check the do-loop
  84. // against a desired score value, and everything from this point
  85. // will loop and shouldn't break:
  86. // do {
  87.  
  88.  
  89. System.out.print(players[loop % 1].getName() + ", guess a letter.\n");
  90. pGuess = keyboard.nextLine(); // Get guess from user (character + newline)
  91.  
  92. for(i = 0; i < usedLetters.length; i++) // Loop 26 times compare guess to each element in usedLetters[]
  93. {
  94. if(pGuess.charAt(0) == usedLetters[i]) // If guess = usedLetters[] tell player he loses, switch players and break out
  95. {
  96. System.out.print(pGuess + " is already guessed. " + players[loop % 1].getName() + ", you lose your turn.\n");
  97. loop++;
  98. compareflag1 = 1;
  99. break;
  100. }
  101. }
  102.  
  103. // If we are here that means our guessed letter is not part
  104. // of usedLetters[] meaning it has never been guessed before.
  105. // This would be a good time to call our function that now takes
  106. // the users guess and does everything with it. That way we can
  107. // just loop this until completion.
  108.  
  109. if(compareflag1 == 0) // compareflag only changes when letter is found in for
  110. { // loop above in which case we shouldn't do whats below
  111. if(pGuess.equalsIgnoreCase("a") || pGuess.equalsIgnoreCase("b") ||pGuess.equalsIgnoreCase("c") ||
  112. pGuess.equalsIgnoreCase("d") || pGuess.equalsIgnoreCase("e") || pGuess.equalsIgnoreCase("f") ||
  113. pGuess.equalsIgnoreCase("g") || pGuess.equalsIgnoreCase("h") || pGuess.equalsIgnoreCase("i") ||
  114. pGuess.equalsIgnoreCase("j") || pGuess.equalsIgnoreCase("k") || pGuess.equalsIgnoreCase("l") ||
  115. pGuess.equalsIgnoreCase("m") || pGuess.equalsIgnoreCase("n") || pGuess.equalsIgnoreCase("o") ||
  116. pGuess.equalsIgnoreCase("p") || pGuess.equalsIgnoreCase("q") || pGuess.equalsIgnoreCase("r") ||
  117. pGuess.equalsIgnoreCase("s") || pGuess.equalsIgnoreCase("t") || pGuess.equalsIgnoreCase("u") ||
  118. pGuess.equalsIgnoreCase("v") || pGuess.equalsIgnoreCase("w") || pGuess.equalsIgnoreCase("x") ||
  119. pGuess.equalsIgnoreCase("y") || pGuess.equalsIgnoreCase("z"))
  120. {
  121. // What happens when your word is 'abacus' and it finds the first 'a',
  122. // add to usedLetters[] increase the counter, then goes to the second
  123. // 'a', does the same thing. Is this what we really want?
  124. // Let's break after first instance so stop that from happening
  125. for(i = 0; i < word.length(); i++) // loop through length of word we are guessing
  126. {
  127. // compare individual character of word to player guess
  128. if(word.substring(i).equalsIgnoreCase(pGuess))
  129. {
  130. // Add score to this player somewhere here, before breaking
  131. // set compareflag2 so we know that at least 1 match was made
  132. System.out.print("Correct: " + players[loop % 1].getName() + " go again.\n");
  133. compareflag2 = 1;
  134. break;
  135. }
  136. }
  137.  
  138. // if compareflag2 = 0 means that no matches were made, so switch players
  139. if(compareflag2 == 0)
  140. {
  141. System.out.print("Incorrect. " + players[loop % 1].getName() + ", you lose your turn.\n");
  142. loop++;
  143. }
  144.  
  145. // reset compareflag2, add guess to usedLetters, increase its counter
  146. compareflag2 = 0;
  147. usedLetters[counter] = pGuess.charAt(0);
  148. counter++;
  149. }
  150. }
  151.  
  152. compareflag1 = 0; // . . . and then we run again
  153.  
  154. // we print the guessed word so far
  155. // also print the letters we used
  156. printWord(word, pGuess, usedLetters);
  157. Arrays.sort(usedLetters);
  158.  
  159. System.out.print("\nUsed letters: ");
  160. for(i = 0; i < usedLetters.length; i++)
  161. {
  162. if(usedLetters[i] != ' ')
  163. System.out.print(usedLetters[i]);
  164. }
  165. System.out.print("\n");
  166.  
  167. // below add a conditional if usedLetters == word
  168. // then you know the user won, and you can then
  169. // break out of the do{}while loop if that's true
  170. // =======
  171. // =======
  172. // this will be very hard to do!
  173.  
  174.  
  175. // that do { loop we started a while ago,
  176. // it should end here with a condition after
  177. // it so we know how many times it should run:
  178. // } while(score < 5);
  179.  
  180.  
  181.  
  182. } // end of playGame() function
  183.  
  184. /////////////////////////////////////////////////////////////////////////////////////////
  185.  
  186. // Junk yard
  187.  
  188. // do {
  189. /*
  190.  
  191. System.out.print("\n");
  192. */
  193.  
  194. // } while (pGuess.equalsIgnoreCase(word)); // (closes do-loop) you're comparing pGuess and word? why?
  195.  
  196. /*
  197. if (usedLetters.equals(word)) // the letters you used equal the word? you sort the letters used!
  198. {
  199. System.out.print("Correct. " + players[loop % 1].getName() + " you win a point!\n");
  200. score++;
  201. System.out.println(word);
  202. System.out.println(players[loop % 1].getName() + ": " + score + "\t" + players[loop % 1].getName() + ": " + score);
  203. loop++;
  204. }
  205. */
  206. // } while (score < 5);
  207.  
  208. // System.out.printf("Game over. " + players[loop % 1].getName() + " wins!");
  209.  
  210.  
  211. // You may (and should) add additional methods below. They should
  212. // all be private to this game
  213. private void printWord(String word, String pGuess, char[] usedLetters)
  214. {
  215. int ii;
  216.  
  217. for (ii = 0; ii < word.length(); ii++)
  218. {
  219. if (word.substring(ii, ii + 1).equalsIgnoreCase(pGuess))
  220. {
  221. System.out.print(pGuess);
  222. }
  223. else
  224. System.out.print("-");
  225.  
  226. }
  227. }
  228.  
  229. }
Add Comment
Please, Sign In to add comment