Posted: 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
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
endif endfor
if count =0then%If you guessed a letter wrong locatexy(200, 200) put"That letter is not there"
tdurr := tdurr + 1 delay(1000) endif if tdurr =1then%If you guess one letter wrong
head
elsif tdurr =2then%If you guess two letter wrong
Torso
elsif tdurr =3then%If you guess three letter wrong
Larm
elsif tdurr =4then%If you guess four letter wrong
Rarm
elsif tdurr =5then%If you guess five letter wrong
Lleg
elsif tdurr =6then%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 endif endif endloop
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 endif exit endif endloop 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."
Posted: 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
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
endif endfor
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 endif exit endif
if count =0then%If you guessed a letter wrong locatexy(200, 200) put"That letter is not there"
tdurr := tdurr + 1 delay(1000) endif if tdurr =1then%If you guess one letter wrong
head
elsif tdurr =2then%If you guess two letter wrong
Torso
elsif tdurr =3then%If you guess three letter wrong
Larm
elsif tdurr =4then%If you guess four letter wrong
Rarm
elsif tdurr =5then%If you guess five letter wrong
Lleg
elsif tdurr =6then%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 endif endif endloop endloop 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."
Posted: 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
Posted: 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
Posted: 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
Posted: 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
Posted: 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
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
endif endfor
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 endif exit endif
if count =0then%If you guessed a letter wrong locatexy(200, 200) put"That letter is not there"
tdurr := tdurr + 1 delay(1000) endif if tdurr =1then%If you guess one letter wrong
head
elsif tdurr =2then%If you guess two letter wrong
Torso
elsif tdurr =3then%If you guess three letter wrong
Larm
elsif tdurr =4then%If you guess four letter wrong
Rarm
elsif tdurr =5then%If you guess five letter wrong
Lleg
elsif tdurr =6then%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 endif endif endloop endloop 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."
Posted: 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
hossack
Posted: 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
Posted: 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
Posted: Thu Mar 26, 2009 11:05 am Post subject: RE:Hangman Game
zip ur files so we can have the .txt file
hossack
Posted: Thu Mar 26, 2009 12:36 pm Post subject: Re: Hangman Game
Posted: 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
Posted: 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
Posted: 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