Need help with hangman game
Author |
Message |
Danyn
|
Posted: Mon Nov 11, 2013 3:53 pm Post subject: Need help with hangman game |
|
|
What is it you are trying to achieve?
I want to add a letter tracker to my program and I want to disable repeated letters from affecting the game.
What is the problem you are having?
I have no clue how to do so.
Describe what you have tried to solve this problem
I've tried using variables and put statements but nothing has worked.
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing: |
var word, category, hidden : string
var life : boolean
var guess : string (1)
var point1, point2, bodypart, colourIntro : int
var font2 := Font.New ("georgia:40")
bodypart := 0
procedure death
%Draws the death stage
drawline (50, 40, 250, 40, 255)
drawline (200, 40, 200, 270, 255)
drawline (200, 270, 125, 270, 255)
drawline (125, 270, 125, 240, 255)
end death
%Asks for word
locate (15, 33)
put "What's the word?"
locate (16, 35)
get word : *
word := Str.Upper (word )
cls
%Asks for category
locate (15, 33)
put "What's the category?"
locate (16, 35)
get category : *
cls
%converts the word to hidden
hidden := ""
for i : 1 .. length (word )
if word (i ) >= "A" and word (i ) <= "Z" then
hidden + = "-"
else
hidden + = word (i )
end if
end for
%Puts hidden
locate (15, 40)
put hidden
%Draws the death hanger
death
%Asks user to guess a letter and remove cursor
View.Set ("nocursor")
locate (13, 40)
put "Guess a letter:"
loop
%Sets the user's life to false
life := false
locate (15, 40)
Input.Flush
%Gets the guess
guess := Str.Upper (getchar)
%Checks to see if the guess is in the word
for i : 1 .. length (hidden )
if guess = word (i ) then
%If it is then puts the letter there
hidden := hidden (1 .. i - 1) + guess + hidden (i + 1 .. *)
locate (15, 40)
put hidden
%Changes life value to true so user doesn't lose a life
life := true
end if
end for
%If user doesn't guess a letter in the word changes life to false
if life = false then
%Adds one body part to the count
bodypart + = 1
end if
%Draws body parts depending on body part count
if bodypart = 1 then
%Draws the head
Draw.Oval (125, 215, 25, 25, 255)
elsif bodypart = 2 then
%draws body
drawline (125, 190, 125, 110, 255)
elsif bodypart = 3 then
%draws arms
drawline (125, 185, 100, 120, 255) %left arm
elsif bodypart = 4 then
drawline (125, 185, 150, 120, 255) %right arm
elsif bodypart = 5 then
%draws legs
drawline (125, 110, 100, 60, 255) %left leg
elsif bodypart = 6 then
drawline (125, 110, 150, 60, 255) %right leg
end if
%How to win/lose
exit when bodypart = 6 or hidden = word
end loop
if bodypart = 6 then
colourIntro := 121
for decreasing intro : 121 .. 44
Font.Draw ("YOU LOSE", 210, 170, font2, colourIntro )
delay (25)
colourIntro - = 1
end for
else
colourIntro := 121
for decreasing intro : 121 .. 44
Font.Draw ("YOU WIN", 210, 170, font2, colourIntro )
delay (25)
colourIntro - = 1
end for
end if
|
Please specify what version of Turing you are using
4.1.1 |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Dreadnought
|
Posted: Mon Nov 11, 2013 9:22 pm Post subject: Re: Need help with hangman game |
|
|
Basically, you want an array to keep track of what letters have already been tried. If you don't know what an array is (or want to learn more about Turing) see The Turing Walkthrough.
You could also use a string as an array of characters if you are more comfortable with strings than arrays.
The general idea is check if array tells you letter has been tried. if yes do something, if not do something else and modify the array to keep track of the current letter.
Hope this helps! |
|
|
|
|
 |
Raknarg

|
Posted: Mon Nov 11, 2013 9:24 pm Post subject: RE:Need help with hangman game |
|
|
Easiest way, is make a string that starts out empty and when the player guesses a letter, add that to the string. However if that letter is already in the string, tell them they can't use that letter.
a string called something like usedLetters and the index function (you can look that up in the documentation) is all you would need for this. |
|
|
|
|
 |
|
|