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

Username:   Password: 
 RegisterRegister   
 Hangman Game
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
hossack




PostPosted: Wed Mar 25, 2009 9:02 am   Post subject: Hangman Game

So when I restart the game, and I guess a letter wrong, it doesn't start drawing the stickman. To me it should because the procedure restarts, but it doesn't work any advice?

Turing:
View.Set ("nocursor")
var stream, pos, offset, randy, file : int
var spaces : string := ""
var key : string (1)
var newword, guess, word : string
var tdurr, count, wcount : int := 0
var lx : int := 400
var ly := 70

open : file, "text.txt", get
assert file > 0

%Procedures
proc background %Drawing background
    drawfillbox (0, 50, 700, 0, 118) %Draw grass
    drawfillbox (0, 50, 700, 400, 53) %Draw sky
    drawfillbox (20, 50, 40, 300, 115) %Draw straight up and down pole
    drawfillbox (20, 300, 140, 320, 115) %Draw across pole
    drawfillbox (120, 300, 125, 280, 115) %Draw rope.
end background

proc Person
    drawoval (122, 260, 20, 20, 16)
    drawline (122, 240, 122, 180, 16)
    drawline (122, 240, 105, 190, 16)
    drawline (122, 240, 140, 190, 16)
    drawline (122, 180, 140, 150, 16)
    drawline (122, 180, 102, 150, 16)
end Person

proc head %Drawing head
    drawoval (122, 260, 20, 20, 16)
end head

proc Torso %Drawing torso
    drawline (122, 240, 122, 180, 16)
end Torso

proc Larm %Drawing Left Arm
    drawline (122, 240, 105, 190, 16)
end Larm

proc Rarm %Drawing Right Arm
    drawline (122, 240, 140, 190, 16)
end Rarm

proc Lleg %Drawing Left Leg
    drawline (122, 180, 140, 150, 16)
end Lleg

proc Rleg %Drawing right Leg
    drawline (122, 180, 102, 150, 16)
end Rleg

proc MainGame

    loop
   
    spaces := ""
wcount := 0
count := 0
lx := 400
ly := 70

        randint (randy, 1, 30)

        for b : 1 .. randy

            get : file, word
        end for
        put word

        for c : 1 .. length (word)
            spaces := spaces + "_ "
        end for

        background

        locatexy (500, 90)
        colorback (53)
        put "Letters Guessed"

        loop
            locatexy (320, 380)
            colorback (53)
            color (white)
            put spaces

            colorback (53)
            color (white)
            locatexy (8, 400)
            View.Set ("cursor")
            put "Guess a letter"
            get guess

            lx := lx + 10 %Adding letters guess
            if lx > maxx - 20 then
                ly := 70
                lx := 400
            end if

            locatexy (lx, ly) %Locating and putting your guesses
            colorback (53)
            put guess

            loop
                if index (word, guess) < 0 then
                    pos := index (word, guess)
                    spaces := spaces (1 .. pos * 2 - 2) + guess + spaces (pos * 2 - 2)
                    exit
                else
                    locatexy (200, 200)
                    put "Guess Again"
                    exit
                end if
            end loop

            count := 0

            for a : 1 .. length (word)
                if word (a) = guess then
                    spaces := spaces (1 .. a * 2 - 2) + guess + spaces (a * 2 .. *)
                    count += 1
                    wcount := wcount + 1
                    locatexy (320, 380)
                    put spaces
                end if
            end for

            if count = 0 then %If you guessed a letter wrong
                locatexy (200, 200)
                put "That letter is not there"
                tdurr := tdurr + 1
                delay (1000)
            end if
            if tdurr = 1 then %If you guess one letter wrong
                head
            elsif tdurr = 2 then %If you guess two letter wrong
                Torso
            elsif tdurr = 3 then %If you guess three letter wrong
                Larm
            elsif tdurr = 4 then %If you guess four letter wrong
                Rarm
            elsif tdurr = 5 then %If you guess five letter wrong
                Lleg
            elsif tdurr = 6 then %If you guess six letter wrong
                Rleg
                delay (1000)
                cls
                background
                locatexy (260, 200)
                put "You Lost, the word was ", word %When you lose
                locatexy (250, 180)
                put "Would you like to play again? (y/n)"
                getch (key)
                if key = "y" or key = "Y" then %if you want to play again
                    MainGame
                else %If you don't want to play again
                    exit
                end if
            end if
        end loop

        if wcount = length (word) then %When you win
            delay (1000)
            background
            locatexy (260, 200)
            put "CONGRATULATIONS"
            delay (1000)
            cls
            background
            put "Would you like to play again? (y/n)" %Giving the option to play again
            getch (key)
            if key = "y" or key = "Y" then %If you want to play again
                MainGame
            else %If you don't want to play again
                exit
            end if
            exit
        end if
    end loop
end MainGame

background         %Drawing backround
Person

colorback (53)
locatexy (260, 200)
put "Welcome to Ryan's hangman!"
locatexy (270, 180)
put "a) Instructions"
locatexy (270, 160)
put "b) Lets just play"
getch (key)

if key = "a" or key = "A" then
    cls
    background         %Drawing backround
    put "1) The computer will randomly select a word."
    put "2) Guess letters until you win or lose."
    put "3) Press any key to start."

    getch (key)

    delay (1000)
    MainGame

else
    cls
    background         %Drawing backround
    MainGame
end if
Sponsor
Sponsor
Sponsor
sponsor
hossack




PostPosted: Thu Mar 26, 2009 7:20 am   Post subject: RE:Hangman Game

I got this to work. but I can only replay it once. Any reason?

Turing:
View.Set ("nocursor")
var stream, pos, offset, randy, file : int
var spaces : string := ""
var key : string (1)
var newword, guess, word : string
var tdurr, count, wcount : int := 0
var lx : int := 400
var ly := 70

open : file, "text.txt", get
assert file > 0

%Procedures
proc background %Drawing background
    drawfillbox (0, 50, 700, 0, 118) %Draw grass
    drawfillbox (0, 50, 700, 400, 53) %Draw sky
    drawfillbox (20, 50, 40, 300, 115) %Draw straight up and down pole
    drawfillbox (20, 300, 140, 320, 115) %Draw across pole
    drawfillbox (120, 300, 125, 280, 115) %Draw rope.
end background

proc Person
    drawoval (122, 260, 20, 20, 16)
    drawline (122, 240, 122, 180, 16)
    drawline (122, 240, 105, 190, 16)
    drawline (122, 240, 140, 190, 16)
    drawline (122, 180, 140, 150, 16)
    drawline (122, 180, 102, 150, 16)
end Person

proc head %Drawing head
    drawoval (122, 260, 20, 20, 16)
end head

proc Torso %Drawing torso
    drawline (122, 240, 122, 180, 16)
end Torso

proc Larm %Drawing Left Arm
    drawline (122, 240, 105, 190, 16)
end Larm

proc Rarm %Drawing Right Arm
    drawline (122, 240, 140, 190, 16)
end Rarm

proc Lleg %Drawing Left Leg
    drawline (122, 180, 140, 150, 16)
end Lleg

proc Rleg %Drawing right Leg
    drawline (122, 180, 102, 150, 16)
end Rleg

proc MainGame

    loop

        spaces := ""
        wcount := 0
        count := 0
        lx := 400
        ly := 70
        tdurr := 0

        randint (randy, 1, 30)

        for b : 1 .. randy

            get : file, word
        end for
        put word

        for c : 1 .. length (word)
            spaces := spaces + "_ "
        end for

        background

        locatexy (500, 90)
        colorback (53)
        put "Letters Guessed"

        loop
            locatexy (320, 380)
            colorback (53)
            color (white)
            put spaces

            colorback (53)
            color (white)
            locatexy (8, 400)
            View.Set ("cursor")
            put "Guess a letter"
            get guess

            lx := lx + 10 %Adding letters guess
            if lx > maxx - 20 then
                ly := 70
                lx := 400
            end if

            locatexy (lx, ly) %Locating and putting your guesses
            colorback (53)
            put guess

            loop
                if index (word, guess) < 0 then
                    pos := index (word, guess)
                    spaces := spaces (1 .. pos * 2 - 2) + guess + spaces (pos * 2 - 2)
                    exit
                else
                    locatexy (200, 200)
                    put "Guess Again"
                    exit
                end if
            end loop

            count := 0

            for a : 1 .. length (word)
                if word (a) = guess then
                    spaces := spaces (1 .. a * 2 - 2) + guess + spaces (a * 2 .. *)
                    count += 1
                    wcount := wcount + 1
                    locatexy (320, 380)
                    put spaces
                end if
            end for

            if wcount = length (word) then %When you win
                delay (1000)
                background
                locatexy (260, 200)
                put "CONGRATULATIONS"
                delay (1000)
                cls
                background
                put "Would you like to play again? (y/n)" %Giving the option to play again
                getch (key)
                if key = "y" or key = "Y" then %If you want to play again
                    MainGame
                else %If you don't want to play again
                    exit
                end if
                exit
            end if

            if count = 0 then %If you guessed a letter wrong
                locatexy (200, 200)
                put "That letter is not there"
                tdurr := tdurr + 1
                delay (1000)
            end if
            if tdurr = 1 then %If you guess one letter wrong
                head
            elsif tdurr = 2 then %If you guess two letter wrong
                Torso
            elsif tdurr = 3 then %If you guess three letter wrong
                Larm
            elsif tdurr = 4 then %If you guess four letter wrong
                Rarm
            elsif tdurr = 5 then %If you guess five letter wrong
                Lleg
            elsif tdurr = 6 then %If you guess six letter wrong
                Rleg
                delay (1000)
                cls
                background
                locatexy (260, 200)
                put "You Lost, the word was ", word %When you lose
                locatexy (250, 180)
                put "Would you like to play again? (y/n)"
                getch (key)
                if key = "y" or key = "Y" then %if you want to play again
                    MainGame
                else %If you don't want to play again
                    exit
                end if
            end if
        end loop
    end loop
end MainGame

background         %Drawing backround
Person

colorback (53)
locatexy (260, 200)
put "Welcome to Ryan's hangman!"
locatexy (270, 180)
put "a) Instructions"
locatexy (270, 160)
put "b) Lets just play"
getch (key)

if key = "a" or key = "A" then
    cls
    background         %Drawing backround
    put "1) The computer will randomly select a word."
    put "2) Guess letters until you win or lose."
    put "3) Press any key to start."

    getch (key)

    delay (1000)
    MainGame

else
    cls
    background         %Drawing backround
    MainGame
end if

DemonWasp




PostPosted: Thu Mar 26, 2009 7:46 am   Post subject: RE:Hangman Game

Calling MainGame from MainGame to do repetition is a recipe for disaster. Instead, what you should do is have a loop around your game code; if the player wants to continue, you allow the loop to do its thing, if not then you can just exit the loop.

Alternately, you could have your game in a procedure called MainGame, but then your "Continue? (Y/N)" question should be asked in a main loop that knows to exit if the player is done playing.
hossack




PostPosted: Thu Mar 26, 2009 7:53 am   Post subject: RE:Hangman Game

It works when I lose, but not when I win. I can only replay it once if I win off the start, but if I lose and then win and try to replay it doesn't work
hossack




PostPosted: Thu Mar 26, 2009 8:44 am   Post subject: RE:Hangman Game

Also it doesn't replay if you win and then replay and then lose and try to replay again
hossack




PostPosted: Thu Mar 26, 2009 9:16 am   Post subject: RE:Hangman Game

K so forget all that. I keep getting an error saying Attempt to read psat eof. How can I fix that?
hossack




PostPosted: Thu Mar 26, 2009 9:42 am   Post subject: RE:Hangman Game

forget that too. Everything is working, its just when I hit N to exit it doesn't exit

Turing:
View.Set ("nocursor")
var stream, pos, offset, randy, file : int
var spaces : string := ""
var key : string (1)
var newword, guess, word : string
var tdurr, count, wcount : int := 0
var lx : int := 400
var ly := 70



%Procedures
proc background %Drawing background
    drawfillbox (0, 50, 700, 0, 118) %Draw grass
    drawfillbox (0, 50, 700, 400, 53) %Draw sky
    drawfillbox (20, 50, 40, 300, 115) %Draw straight up and down pole
    drawfillbox (20, 300, 140, 320, 115) %Draw across pole
    drawfillbox (120, 300, 125, 280, 115) %Draw rope.
end background

proc Person
    drawoval (122, 260, 20, 20, 16)
    drawline (122, 240, 122, 180, 16)
    drawline (122, 240, 105, 190, 16)
    drawline (122, 240, 140, 190, 16)
    drawline (122, 180, 140, 150, 16)
    drawline (122, 180, 102, 150, 16)
end Person

proc head %Drawing head
    drawoval (122, 260, 20, 20, 16)
end head

proc Torso %Drawing torso
    drawline (122, 240, 122, 180, 16)
end Torso

proc Larm %Drawing Left Arm
    drawline (122, 240, 105, 190, 16)
end Larm

proc Rarm %Drawing Right Arm
    drawline (122, 240, 140, 190, 16)
end Rarm

proc Lleg %Drawing Left Leg
    drawline (122, 180, 140, 150, 16)
end Lleg

proc Rleg %Drawing right Leg
    drawline (122, 180, 102, 150, 16)
end Rleg

proc MainGame

    loop
 
        open : file, "text.txt", get
        assert file > 0

        spaces := ""
        wcount := 0
        count := 0
        lx := 400
        ly := 70
        tdurr := 0

        randint (randy, 1, 30)

        for b : 1 .. randy

            get : file, word
        end for
        put word

        for c : 1 .. length (word)
            spaces := spaces + "_ "
        end for

        background

        locatexy (500, 90)
        colorback (53)
        put "Letters Guessed"

        loop
            locatexy (320, 380)
            colorback (53)
            color (white)
            put spaces

            colorback (53)
            color (white)
            locatexy (8, 400)
            View.Set ("cursor")
            put "Guess a letter"
            get guess

            lx := lx + 10 %Adding letters guess
            if lx > maxx - 20 then
                ly := 70
                lx := 400
            end if

            locatexy (lx, ly) %Locating and putting your guesses
            colorback (53)
            put guess

            loop
                if index (word, guess) < 0 then
                    pos := index (word, guess)
                    spaces := spaces (1 .. pos * 2 - 2) + guess + spaces (pos * 2 - 2)
                    exit
                else
                    locatexy (200, 200)
                    put "Guess Again"
                    exit
                end if
            end loop

            count := 0

            for a : 1 .. length (word)
                if word (a) = guess then
                    spaces := spaces (1 .. a * 2 - 2) + guess + spaces (a * 2 .. *)
                    count += 1
                    wcount := wcount + 1
                    locatexy (320, 380)
                    put spaces
                end if
            end for

            if wcount = length (word) then %When you win
                delay (1000)
                background
                locatexy (260, 200)
                put "CONGRATULATIONS"
                delay (1000)
                cls
                background
                put "Would you like to play again? (y/n)" %Giving the option to play again
                getch (key)
                if key = "y" or key = "Y" then %if you want to play again
                    MainGame
                elsif key = "n" or key = "N" then %If you don't want to play again
                    exit
                end if
                exit
            end if

            if count = 0 then %If you guessed a letter wrong
                locatexy (200, 200)
                put "That letter is not there"
                tdurr := tdurr + 1
                delay (1000)
            end if
            if tdurr = 1 then %If you guess one letter wrong
                head
            elsif tdurr = 2 then %If you guess two letter wrong
                Torso
            elsif tdurr = 3 then %If you guess three letter wrong
                Larm
            elsif tdurr = 4 then %If you guess four letter wrong
                Rarm
            elsif tdurr = 5 then %If you guess five letter wrong
                Lleg
            elsif tdurr = 6 then %If you guess six letter wrong
                Rleg
                delay (1000)
                cls
                background
                locatexy (260, 200)
                put "You Lost, the word was ", word %When you lose
                locatexy (250, 180)
                put "Would you like to play again? (y/n)"
                getch (key)
                if key = "y" or key = "Y" then %if you want to play again
                    MainGame
                elsif key = "n" or key = "N" then %If you don't want to play again
                    exit
                end if
            end if
        end loop
    end loop
end MainGame


loop
    background     %Drawing backround
    Person

    colorback (53)
    locatexy (260, 200)
    put "Welcome to Ryan's hangman!"
    locatexy (270, 180)
    put "a) Instructions"
    locatexy (270, 160)
    put "b) Lets just play"
    getch (key)

    if key = "a" or key = "A" then
        cls
        background     %Drawing backround
        put "1) The computer will randomly select a word."
        put "2) Guess letters until you win or lose."
        put "3) Press any key to start."

        getch (key)

        delay (1000)
        MainGame
    elsif key = "b" or key = "B" then
        cls
        background     %Drawing backround
        MainGame
    end if
end loop

close (file)

DemonWasp




PostPosted: Thu Mar 26, 2009 9:53 am   Post subject: RE:Hangman Game

This one you get to fix on your own, but I'll point you in the right direction:

1. What does it mean when it says that you're trying to read past eof (eof means "end of file")?
2. When do you open a file? What do you read from it?
3. Why doesn't that always work, particularly if you replay?
Sponsor
Sponsor
Sponsor
sponsor
hossack




PostPosted: Thu Mar 26, 2009 9:55 am   Post subject: RE:Hangman Game

Why ask me questions?
1. Fixed That
2. Thats good
3. Im stuck and have no idea
DemonWasp




PostPosted: Thu Mar 26, 2009 10:23 am   Post subject: RE:Hangman Game

I'm not asking you questions, I'm providing the series of questions you're supposed to ask yourself to figure out the problem on your own. It's a learning exercise to get you to the point where you can figure out what questions to ask yourself to solve the problem on your own.

Think more carefully about what happens in your program. Here's an outline:

1. Opens a file for reading.
2. Read a random number of lines from the file.
3. Play the game with the last line read.
4. Replay? If yes, go to 2.

Think about how many lines are in the file and how many lines are consumed in step #2. Do you see the problem yet?
Tallguy




PostPosted: Thu Mar 26, 2009 11:05 am   Post subject: RE:Hangman Game

zip ur files so we can have the .txt file
hossack




PostPosted: Thu Mar 26, 2009 12:36 pm   Post subject: Re: Hangman Game

Heres the zip


Hangman.zip
 Description:
Hangman Zip

Download
 Filename:  Hangman.zip
 Filesize:  1.83 KB
 Downloaded:  98 Time(s)

hossack




PostPosted: Fri Mar 27, 2009 7:14 am   Post subject: RE:Hangman Game

anyone know how I could fix this?

edit: I tried using the return thing instead of using MainGame to replay it, but it started it from the very start of the game.
DemonWasp




PostPosted: Fri Mar 27, 2009 8:05 am   Post subject: RE:Hangman Game

I've already given you how to find the error, but since you don't seem to be interested in figuring it out yourself, I'll supply the answer:

1. You have x lines in the file. It doesn't particularly matter what the value of x is.
2. You keep reading lines from the file, heedless of the fact that you will eventually hit the end of file. You have no code path for dealing with this eventuality.
3. When your program gets to the end of the file, it does exactly what you would expect and says "attempt to read past end of file!"

To solve this, it's probably easiest to just read all of the possible strings into an array of strings at the beginning of the program, then choose a random string from the array every time you want to play the game.
hossack




PostPosted: Fri Mar 27, 2009 8:13 am   Post subject: RE:Hangman Game

My teacher hasn't taught us Array's and I had to learn proc on my own. Ill try to find out how to use Array's by reading the help thing in turing. Thanks
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 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: