Reputation: 107
Background: I am learning Java through University of Helsinki’s massive open online course (MOOC) program. One of the goals is to build a text based hangman game. Here's some more reference: https://materiaalit.github.io/2013-oo-programming/part1/week-2/
I figured out most of the code. However, the game executes awkwardly. To expand, the game should keep running until the number of guesses or count reaches to 7. So, the while loop keeps working until it reaches 7 or the word is guessed correctly.
Issue: My word to guess is Toyota. The printWord() method hides the word by doing this: ******. Every time the user guesses the letter, it removes the asterisk. So, if I guess the right letter it should do this: *o**o. However, when the user enters the next letter it forgets that 'o' was guessed. So, it currently works like this:
User: o
printWord(): *o*o**
User: T
printWord(): T*****
Here is my main class:
public class main {
public static void main(String[] args) {
String word = "Toyota";
int count = 0;
Hangman hangman = new Hangman();
System.out.println("************");
System.out.println("* Hangman *");
System.out.println("************");
System.out.println("");
hangman.printMenu();
System.out.println("");
Scanner reader = new Scanner (System.in);
String input = reader.nextLine();
if (input.equals("quits")){
System.out.println("Thank you for playing!");
}else{
while (count < 7){
char c = reader.next().charAt(0);
hangman.printWord(c);
hangman.printMan(count);
hangman.printStatus(c);
count++;
}
}
}
}
Here is the hangman class:
public class Hangman {
public static void printMenu(){
System.out.println(" * menu *");
System.out.println("game on - starts the game.");
System.out.println("quit - quits the game");
System.out.println("status - prints the game status");
System.out.println("a single letter uses the letter as a guess");
System.out.println("an empty line prints this menu");
}
public static void printMan(int count){
String word = "Toyota";
if (count == 1) {
System.out.println("Wrong guess, try again");
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("___|___");
System.out.println();
}
if (count == 2) {
System.out.println("Wrong guess, try again");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 3) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println(" | ");
System.out.println("___|___");
}
if (count == 4) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" |");
System.out.println(" |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 5) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" |");
System.out.println("___|___");
}
if (count == 6) {
System.out.println("Wrong guess, try again");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | |");
System.out.println(" | |");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
}
if (count == 7) {
System.out.println("GAME OVER!");
System.out.println(" ____________");
System.out.println(" | _|_");
System.out.println(" | / \\");
System.out.println(" | | |");
System.out.println(" | \\_ _/");
System.out.println(" | _|_");
System.out.println(" | / | \\");
System.out.println(" | / \\ ");
System.out.println("___|___ / \\");
System.out.println("GAME OVER! The word was " + word);
}
}
public static void printStatus(char c){
String mystring = "abcdefghijklmnopqrstuvwxyz";
int count = 0;
int sum = 0;
StringBuilder sb = new StringBuilder();
sb.append(mystring);
for (int i = 0; i < sb.length(); i++){
if (c ==sb.charAt(i)){
sb.deleteCharAt(i);
}
}
count ++;
sum = sum + count;
System.out.println(sb.toString());
System.out.println("The number of guesses are: " + sum);
}
public void printWord(char c){
String word = "Toyota";
int count = 0;
char [] blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
if (word.contains(c + "")){
for (int j = 0; j <word.length(); j++ ){
if (word.charAt(j) ==c){
blank[j] = c;
}
count++;
}
}else {
count++;
printMan(count);
System.out.println("Wrong guess");
}
if (word.equals(String.valueOf(blank))){
System.out.println(blank);
System.out.println("You won");
}
System.out.println(blank);
}
public static void gameOn(){
int count = 0;
if (count < 7){
System.out.println("Keep Trying. You can get this");
System.out.println(count);
}else {
System.out.println("Game Over bud");
System.out.println(count);
}
}
}
I was assuming that making the method printWord() static was the issue as making a new instance of the method would reset everything. However, removing that keyword didn't work. For some reason, printStatus can keep track of the letter used, but not the actual count.
Any help is always appreciated. Thank you!
Upvotes: 0
Views: 70
Reputation: 1341
Make word and blank global variables and a constructor that sets the variables when you call the constructor. Like below.
Move the lines where you create the blank char array in printWord()
to the constructor. Because every time you call printWord
you are creating a new blank array or replacing the old one that is why printing blank it is incorrect. You only want to set the blank char array once, the construction Hangman()
would be the best place for this as this is called only once when you create the hangman object (Hangman hangman = new Hangman();
). I hope it makes sense.
Let me know if this solved your problem.
Add/Update Code
public class Hangman {
private String word;
private char[] blank;
private int guesses;
public Hangman() {
word = "Toyota";
blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
guesses = 0;
}
// Optional Constructor - Hangman hangman = new Hangman("Toyota");
public Hangman(String word) {
this.word = word;
blank = new char [word.length()];
for (int i =0; i < word.length(); i++){
blank[i] = '*';
}
guesses = 0;
}
/**
* Rest of your code
*/
}
Remove Code - Remove the commented lines marked from printWord()
method
//String word = "Toyota";
int count = 0;
//char [] blank = new char [word.length()];
//for (int i =0; i < word.length(); i++){
// blank[i] = '*';
//}
Updated - Changes in printStatus
public static void printStatus(char c){
String mystring = "abcdefghijklmnopqrstuvwxyz";
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append(mystring);
for (int i = 0; i < sb.length(); i++){
if (c ==sb.charAt(i)){
sb.deleteCharAt(i);
}
}
guesses++;
System.out.println(sb.toString());
System.out.println("The number of guesses are: " + guesses);
}
Upvotes: 1