Quantcast

Jump to content


Photo

Blah!


  • Please log in to reply
2 replies to this topic

#1 Rambo

Rambo
  • 833 posts

Posted 08 March 2010 - 09:28 AM

So I am working on my hangman game, still. I've got my guessing feature working, thanks for the help, I will post my solution shortly in the other topic, but I am now stuck on deducting the amount of guesses left.

Here's an example of what I want it to do:


Say the word is: a b o v e
I have 4 guesses to guess this word.
I guess 'a' so therefore I now have 3 guesses left.

How do I deduct?

I have an array called underscore which holds an underscore for each character. So here is my code:


public int guesses_left() {
        for(int i = 0; i < underscore.length; i++) {
            if(underscore[i] != '_') {
                int amount = underscore.length;
                return amount;
            }
        }
        int default1 = underscore.length - 1;
        return default1;
    }

Suffice to say, it did not work :(

#2 Waser Lave

Waser Lave

  • 25516 posts


Users Awards

Posted 08 March 2010 - 09:48 AM

Well the amount of guesses shouldn't be based on the length of the word should it? In hangman you usually get 10 wrong answers before you lose, so you should have an int variable at the start of the game with a value of 10 then for every wrong guess you take 1 off that until the value equals 0. When it reaches 0 the player loses the game.

If however you're trying to find the number of un-found characters left then you need to declare an int variable of value 0 at the start then just loop through your underscore string and if it's a _ then add 1 to your count variable.

#3 Rambo

Rambo
  • 833 posts

Posted 08 March 2010 - 10:39 AM

Well the amount of guesses shouldn't be based on the length of the word should it? In hangman you usually get 10 wrong answers before you lose, so you should have an int variable at the start of the game with a value of 10 then for every wrong guess you take 1 off that until the value equals 0. When it reaches 0 the player loses the game.

If however you're trying to find the number of un-found characters left then you need to declare an int variable of value 0 at the start then just loop through your underscore string and if it's a _ then add 1 to your count variable.


Having to base the game differently to hangman, hence why I am doing it this way.. Otherwise I would of course be doing it your way.

The second idea sounds like the charm and I shall look at that now and get back to you :)

Got it working :) Thanks!


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users