Guest User

Untitled

a guest
Jan 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. //create new Console
  2. c = new Console ();
  3. f = new Console ();
  4.  
  5. //declare variables
  6. String inputWord; //the word entered by player 1 that player 2 will guess
  7. char inputLetter; //the letter entered by player 2 that is the guess
  8. String category; //the category entered by player 1 which acts as a hint for player 2
  9. String tempString = ""; //a temporary string variable that holds the guess, used to convert char to String so that we can use the replace method
  10. int numGuesses = 0; //counter which keeps track of the incorrect guesses
  11. StringBuffer blanks = new StringBuffer (""); //a string buffer object that prints out the "?"'s based on the length of the word entered by player 1
  12. boolean isWinner; //used in main to check if player 2 has correctly guessed the word, then it will break
  13.  
  14. //print instructions then clear them (working)
  15. instructions();
  16. c.readLine();
  17. c.clear();
  18.  
  19. //get word input from user (working)
  20. c.println("Player 1, please enter the word you would like Player 2 to guess. Please do not enter any spaces or punctuation. It will be deleted.");
  21. inputWord = c.readLine();
  22. inputWord = inputWord.toUpperCase();
  23. c.println("Please enter a category that your word falls under. Don't be too specific!");
  24. category = c.readLine();
  25.  
  26. //print category in other console (working)
  27. f.println("Category: " + category);
  28.  
  29. //draw underscores for each letter of the word (working)
  30. for (int i = 0; i < inputWord.length(); i++)
  31. {
  32. blanks = blanks.append("?");
  33. }//rof
  34.  
  35. //print the underscores out (working)
  36. f.print(blanks.toString());
  37.  
  38. //draw template (working)
  39. drawGallows();
  40.  
  41. //clear screen
  42. c.clear();
  43.  
  44. //prompt for user 2 to enter input
  45. c.println("Player 2, please enter your letter guess in uppercase.");
  46. c.println("If you think you know the word, just type it in!");
  47.  
  48. //main game loop
  49. whileLoop:
  50. while (numGuesses < 7)
  51. {
  52. c.println (numGuesses);
  53. //get letter guess from player 2 (working)
  54. inputLetter = c.readChar();
  55.  
  56. //converts the char to a string so we can use the StringBuffer method replace
  57. tempString = Character.toString(inputLetter);
  58.  
  59. //if the letter is somewhere in the word then replace
  60. if (inputWord.indexOf(inputLetter) != -1)
  61. {
  62.  
  63. //Loop to check every letter in word, outputs letter if letter is located in the word
  64. for (int i = 0; i < inputWord.length(); i++)
  65. {
  66. if (inputWord.charAt(i) == inputLetter)
  67. {
  68. //boolean isLetterdetected is true if theres a match
  69. //isLetterDetected = true;
  70.  
  71. //replaces the underscore with the correctly guessed letter
  72. blanks.replace(i,i+1,tempString);
  73.  
  74. //clear the screen
  75. f.clear();
  76.  
  77. //reprint the template and category
  78.  
  79. drawGallows();
  80. f.println("Category: " + category);
  81.  
  82. //print the new stringbuffer
  83. f.print(blanks.toString());
  84.  
  85. }//fi
  86.  
  87. }//rof
  88.  
  89. }//fi
  90.  
  91. //else the letter is not anywhere in the word so increase counter, and draw body part
  92. else
  93. {
  94. numGuesses++;
  95. c.println (numGuesses);
  96. drawBodyPart(numGuesses);
  97. }
  98.  
  99. //check if player 2 has correctly solved the word
  100. forLoop:
  101. for (int i = 0; i < blanks.length(); i++)
  102. {
  103. if (blanks.indexOf("?") == -1)
  104. {
  105. isWinner = true;
  106. results (numGuesses, isWinner, inputWord);
  107. break whileLoop;
  108. }//fi
  109. else
  110. {
  111. break forLoop;
  112. }//esle
  113. }//rof
  114.  
  115. }//elihw
Add Comment
Please, Sign In to add comment