View difference between Paste ID: 6XZP7qJ3 and SFXDiM0J
SHOW: | | - or go back to the newest paste.
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
	private RandomGenerator rgen = RandomGenerator.getInstance();
17
	
18
	private String orig;
19
	private String hidden;
20
	private String userInput;
21
	private String lowerInput;
22
	// private String missedLetters = "";  // this is not needed
23
	
24
	private char lastLetter;
25
	private int guess = 8;
26
	
27
	//private boolean guessed = false;    //this is not needed
28
	private boolean GameWon = false;     // added this to stop the program before 8 tries passed if all letters were guessed
29
30
	public class getWord extends HangmanLexicon {
31
    	int rand = rgen.nextInt(getWordCount());
32
    	String str = getWord(rand);
33
    }
34
35
	getWord w = new getWord();
36
	private String getAWord() {
37
    	return(w.str);
38
    }
39
40
    public void run() {
41
    	console();
42
//    	graphics();
43
//    	readLexicon();
44
	}
45
    
46
    private void console() {
47
    	orig = getAWord();
48-
		hidden = orig;   // assign it here, only once. Used then due the game to control win/loss
48+
   		// write dashes in HIDDEN only once. Used then during the game to control win/loss
49
        hidden = orig.replaceAll(".","-");
50
    	interactWithUser();
51
    }
52
53
    private void interactWithUser() {
54
    	println("Welcome to Hangman!");
55
    	gameProcess();
56
    	gameEnd();    // changed to finish game regardless of current status. gameEnd will determine if won or lost.
57
    }
58
    
59
    private void gameProcess() {
60
    	while (guess != 0 || GameWon) {   // added "or if GameWon = true" to exit the loop if won before 8 tries
61
        	println("The word now looks like this: " + hidden);
62
        	checkUserInput();
63
        	println("You have " + guess + " guesses left.");
64
    	}
65
    }
66
67
    private void checkUserInput() {
68
    	userInput = readLine("Your guess: ");
69
    	lastLetter = userInput.charAt(0);
70
		lowerInput = orig.toLowerCase();
71
		if (orig.contains(userInput) || lowerInput.contains(userInput)) {
72
			// guessed = true;   // this is not needed
73
			updateHidden();
74
			checkWin();
75
    	} else {
76
    		guess--;    // placed instead of guessed = false;
77
    	}
78
		// this is not needed
79
		//if (!guessed) {
80
		//	guess--;
81
		//}
82
    }
83
84
	private void updateHidden() {
85-
			if (orig.charAt(i).toLowerCase() == lastLetter.toLowerCase()) {
85+
86-
				// match found, need to replace
86+
			if (Character.toLowerCase(orig.charAt(i)) == lastLetter.toLowerCase() ) {
87-
				hidden.charAt(i) = orig.charAt(i);
87+
				char c = orig.charAt(i);
88
				hidden = replaceChar(hidden, c ,  i);
89
			}
90
		}
91
	}
92
93
	private String replaceChar(String str, char ch, int index) {
94
    	return str.substring(0, index) + ch + str.substring(index+1);
95
	}
96
97
	private void checkWin() {
98
		if (!hidden.contains("-")) {
99
			GameWon =  true;
100
		}
101
	}
102
103
    private void gameEnd() {    // changed from gameLost to gameEnd
104
    	if (guess == 0) {
105
    		println("You're completely hung.");
106
    		println("The word was: " + getAWord());
107
    		println("You lose.");
108
    	} 
109
		if (GameWon) {
110
    		println("Congratulations, you WON!!");
111
			println("The word was: " + getAWord());			
112
		}
113
    }
114
115
}