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

Username:   Password: 
 RegisterRegister   
 Help correct my hangman code PLEASE!!
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Miko99




PostPosted: Tue Apr 22, 2003 9:52 am   Post subject: Help correct my hangman code PLEASE!!

Please correct my code. I am lost and do not know what to do!

code:


const VOWELS : string := "AEIOUaeiou"
const CONSONANTS : string := "BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz"

var word : string
var mic : string := ""
var guessedLetters : string :=""
var count:int:=0
var ans:string
var guess1:string
var wrong:int:=0

put "player 1 please enter a word use uppercase letters"
get word


put " _  stands for a consonant"
put " =  stands for a vowel "
put skip

for x:1..8
    count := count + 1
   
   
    for i : 1 .. length (word)


        if index (guessedLetters, word (i)) not= 0 then
            mic := mic + word (i)


        elsif index (CONSONANTS, word (i)) not= 0 then

            mic := mic + "-"
        elsif index (VOWELS, word (i)) not= 0 then

            mic := mic + "="

        else


            mic := mic + word (i)
        end if

    end for
   

    put " the word to guess is ", mic
   

    exit when mic = word


    var guess : string
    put "please enter your guess " ..
    get guess
   
    guessedLetters := guessedLetters + guess
    mic := ""
    if guess = mic then
    put "Nice Job you got a letter"
   
    else
    put "sorry"
    wrong:=wrong+1
if wrong=1 then
drawarc(350,150,20,20,0,360,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)   

elsif wrong=2 then
drawarc(350,150,20,20,0,360,3)
drawline(350,130,350,90,3)
drawline(350,110,360,120,3)
drawline(350,110,340,120,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)

elsif wrong=3 then
drawarc(350,150,20,20,0,360,3)
drawline(350,130,350,70,3)
drawline(350,110,360,120,3)
drawline(350,110,340,120,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)

elsif wrong=4 then
drawarc(350,150,20,20,0,360,3)
drawline(350,130,350,70,3)
drawline(350,110,360,120,3)
drawline(350,110,340,120,3)
drawline(350,70,360,60,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)

elsif wrong=5 then
drawarc(350,150,20,20,0,360,3)
drawline(350,130,350,70,3)
drawline(350,110,360,120,3)
drawline(350,110,340,120,3)
drawline(350,70,360,60,3)
drawline(350,70,340,60,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)

elsif wrong=6 then
drawarc(350,150,20,20,0,360,3)
drawline(350,130,350,70,3)
drawline(350,110,360,120,3)
drawline(350,110,340,120,3)
drawline(350,70,360,60,3)
drawline(350,70,340,60,3)
drawline(340,120,330,130,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)

elsif wrong=7 then
drawarc(350,150,20,20,0,360,3)
drawline(350,130,350,70,3)
drawline(350,110,360,120,3)
drawline(350,110,340,120,3)
drawline(350,70,360,60,3)
drawline(350,70,340,60,3)
drawline(340,120,330,130,3)
drawline(360,120,370,130,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)

else
drawarc(350,150,20,20,0,360,3)
drawline(350,130,350,70,3)
drawline(350,110,360,120,3)
drawline(350,110,340,120,3)
drawline(350,70,360,60,3)
drawline(350,70,340,60,3)
drawline(340,120,330,130,3)
drawline(360,120,370,130,3)
drawline(340,60,330,50,3)
drawline (300,50,300,220,3)
drawline (300,220,350,220,3)
drawline (350,220,350,170,3)
end if

   
    exit when wrong=9
end if

end for
put "Sorry thats 8 guesses! GUESS THE WORD!!"
get guess1

if (guess1=word) then

put "CONGRATS! you guessed it! the word was ",word

put skip, "thanks for playing"

else

put"HAHAHAHAAHA!!! YOU SUCK!!!!! The word was ",word
end if
Sponsor
Sponsor
Sponsor
sponsor
Blade




PostPosted: Tue Apr 22, 2003 10:48 am   Post subject: (No subject)

gotta post what you are having trouble with... i looked and id dint find a problem, other than the not being able to guess with uppercase or lowercase... but i dont know if thats what you want..
Miko99




PostPosted: Wed Apr 23, 2003 9:01 am   Post subject: (No subject)

My problem is that when i put it a right letter, it reads it as wrong and gives me another body part. WTF is with that?
Blade




PostPosted: Wed Apr 23, 2003 10:48 am   Post subject: (No subject)

Quote:
guessedLetters := guessedLetters + guess
mic := ""
if guess = mic then
put "Nice Job you got a letter"

that part right there.... you are comparing guess to mic when mic = the hidden word, up top... i dunno how efficient this is but this is what i'd do..
code:
for i:1..length(word)
if(index(word(i..*),guess) = 1)then
%return found
else
%return unfound, therefore wrong guess
end if
end for


oh and by the way, this code is an example, it shouldnt work (theres people who discrminate because i give non-working code)
Tony




PostPosted: Wed Apr 23, 2003 11:10 am   Post subject: (No subject)

in blade's example, once the letter is found, the value of i is also character's position. Though I donno why he put that inside the loop Confused
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Blade




PostPosted: Wed Apr 23, 2003 11:43 am   Post subject: (No subject)

i put it in a loop because it checks for more than one occurance of the letter... hence index(word(i .. *),pattern)

but in his code he doesnt need it... for some reason i was thinking of mine... i used it because i had a boolean array for each of the letters, if the letter was marked as false it would display a "-" if it was marked as true, it would display the correct letter lol so take out the loop, and the subscript of word

code:
if(index(word,guess) not= 0)then
%return found
else
%return unfound, therefore wrong guess
end if
Tony




PostPosted: Wed Apr 23, 2003 4:28 pm   Post subject: (No subject)

thats how I did my hangman too. Boolean array for letters.

its just that if you're to look for more then one occurance, instead of having a loop with index, you just continue from the place first match was found. Such as if first match is 10th letter, you dont need to check first 9... just makes it for a more efficient program Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Blade




PostPosted: Wed Apr 23, 2003 4:35 pm   Post subject: (No subject)

but if it finds more than one, or two, then you're gonna have to use a loop to check more than once... right?
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Apr 23, 2003 4:38 pm   Post subject: (No subject)

ya, a loop... with exit when counter > length(word)

then inside the loop itself you use

index(word(counter..*),guess)

if that yields a positive result, you guessed another letter and reset the counter to the return value of index. Otherwise you quit the loop.

as for the for loop. You might as well just compare individual letters, no point in looking for a match just to see if it was found on first letter. Waste of resourses.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Miko99




PostPosted: Wed Apr 23, 2003 5:50 pm   Post subject: (No subject)

Ok so can someone show me what to put in to my code to get it working right and where to enter this correction.
Blade




PostPosted: Wed Apr 23, 2003 6:28 pm   Post subject: (No subject)

Quote:
if guess = mic then
put "Nice Job you got a letter"

instead of if guess = mic then do
code:
if index(word, guess) not= 0 then


you gotta check to see if guess is in the word... you cant compare your guess to your hidden word
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  [ 11 Posts ]
Jump to:   


Style:  
Search: