Computer Science Canada Touble with Arrays and If's... making hangman |
Author: | helpmeagain [ Sat May 31, 2008 3:55 pm ] |
Post subject: | Touble with Arrays and If's... making hangman |
Hi I need help on my program, I'm making hangman and i can't figure out how to track if the letter is not part of the word... Heres the code: the bolded stuff is the part i need help on var file_contents : flexible array 1 .. 0 of string var stream : int var geuss : string var xx, font : int font := Font.New ("Arial:14") 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, 6) 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, black) end for var finish, wrong : int finish := 0 wrong := 0 xx := 20 loop Text.Locate (1, 1) put "geuss a letter" get geuss for x : 1 .. length (file_contents (word)) if geuss = letter (x) then finish := finish + 1 put "ayaya" locatexy (50, 50) Draw.Text (letter (x), location (x), 105, font, green) end if if finish = (upper (letter)) then put "You have won!You have won!" exit when finish = (upper (letter)) end if end for for x : 1 .. length (file_contents (word)) if geuss ~= letter (x)then xx := xx + 20 Draw.Text (geuss, 300 + xx, 50, font, red) wrong := wrong + 1 end if end for exit when finish = (upper (letter)) end loop the bolded part is my statement used to detect wether it is correct. If its not correct its supposed to put the incorrect letter on the screen so you know you've already geussed it. The problem is if the word is apple when you geuss a a is equal to a but not to p p l or e and so it puts on the screen that you geussed it wrong and that its right, but i want to take the geuss wrong out if its right. |
Author: | TheGuardian001 [ Sat May 31, 2008 4:21 pm ] | ||
Post subject: | Re: Touble with Arrays and If's... making hangman | ||
the easiest way to check if a letter is in a string is using the index command. index returns the position of a letter in a string. if the character can't be found in that string, it returns 0.
hope that helps you out. |
Author: | syntax_error [ Sat May 31, 2008 4:21 pm ] |
Post subject: | RE:Touble with Arrays and If\'s... making hangman |
I forget in Turing if they take string as an array of char or as an object, but you have to simple check for 1 char not the whole word. So, break your word up into an array of char and just make a condition for one match to happen and then the whole statement becomes true? |