Advertisement
ssun098

Untitled

Sep 21st, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. //Student Version
  2. //Name: Selynna Sun
  3. //Period: 4
  4.  
  5. public class Hangman {
  6.  
  7.  
  8. public static void main(String[] args) {
  9. System.out.println("Welcome to Hangman!");
  10. System.out.println("Once you guess incorrectly 10 times, you die.");
  11. String secretWord = WordFinder.getWordFromDictionary();
  12. System.out.println(secretWord); //uncomment this line to see the secret word while playing
  13. String wordWithBlanks = convertWordToBlanks(secretWord);
  14. int numIncorrectGuesses = 0;
  15. System.out.println(wordWithBlanks);
  16. System.out.print("Please guess a character: ");
  17. char input = TextIO.getlnChar();
  18. if (secretWord.contains("" + input)) {
  19. wordWithBlanks = replaceBlanksWithChar(wordWithBlanks, secretWord, input);
  20. }
  21. else {
  22. System.out.println("The secret word does not contain " + input);
  23. numIncorrectGuesses++;
  24. System.out.println("You have made " + numIncorrectGuesses + " incorrect guesses.");
  25. }
  26.  
  27.  
  28. if (countNumberOfBlanks(wordWithBlanks) == 0) {
  29. System.out.println("You win!");
  30. System.out.println("The secret word was " + secretWord);
  31. }
  32. else if (numIncorrectGuesses >= 10) {
  33. System.out.println("You lose!");
  34. System.out.println("The secret word was " + secretWord);
  35. }
  36.  
  37. System.out.println("Would you like to play again?");
  38. String inp = null;
  39. if (inp.equals("y")) {
  40. secretWord = WordFinder.getWordFromDictionary();
  41. System.out.println(secretWord); //uncomment this line to see the secret word while playing
  42. wordWithBlanks = convertWordToBlanks(secretWord);
  43. numIncorrectGuesses = 0;
  44. System.out.println(wordWithBlanks);
  45. System.out.print("Please guess a character: ");
  46. input = TextIO.getlnChar();
  47. }
  48. else {
  49. System.out.println("Thanks for playing!");
  50. }
  51.  
  52. }
  53.  
  54.  
  55. /**
  56. * Example: convertWordToBlanks("dollop") returns "______"
  57. * Example: convertWordToBlanks("") returns ""
  58. * Example: convertWordToBlanks("The red hat") returns "___________"
  59. * Example: convertWordToBlanks("r__ei__") returns "_______"
  60. */
  61. public static String convertWordToBlanks(String word) {
  62. String blank = "_";
  63. int rep = 0;
  64. if (word.length() == 0) {
  65. return "_";
  66. }
  67. else {
  68. while (word.length() > 1) {
  69. blank = blank + blank;
  70. rep++;
  71.  
  72. }
  73. return blank;
  74. }
  75. }
  76.  
  77. /**
  78. * Example: countNumberOfBlanks("__b_l_") returns 4
  79. * Example: countNumberOfBlanks("") returns 0
  80. * Example: countNumberOfBlanks("hello") returns 0
  81. * Example: countNumberOfBlanks("t_e_a_y") returns 3 */
  82.  
  83. public static int countNumberOfBlanks(String word) {
  84. int counter = 0;
  85. while (word.length() > 0) {
  86. int substring;
  87. for (substring = 0; substring< word.length(); substring++)
  88. word.substring(substring, substring + 1);
  89. if (word.substring(substring, substring + 1).equals("_"));
  90. counter++;
  91.  
  92. }
  93. return counter;
  94. }
  95.  
  96.  
  97. /** Example: replaceBlanksWithChar("__b_l_", "subtle", 't') returns "__btl_"
  98. * Example: replaceBlanksWithChar("su__er", "supper", 'p') returns "supper"
  99. * Example: replaceBlanksWithChar("__i____", "trident", 's') returns "__i____"
  100. * Example: replaceBlanksWithChar("_______", "hepatitis", 'i') returns "_____i_is"
  101. */
  102. public static String replaceBlanksWithChar(String wordWithBlanks, String secretWord, char ch) {
  103. ch = TextIO.getChar();
  104. int substring;
  105. for (substring = 0; substring< secretWord.length();substring++)
  106. secretWord.substring(substring,substring+1);
  107. if (secretWord.substring(substring,substring+1).equals(ch)) {
  108. return wordWithBlanks + ch;
  109. }
  110. return replaceBlanksWithChar(wordWithBlanks, secretWord.substring(0,1), ch);//fix
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement