Hi, I've just completed my intro to programming class and my last assignment is to write a program for a hangman game. I have started and I think somewhere along the line I've gotten confused, can anyone give me some guidance. We did not do randomize in class but the question requires the program to choose a word at random--I googled it but it still confuses me. I really just want a simple program for now---if anyone could help i'll greatly appreciate it.
I've attached my code thus far.
Don't use Word to write your programs. Use a plain-text editor, or the Pascal IDE. If you are using Turbo Pascal, it has a very nice IDE.
Next, make your program compile before you post asking questions.
To use random numbers, somewhere at the beginning of your program say:
randomize; and to get a random number say
num := random( N ); where N is the range. The random number returned is in 0..(N-1).
Don't forget to terminate your program with
end. You should work on your indentation. It is causing you a little confusion. A good rule of thumb is that statements that belong to another statement should be indented under that statement. For example:
if x = y then celebrate else complain_something_fierce; while quuxxing( x ) do begin x := frobnicate( knob[ x ] ); writeln( neutrino_value( x ) ) end;
Here you can instantly see that lines 2 and 3 belong to line 1, and that lines 6..9 belong to line 5.
You really shouldn't play with input like you are. I suggest you make yourself a function to get the word to guess out of a file:
function get_word_to_guess( filename: string ): string; var f: file; begin assign( f, filename ); reset( f ); if IOResult = 0 then begin readln( f, result ); close( f ) end end;
Personally, I would make the file have a lot of words, one word per line, and have the function choose one at random.
The stuff about a wrong letter and writing the guessed-so-far letters belong inside the loop.
The stuff about telling the user if he won or not belongs outside the loop.
Hope this helps.