Index/ hangman please help
Author |
Message |
helpmeagain
|
Posted: Sun Jun 01, 2008 5:02 pm Post subject: Index/ hangman please help |
|
|
I need help with making hangman, ive got the programm detecting the letter the user enters and if its its part of the word it shows that. However if there is 2 of the same letter in that word, only the first of that letter is detected. For example the word is apple and the user geusses p only the first p will be detected and displayed. the bolded part (down in the code) is where the program detects what letter is inputted.
var file_contents : flexible array 1 .. 0 of string
var stream : int
var geuss : string
var xx, font, c, a, coulor, x, y, button, alphabett : int
font := Font.New ("Arial:14")
Draw.Fill (1, 1, black, black)
open : stream, "words.txt", get
loop
exit when eof (stream)
new file_contents, upper (file_contents) + 1
get : stream, file_contents (upper (file_contents))
end loop
var word : int
randomize
randint (word, 1, upper (file_contents))
var letter : array 1 .. length (file_contents (word)) of string
var xbound : int
xbound := 20
var location : array 1 .. length (file_contents (word)) of int
for xy : 1 .. length (file_contents (word))
letter (xy) := (file_contents (word) (xy .. xy))
xbound := xbound + 20
location (xy) := 80 + xbound
Draw.FillBox (location (xy), 100, location (xy) + 10, 102, green)
end for
c := 3
a := 0
var aletter : int
var a2 : array 1 .. 26 of string
aletter := 97
for i : 1 .. 26
a2 (i) := chr (aletter)
aletter := aletter + 1
end for
var aletter2 : int
var a3 : array 1 .. 26 of string
aletter2 := 65
for i2 : 1 .. 26
a3 (i2) := chr (aletter2)
aletter2 := aletter2 + 1
end for
xx := 10
coulor := 32
for alpha : 1 .. 26
xx := xx + 20
coulor := coulor + 1
Draw.Text (a3 (alpha), 1 + xx, 50, font, coulor)
Draw.Box (xx, 48, xx + 20, 75, 21)
end for
var wordd : string := (file_contents (word))
var wrong, finish, x2, x3 : int
wrong := 0
finish := 0
xx := 10
loop
x2 := 10
x3 := 30
Mouse.Where (x, y, button)
if button = 1 then
for b : 1 .. 26
x2 := x2 + 20
x3 := x3 + 20
if x > x2 and x < x3 and y > 48 and y < 75 and a3 (b) ~= a2 (b) and button = 1 then
geuss := a2 (b)
if index (wordd, geuss) > 0 then
finish := finish + 1
Draw.Text (geuss, location (index (wordd, geuss)), 105, font, green)
end if
if index (wordd, geuss) = 0 then %if the letter is not in the word
xx := xx + 20
Draw.Text (geuss, 100 + xx, 25, font, red)
wrong := wrong + 1
end if
a3 (b) := a2 (b)
Draw.FillBox (x2, 48, x3, 75, black)
end if
var ch : string (1)
if hasch then
getch (ch)
if ch = a2 (b) and a3 (b) ~= a2 (b) then
geuss := a2 (b)
if index (wordd, geuss) > 0 then
finish := finish + 1
locatexy (50, 50)
Draw.Text (geuss, location (index (wordd, geuss)), 105, font, green)
end if
if index (wordd, geuss) = 0 then %if the letter is not in the word
xx := xx + 20
Draw.Text (geuss, 100 + xx, 25, font, red)
wrong := wrong + 1
end if
a3 (b) := a2 (b)
Draw.FillBox (x2, 48, x3, 75, black)
end if
end if
end for
end if
if finish = (upper (letter)) then
Draw.Text ("You have been saved", 300, 200, font, white)
exit when finish = (upper (letter))
end if
if wrong = 10 then
Draw.Text ("You have been hung", 300, 200, font, white)
exit when wrong = 10
end if
exit when wrong = 10
exit when finish = (upper (letter))
end loop
Draw.Text (wordd, 100, 300, font, green) |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheGuardian001
|
Posted: Sun Jun 01, 2008 5:22 pm Post subject: Re: Index/ hangman please help |
|
|
The only way i can think of to find multiple occurrences in a word is to make a temporary string to hold the rest of the word, then index that again, and keep doing so until index returns 0.
for example if word = "apple"
code: |
if index(word,"p") > 0 then %returns 2
your code
loop
tempstr := word(index(word,"p")+1 .. length(word)) %sets tempstr to the word from the first "p" onwards (ple)
|
and so on. |
|
|
|
|
|
helpmeagain
|
Posted: Sun Jun 01, 2008 5:37 pm Post subject: Re: Index/ hangman please help |
|
|
hmmm that didnt work anyone else got ideas? Im thinking of getting it to check after the first position it found the letter. Ex.
if the word is apple... the first time it checked index got 2 (because the first p is at location2) so i want it to loop and check from location 3 onwards... what would be the code to check the word from location 3 instead of the start |
|
|
|
|
|
TheGuardian001
|
Posted: Sun Jun 01, 2008 6:53 pm Post subject: Re: Index/ hangman please help |
|
|
i found one reason it wont work (but i know how to fix it)
the temp string isnt the length of the original string, so instead of
tempstr := wordd(index(wordd, geuss) + 1 .. length( wordd))
it should be
tempstr := repeat(" ",index(wordd,geuss)) + wordd(index(wordd, geuss) + 1 .. length( wordd))
other than that, just make sure you're indexing tempstr instead of wordd inside the loop |
|
|
|
|
|
gitoxa
|
Posted: Sun Jun 01, 2008 11:56 pm Post subject: Re: Index/ hangman please help |
|
|
Don't index. Use string manipulation. |
|
|
|
|
|
Insectoid
|
Posted: Mon Jun 02, 2008 7:49 am Post subject: RE:Index/ hangman please help |
|
|
I suppose when you have identified a letter in the word, you remove it.
If the word is 'hello', and the player guesses 'l', then word := word (1, 2) + word (5)
This would result in 'heo'. |
|
|
|
|
|
Saad
|
Posted: Mon Jun 02, 2008 1:45 pm Post subject: Re: Index/ hangman please help |
|
|
gitoxa @ Sun Jun 01, 2008 11:56 pm wrote:
index is part of string manipulation. And to tell you the truth I can't see a problem with using index for this. |
|
|
|
|
|
|
|