Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Student Version
- //Name: Selynna Sun
- //Period: 4
- public class Hangman {
- public static void main(String[] args) {
- System.out.println("Welcome to Hangman!");
- System.out.println("Once you guess incorrectly 10 times, you die.");
- String secretWord = WordFinder.getWordFromDictionary();
- System.out.println(secretWord); //uncomment this line to see the secret word while playing
- String wordWithBlanks = convertWordToBlanks(secretWord);
- int numIncorrectGuesses = 0;
- System.out.println(wordWithBlanks);
- System.out.print("Please guess a character: ");
- char input = TextIO.getlnChar();
- if (secretWord.contains("" + input)) {
- wordWithBlanks = replaceBlanksWithChar(wordWithBlanks, secretWord, input);
- }
- else {
- System.out.println("The secret word does not contain " + input);
- numIncorrectGuesses++;
- System.out.println("You have made " + numIncorrectGuesses + " incorrect guesses.");
- }
- if (countNumberOfBlanks(wordWithBlanks) == 0) {
- System.out.println("You win!");
- System.out.println("The secret word was " + secretWord);
- }
- else if (numIncorrectGuesses >= 10) {
- System.out.println("You lose!");
- System.out.println("The secret word was " + secretWord);
- }
- System.out.println("Would you like to play again?");
- String inp = null;
- if (inp.equals("y")) {
- secretWord = WordFinder.getWordFromDictionary();
- System.out.println(secretWord); //uncomment this line to see the secret word while playing
- wordWithBlanks = convertWordToBlanks(secretWord);
- numIncorrectGuesses = 0;
- System.out.println(wordWithBlanks);
- System.out.print("Please guess a character: ");
- input = TextIO.getlnChar();
- }
- else {
- System.out.println("Thanks for playing!");
- }
- }
- /**
- * Example: convertWordToBlanks("dollop") returns "______"
- * Example: convertWordToBlanks("") returns ""
- * Example: convertWordToBlanks("The red hat") returns "___________"
- * Example: convertWordToBlanks("r__ei__") returns "_______"
- */
- public static String convertWordToBlanks(String word) {
- String blank = "_";
- int rep = 0;
- if (word.length() == 0) {
- return "_";
- }
- else {
- while (word.length() > 1) {
- blank = blank + blank;
- rep++;
- }
- return blank;
- }
- }
- /**
- * Example: countNumberOfBlanks("__b_l_") returns 4
- * Example: countNumberOfBlanks("") returns 0
- * Example: countNumberOfBlanks("hello") returns 0
- * Example: countNumberOfBlanks("t_e_a_y") returns 3 */
- public static int countNumberOfBlanks(String word) {
- int counter = 0;
- while (word.length() > 0) {
- int substring;
- for (substring = 0; substring< word.length(); substring++)
- word.substring(substring, substring + 1);
- if (word.substring(substring, substring + 1).equals("_"));
- counter++;
- }
- return counter;
- }
- /** Example: replaceBlanksWithChar("__b_l_", "subtle", 't') returns "__btl_"
- * Example: replaceBlanksWithChar("su__er", "supper", 'p') returns "supper"
- * Example: replaceBlanksWithChar("__i____", "trident", 's') returns "__i____"
- * Example: replaceBlanksWithChar("_______", "hepatitis", 'i') returns "_____i_is"
- */
- public static String replaceBlanksWithChar(String wordWithBlanks, String secretWord, char ch) {
- ch = TextIO.getChar();
- int substring;
- for (substring = 0; substring< secretWord.length();substring++)
- secretWord.substring(substring,substring+1);
- if (secretWord.substring(substring,substring+1).equals(ch)) {
- return wordWithBlanks + ch;
- }
- return replaceBlanksWithChar(wordWithBlanks, secretWord.substring(0,1), ch);//fix
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement