Please Help! Simple Hangman
Author |
Message |
jadelinanne
|
Posted: Sun Jan 13, 2013 12:37 pm Post subject: Please Help! Simple Hangman |
|
|
What is it you are trying to achieve?
<I'm trying to create a very simple hangman game <>>
What is the problem you are having?
I can't get the loop to check if a letter if wrong or right. I have 10 words in an array and I use a randint to assign a word to each random number generated. But if I have the word "apple" I can only get it to check for the first letter. If the word if correct, I need to draw it some lines. Also, I'm not sure how many guesses to give the user. Do I make a variable for each guess? (guess,guess1,guess2,guess3) etc?Please please help ;S
Describe what you have tried to solve this problem
<Lots of things but nothing worked :l>
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
[syntax="turing"]
<for example:
>
var letters : array 1 .. 26 of string := init ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
var words : array 1 .. 10 of string := init ("Dog","Cat",Apple","Bear","Zoo","Coconut","Pineapple","Cake","Book","Smile")
put "Do you want to Play?"
put "Enter Yes or No"
get option
if option = "Yes" or option = "YES" or option = "yes" then
put "Guess a letters"
get guess : *
put "Guess a letter"
get guess1 : *
put "Guess a letter"
get guess2
put "Guess again"
get guess3
put "Guess"
get guess4
put "GUESS AGAIN"
get guess5
drawline (800, 100, 800, 280, black)
drawline (700, 280, 800, 280, black)
drawline (700, 260, 700, 280, black)
correct := 0
loop
if generate = 1 then
word := words (1)
if guess = letters (4) or guess = "d" and guess1 = letters (15) or guess1 = "o" and guess2 = letters (6) or guess2 = "g" then
correct := correct + 1
elsif generate = 2 then
word := words (2)
if guess = letters (3) or guess= "c" and guess1 = letters (1) or guess1= "a" and guess2= letters (20) or "t" then
correct := correct + 1
Please specify what version of Turing you are using
<4.11> |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
DemonWasp
|
Posted: Sun Jan 13, 2013 1:13 pm Post subject: RE:Please Help! Simple Hangman |
|
|
Let's start with one guess, then you can move on to repeated guesses later.
To make things easier, you should make all your words upper case or lower case (choose one).
You already know how to select the word the user is guessing. You mentioned using randint. Please put that block of code BEFORE any guesses (your program needs to know the word before it can tell if a guess was correct).
You already know how to get the guess from the user:
Turing: |
var guess : string(1)
put "Guess a letter"
get guess
|
Next, you need to figure out whether that guess is right or wrong. A guessed letter is correct if it is in the word and hasn't been guessed already. Try to write a small if-statement to determine whether guess is in word. Hint: http://compsci.ca/holtsoft/doc/index.html
If guess is correct, then you should add one to an integer variable called "correct". If the guess was wrong, add one to "incorrect". Once you know how many guesses your player got correct/incorrect, you know how many pieces of pacman to draw.
Post again here when you've figured that part out. |
|
|
|
|
 |
jadelinanne
|
Posted: Sun Jan 13, 2013 2:05 pm Post subject: RE:Please Help! Simple Hangman |
|
|
I've never used the index command before so i'm not really sure how to use it ;S
this is what I got so far
random := 0
randint (random, 1, 15)
cls
loop
put "Guess a letter"
get guess : *
if random = 1 then
word := words (1)
if guess = letters (3) then
correct := correct + 1
Font.Draw ("C", 10, 50, font, 100)
elsif guess = letters (1) then
correct := correct + 1
Font.Draw ("A", 30, 50, font, 100)
elsif guess = letters (20) then
correct := correct + 1
Font.Draw ("T", 50, 50, font, 100)
end if
end if
end loop
end if
and if its incorrect, is should be
elsif guess not= letters (3) then
incorrect := incorrect + 1
elsif guess not= letters (1) then
incorrect := incorrect + 1
elsif guess not= letters (20) then
incorrect := incorrect + 1
I still don't get the usage of index, what do I do my word has a double letter such as "apple"? |
|
|
|
|
 |
DemonWasp
|
Posted: Sun Jan 13, 2013 2:46 pm Post subject: RE:Please Help! Simple Hangman |
|
|
You're making this way more complicated than it needs to be. Let's simplify.
1) Open a new Turing window to work on this example in.
2) Copy the "words" array into the new window.
3) Copy the two lines of 'randint' code above into the new window.
You now have an array of words. Start by selecting one word, at random into a variable called word. I'll get you started:
Turing: |
var word : string := % fill in this part here. You will have to use the "words" array and the random number you generated earlier.
|
Then add:
Run this program several times. It should select different words each time. Post back here once you've done that. |
|
|
|
|
 |
jadelinanne
|
Posted: Sun Jan 13, 2013 4:00 pm Post subject: RE:Please Help! Simple Hangman |
|
|
But random is an integer, and the word variable is a string, I can't put both of them into one without an error |
|
|
|
|
 |
DemonWasp
|
Posted: Sun Jan 13, 2013 4:34 pm Post subject: RE:Please Help! Simple Hangman |
|
|
You're not putting both of them into one. You're choosing one word from the words array and putting it into the variable "word".
How do you get one word from the words array? You use "words(1)" or "words(2)" or "words(integer variable)".
How do you assign a value to word? You use something like:
word = "value"
word = other_string_variable
Try putting those two together. |
|
|
|
|
 |
jadelinanne
|
Posted: Sun Jan 13, 2013 4:48 pm Post subject: RE:Please Help! Simple Hangman |
|
|
Oh okay
var random : int
random := 0
randint (random, 1, 10)
var words : array 1 .. 10 of string := init ("Dog", "Cat", "Apple", "Bear", "Zoo", "Coconut", "Pineapple", "Cake", "Book", "Smile")
var word : string := words (random)
put word
yeah, now I get a different word each time I run it
whats the next step ? |
|
|
|
|
 |
DemonWasp
|
Posted: Mon Jan 14, 2013 1:52 pm Post subject: RE:Please Help! Simple Hangman |
|
|
The next step is to gather a guess from the user. You've already written the correct thing for that, so I'll just paste it here so we're clear:
Turing: |
put "Guess a letter"
get guess
|
While you're writing your program, it may make sense to have it show the correct word, so that you can tell whether your program is doing the right thing, so leave the put statement there for a while.
Next, you need to check whether the guessed letter appears in the randomly-chosen word. You need to look at each letter in the random word and see whether it is equal to the guessed letter. Hint: start with a for loop to go over every letter in the word. At each step, it should check whether the letter from the word is equal to the guessed letter. If it is, you should mark the guess "correct" (by setting a boolean variable, "correct", which starts false, to true). After you've checked every letter, if "correct" is true, then you should put "Correct", otherwise put "Incorrect".
Once you've written that, TEST that it works. Make a correct guess and it should say "Correct". Make the wrong guess, and it should say "Incorrect". |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
jadelinanne
|
Posted: Sat Jan 19, 2013 2:25 pm Post subject: RE:Please Help! Simple Hangman |
|
|
I tried to do that but it didn't work :/
I did a different way but it only works for the 1st word
when I try to do the same for the second word it messes itup ;S |
|
|
|
|
 |
Panphobia

|
Posted: Sat Jan 19, 2013 2:31 pm Post subject: RE:Please Help! Simple Hangman |
|
|
I can see where a problem might arise, you did nothing to stop using the same letter over and over again, so either remove a letter from the word or the alphabet, or alternatively use a boolean array to check which letters in the word have been chosen true and not chosen false |
|
|
|
|
 |
Tony

|
Posted: Sat Jan 19, 2013 4:53 pm Post subject: Re: RE:Please Help! Simple Hangman |
|
|
jadelinanne @ Sat Jan 19, 2013 2:25 pm wrote: I tried to do that but it didn't work :/
I did a different way but it only works for the 1st word
when I try to do the same for the second word it messes itup ;S
Notice how nobody but you actually knows what you have tried to do or in what ways your attempts did not work. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
|
|