Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 I'm having problem with my loop statements
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
KONjbnj




PostPosted: Mon Jun 06, 2005 8:38 am   Post subject: I'm having problem with my loop statements

I'm doing a project for schoool, and I have an loop statement in there. It's a hangman game, and they have to guess letters. If they guess a correct letter, it adds one to a variable I call "count". It keeps asking the user for letters until they either they guess all correct amount of letters and the "count" reaches a certain point, or if they guess five wrong letters which would complete the hanged man picture. The problem is that sometimes people become forgetful and guess a letter twice. If they guess a correct letter twice, the word would not be complete because it counts the letter, say "s", twice.

Is there a way so that once the user guesses a letter in a loop and it adds one to "count", that if the user guesses it again it would not add another one to "count"?
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Mon Jun 06, 2005 2:53 pm   Post subject: (No subject)

Oh-[pause]-my-[pause]-Gawd!

I just had a flash of brilliance.
But first:
- please post your problematic code next time (using [code] or [syntax] tags). Though your descption may be adequete (as it is in this case), often is the case that it is not.
Your problem is this:
Each time you enter a letter, it is checked with the actual word to see if it exists. Once a letter is correctly guessed, it should not be available to guess anymore.

So you could always restrict input. But that could take a long time, and you'd have to have it dynamic to keep up with all entries.

So, instead you do some very smart programming.
"Wallpaper"
- guess 'W'
Ok, 'W' exists, and has been guessed, so let's not ever consider 'W' again...
new word: "allpaper"
- guess 'a'
It is present...
new word: "llpper"
- and so on and so forth.

Let's say we're at "ller" now
- guess 'p'
Well, that's been checked before. How do we know?
guess stack: "Wap"
How did that get there? Catenation!

In other words, do some really fancy string manipulation and you're set.

You may applaud now.
AsianSensation




PostPosted: Mon Jun 06, 2005 3:03 pm   Post subject: (No subject)

yes, there is. If you do a little string manipulation you will definitely be able to do that.

First, create a string variable to hold all the used letters, and then everytime you input a letter, check to see if you used it already by using the index command. If you didn't, then add what you've inputted into used string and add 1 to your counter.

code:
var used := ""
loop
    get ans
    if index (used, ans) = 0 then
        used += ans
        count += 1
    end if
end loop


Edit: this is what I get for dillying around when posting, Razz
[Gandalf]




PostPosted: Mon Jun 06, 2005 3:55 pm   Post subject: (No subject)

Not bad... not bad at all...

But then again, you could do it the more simple way and output the correct letters guessed onto the screen so that the user knows which ones he already guessed Confused.
Delos




PostPosted: Mon Jun 06, 2005 4:22 pm   Post subject: (No subject)

Yes you could, but don't forget the following mantra:

"The User
Is stupid
The User
Knows nothing
The User's
Only purpose
Is to crash and frag your programme"
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: