Advertisement
elkoholick

Hangman.java (console only, CS106A)

Aug 20th, 2012
530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. /*
  2.  * File: Hangman.java
  3.  * ------------------
  4.  * This program will eventually play the Hangman game from
  5.  * Assignment #4.
  6.  */
  7.  
  8. import acm.graphics.*;
  9. import acm.program.*;
  10. import acm.util.*;
  11.  
  12. import java.awt.*;
  13.  
  14. public class Hangman extends ConsoleProgram {
  15.    
  16.     RandomGenerator rand = new RandomGenerator();
  17.     HangmanLexicon lex = new HangmanLexicon();
  18.     String secretWord = "";
  19.     String currentWord = "";
  20.     char guessedLetter;
  21.     int turns = 0;
  22.     boolean wordGuessed = false;
  23.     int lettersLeftToGuess = 0;
  24.    
  25.     /* ONLY TO BE ADDED IF EXPORT TO JAR FILE (+ THEN CREATE MANIFEST FILE!)
  26.      * public static void main(String[] args) {
  27.         new Hangman().start(args);
  28.     }*/
  29.  
  30.     public void run() {
  31.        
  32.         setupTheGame();
  33.        
  34.         //GUESSING LETTERS UNTIL EITHER NO TURNS LEFT OR WORD GUESSED
  35.         while (turns < 8 && lettersLeftToGuess != 0) {
  36.            
  37.             getValidGuess();
  38.            
  39.             //check if letter is part of word and output result
  40.             checkLetterAndUpdate();
  41.            
  42.         }
  43.        
  44.         //OUTPUT WIN OR FAIL MESSAGE
  45.         if (turns == 8 && lettersLeftToGuess != 0) {
  46.             println("You have no more turns left. You loose.");
  47.             println("Restart the program to try again with a different word.");
  48.             println("The word was " + secretWord);
  49.         } else {
  50.             println("Correct! You guessed the word after " + turns + " turns. To play again, restart the program.");
  51.        
  52.         }
  53.     }
  54.  
  55.     private void checkLetterAndUpdate() {
  56.        
  57.         String guessedString = Character.toString(guessedLetter);
  58.         int letterIndex = secretWord.indexOf(guessedString);
  59.         if (letterIndex == -1) {
  60.             turns ++;
  61.             println("Sorry, that letter is not included in the word. Try another one.");
  62.             println("You have " + (8 - turns) + " turns to guess the word.");
  63.             println("Here is the current word: " + currentWord);
  64.             println("");
  65.         } else {
  66.             currentWord = replaceAllOccurences(secretWord, currentWord, guessedString);
  67.             println("Your guess was correct!");
  68.             println("Here is the current word: " + currentWord);
  69.             println("You have " + (8 - turns) + " turns to guess the word.");
  70.             println("");
  71.         }
  72.        
  73.     }
  74.  
  75.     private String replaceAllOccurences(String secretW, String currentW,
  76.             String guessedString) {
  77.        
  78.         String result = currentW;
  79.        
  80.         int pos = 0;
  81.        
  82.         int index = secretW.indexOf(guessedString, pos);
  83.            
  84.         while (index != -1) {
  85.            
  86.             //assign index of char in secret word
  87.             pos = index;
  88.            
  89.             //generate start and end STRINGS in currentW
  90.            
  91.             String start = currentW.substring(0, pos);
  92.             String end = currentW.substring(pos + 1);
  93.            
  94.             //concatenate them in the result
  95.             result = start + guessedString + end;
  96.            
  97.             //UP TO HERE IT WORKS
  98.            
  99.             //check anew if the string is again in the resulting string
  100.             index = result.indexOf(guessedString, pos + 1);
  101.            
  102.             lettersLeftToGuess --;
  103.            
  104.         }
  105.        
  106.         return result;
  107.        
  108.     }
  109.  
  110.     private void setupTheGame() {
  111.        
  112.         //greet user
  113.         println("Hello. You can play the hangman game here!");
  114.        
  115.         //choose a random word
  116.         int indexOfSecretWord = rand.nextInt(0, lex.getWordCount());
  117.         secretWord = lex.getWord(indexOfSecretWord);
  118.         println("A random word has been chosen for you.");
  119.         lettersLeftToGuess = secretWord.length();
  120.        
  121.         //print out blank current word
  122.         for (int i = 0; i < secretWord.length(); i++) currentWord += "_ ";
  123.         secretWord = secretWord.replace("", " ").trim();
  124.         println("The word now looks like this: " + currentWord);
  125.        
  126.     }
  127.  
  128.     private void getValidGuess() {
  129.        
  130.         //ask until you get a valid guess
  131.         while (true) {char guess = readLine("Enter your guess: ").charAt(0);
  132.             if (Character.isLetter(guess)) {
  133.                 guessedLetter = Character.toUpperCase(guess);
  134.                 break;
  135.             }
  136.             else println("Your guess was not a valid letter. Please try again.");
  137.         }
  138.        
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement