I hope this message doesn't insult you but first you don't really need to separate each letter into another array.
Your code:
Rem get the length of word passed into a variable
letter = len(almost$)
dim letterpart$(letter)
For i = 1 to letter
letterpart$(i) = mid$(almost$,i)
next i
The above isn't needed because you have the string already in "almost$". You can look at the string itself to see if the letter guessed by the player is in "almost$"... I'll go over this later though.
This next part that creates the blank lines can be put into one function.
Your code:
Rem put in if statements to show word randomly picked
If letter = 3
threeSpace()
else
if letter = 4
fourSpace()
else
if letter = 5
fiveSpace()
else
endif
endif
endif
One function with simple if/then statements lower the amount of code because it's not repeated within separate functions.
` Draw the lines based on the # of characters in almost$
Lines(len(almost$))
function Lines(Size)
ink rgb(0,255,255),0
if Size=>1 then line 70, 400, 130, 400
if Size=>2 then line 140,400,200,400
if Size=>3 then line 210,400,270,400
if Size=>4 then line 280,400,340,400
if Size=>5 then line 350,400,410,400
endfunction
Putting an input outside of the while/endwhile and repeating that input before the endwhile isn't needed because you can simply put the input inside the while/endwhile.
Your code:
Rem input guess word and start loop
set text size normal
Text 0,450, "Press qq to quit"
INPUT "Guess ", guess$
while guess$ <> "qq"
Rem input guess word and start loop
set text size normal
Text 0,450, "Press qq to quit"
while guess$ <> "qq"
INPUT "Guess ", guess$
` Force guess$ to be lowercase
guess$=lower$(guess$)
In the above code notice the command under "input". When you search strings it's case sensitive... "A" and "a" are two different letters. The command "lower$(string$)" forces the user input to be lowercase... since all your words are lowercase they will always match letters the user types in (unless the user types more than one letter at a time).
Next is the fun part.
Your code:
Rem start nested for/next loop
for c = 1 to letter
if guess$ = letterpart$(c) and c = 1
text 90,377, guess$
yes = yes+ 1
else
if guess$ = letterpart$(c) and c = 2
text 160,377, guess$
yes = yes+ 1
else
if guess$ = letterpart$(c) and c = 3
text 230,377, guess$
yes = yes+ 1
else
if guess$ = letterpart$(c) and c = 4
text 300,377, guess$
yes = yes+ 1
else
if guess$ = letterpart$(c) and c = 5
text 370,377, guess$
yes = yes+ 1
else
endif
endif
endif
endif
endif
Rem call win function when variable equals the guessed word
if yes = letter
youwin()
else
endif
next c
Since the "letterpart$()" array is not there anymore you're probably asking how do you check the string for a letter?... basically how you did it to separate the letters into the array but a little differently. You want a for/next loop that starts at 1 and ends at "len(almost$)" (there's no need to make a variable to store the length of the string because of that command). To check the letter you use an if/then looking for "mid$(almost$,fornextvariable)". To get the routine to work right you need a switch. It's just like the light switches at your house... it's either on or it's off. You make the switch in code by defining a variable and make it equal zero (which means off) and if a certain thing happens the switch is turned on (something other than zero... usually 1 at first). After the routine you check to see if the switch is on or off (still a zero or a one) to determine what to do.
In your program you start the switch at zero which represents no letters found. When a letter is found you turn the switch on, increase "yes" for a right answer, and write the letter to the screen. After all the letters in the "almost$" have been looked at you check to see if the switch is still off (no letter has been found). If the switch is still at zero you increase the variable "error" and call the function "WrongGuess(error)"... when error=1 the head is drawn... when error=2 the body is drawn and so forth.
When you have an if/then like you have at the bottom of the above code snip... if you don't have anything "else" for it to do then take it out the "else". Also there's no need to check if all the letters have been guessed within the for/next loop. It's better to move it outside of the for/next loop.
The last thing that you had that's not a good idea to do is have regular code at the end. The regular code should end before the functions. Move "endofprogram: end" to before your first function.
Here's your complete code with changes:
startofprogram:
cls
error = 0
yes = 0
Rem create the 12 element word array
dim word$(12)
Rem load data into the array elements
word$(0) = "leg"
word$(1) = "eye"
word$(2) = "toe"
word$(3) = "arm"
word$(4) = "head"
word$(5) = "face"
word$(6) = "nose"
word$(7) = "hand"
word$(8) = "neck"
word$(9) = "calve"
word$(10) = "elbow"
word$(11) = "organ"
word$(12) = "brain"
Rem randomly body part from word array
randomize timer()
num = rnd(12)
Rem pass the randomly selected body part into a working variable
almost$ = word$(num)
Rem calling game title
gametitle()
Rem calling gallow function
gallows()
` Draw the blank lines based on the # of characters in almost$
Lines(len(almost$))
Rem input guess word and start loop
set text size normal
Text 0,450, "Press qq to quit"
while guess$ <> "qq"
INPUT "Guess ", guess$
` Force guess$ to be lowercase
guess$=lower$(guess$)
` Create a switch 0=Letter Not Found 1=Letter Found
Right=0
` This is for the first blank lines text spot
` I noticed that each text spot was 70 pixels away
` from the last. This makes it easier to print the
` text.
TextX=90
for d=1 to len(almost$)
if guess$=mid$(almost$,d)
` If the letter is found make Right=1
Right=1
` Increase right answers
inc yes
` Show the letter on the screen
text TextX,377,guess$
endif
` Increase TextX by 70 to go to the next line
inc TextX,70
next d
` If no letter has been found increase the errors
` and call the function to show the body
if Right=0
inc error
WrongGuess(error)
endif
Rem call win function when variable equals the guessed word
if yes = len(almost$)
youwin()
endif
endwhile
end
endofprogram:
end
Rem create functions that were called
Rem game title function
function gametitle()
set text size 36
set text font "comic sans ms"
ink rgb(0,255,0),0
text 100,50, "Welcome To Hang-man"
ink rgb(255,128,0),0
text 103,51, "Welcome To Hang-man"
endfunction
Rem entire gallow function
function gallows()
ink rgb(128,255,0),0
line 174,100,174,290
sleep 50
line 174,100,290,100
sleep 50
line 290,100,290,140
endfunction
Rem three word space function
function Lines(Size)
ink rgb(0,255,255),0
if Size=>1 then line 70, 400, 130, 400
if Size=>2 then line 140,400,200,400
if Size=>3 then line 210,400,270,400
if Size=>4 then line 280,400,340,400
if Size=>5 then line 350,400,410,400
endfunction
` Draw the body
function WrongGuess(BodyPart)
select BodyPart
` Head
case 1
circle 290,150,10
endcase
` Body
case 2
line 290,160,290,190
endcase
` Right Leg
case 3
line 290,190,300,220
endcase
` Left Leg
case 4
line 290,190,280,220
endcase
` Right Arm
case 5
line 290,160,300,190
endcase
` Left Arm
case 6
line 290,160,280,190
endcase
endselect
endfunction
Rem Win function
function youwin()
cls
for i =1 to 30
sleep 20
text rnd(450), rnd(450), "You Win!"
next i
endfunction
Rem Lose Functon
function youlose()
cls
for t=1 to 30
wait 50
text rnd(450), rnd(450), "You Lose the word was " + almost$
next t
endfunction
Don't be afraid to space out your code. Spacing is easier on the eyes and makes it easier to debug. Indenting code also helps. I indent with a single space to the right of any loop so I can see where the loop begins and ends. Some people use tabs but a space is enough for me.
One of the things I didn't put in (so you have something to do) is a way to check if a letter has been guessed already. If a person puts in the same letter (that's in the string) over and over again they'll eventually win because "yes" will eventually equal the length of the string. I'm right here if you need help.