Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Hangman.java
- * ------------------
- * This program will eventually play the Hangman game from
- * Assignment #4.
- */
- import acm.graphics.*;
- import acm.program.*;
- import acm.util.*;
- import java.awt.*;
- public class Hangman extends ConsoleProgram {
- RandomGenerator rand = new RandomGenerator();
- HangmanLexicon lex = new HangmanLexicon();
- String secretWord = "";
- String currentWord = "";
- char guessedLetter;
- int turns = 0;
- boolean wordGuessed = false;
- int lettersLeftToGuess = 0;
- /* ONLY TO BE ADDED IF EXPORT TO JAR FILE (+ THEN CREATE MANIFEST FILE!)
- * public static void main(String[] args) {
- new Hangman().start(args);
- }*/
- public void run() {
- setupTheGame();
- //GUESSING LETTERS UNTIL EITHER NO TURNS LEFT OR WORD GUESSED
- while (turns < 8 && lettersLeftToGuess != 0) {
- getValidGuess();
- //check if letter is part of word and output result
- checkLetterAndUpdate();
- }
- //OUTPUT WIN OR FAIL MESSAGE
- if (turns == 8 && lettersLeftToGuess != 0) {
- println("You have no more turns left. You loose.");
- println("Restart the program to try again with a different word.");
- println("The word was " + secretWord);
- } else {
- println("Correct! You guessed the word after " + turns + " turns. To play again, restart the program.");
- }
- }
- private void checkLetterAndUpdate() {
- String guessedString = Character.toString(guessedLetter);
- int letterIndex = secretWord.indexOf(guessedString);
- if (letterIndex == -1) {
- turns ++;
- println("Sorry, that letter is not included in the word. Try another one.");
- println("You have " + (8 - turns) + " turns to guess the word.");
- println("Here is the current word: " + currentWord);
- println("");
- } else {
- currentWord = replaceAllOccurences(secretWord, currentWord, guessedString);
- println("Your guess was correct!");
- println("Here is the current word: " + currentWord);
- println("You have " + (8 - turns) + " turns to guess the word.");
- println("");
- }
- }
- private String replaceAllOccurences(String secretW, String currentW,
- String guessedString) {
- String result = currentW;
- int pos = 0;
- int index = secretW.indexOf(guessedString, pos);
- while (index != -1) {
- //assign index of char in secret word
- pos = index;
- //generate start and end STRINGS in currentW
- String start = currentW.substring(0, pos);
- String end = currentW.substring(pos + 1);
- //concatenate them in the result
- result = start + guessedString + end;
- //UP TO HERE IT WORKS
- //check anew if the string is again in the resulting string
- index = result.indexOf(guessedString, pos + 1);
- lettersLeftToGuess --;
- }
- return result;
- }
- private void setupTheGame() {
- //greet user
- println("Hello. You can play the hangman game here!");
- //choose a random word
- int indexOfSecretWord = rand.nextInt(0, lex.getWordCount());
- secretWord = lex.getWord(indexOfSecretWord);
- println("A random word has been chosen for you.");
- lettersLeftToGuess = secretWord.length();
- //print out blank current word
- for (int i = 0; i < secretWord.length(); i++) currentWord += "_ ";
- secretWord = secretWord.replace("", " ").trim();
- println("The word now looks like this: " + currentWord);
- }
- private void getValidGuess() {
- //ask until you get a valid guess
- while (true) {char guess = readLine("Enter your guess: ").charAt(0);
- if (Character.isLetter(guess)) {
- guessedLetter = Character.toUpperCase(guess);
- break;
- }
- else println("Your guess was not a valid letter. Please try again.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement