Hello, I am having a bit of trouble with a hangman game that I am currently writing. The program runs without any error (syntax) however when I press a letter (buttons on the form), it simply states Congratulations, you were saved from the Gallows!' with only having had 1 go on each occassion. I was sure I was doing it correctly but obviously there is some sort of logic error in the code. I have attached below one of the keys code (which are all the same code) and the form create code to see if any can pick up why it is not working correctly.
Any assistance appreciated.
procedure THangmanForm.FormCreate(Sender: TObject);
begin
Usedletters := '';
Hiddenlbl.caption:=''; {Sets the caption inside the label to blank}
Memofile.hide; {Hides the Memo File from the user}
Randomize;
Hiddenlbl.Visible:=true;
MemoRow:= MemoFile.Lines.Count; {Gives the Memo Row the number of rows that are stored in the Memo File}
MemoRow:= Random(MemoRow); {Selects a random row out of the Memo File}
ComputerWord:= UpperCase(MemoFile.Lines[MemoRow]); {Defines the random word with a set variable and makes it upper case}
NumberOfLetters:= Length(ComputerWord); {This sets the number of letters in the hidden word to the number of letters that the memobox says it has}
SetLength(UsersWord,NumberOfLetters);
NumberFound:=0;
NumberOfErrors:=0;
for count := 1 to NumberOfLetters Do
Begin
UsersWord[count] :='-';
HiddenLbl.Caption := HiddenLbl.Caption + '-';
End;
end;
procedure THangmanForm.AbtnClick(Sender: TObject);
begin
abtn.enabled := false;
key := 'a';
found := false;
for count := 1 to NumberofLetters do
if ComputerWord[count] = Key then
Begin
found := true;
UsersWord[Count]:= 'a';
NumberFound:= NumberFound + 1;
End;
if found
then
begin
Hiddenlbl.Caption:='';
for Count := 1 to length(UsersWord) Do
hiddenlbl.caption:=hiddenlbl.caption + UsersWord[count] + '';
end
else
begin
NumberofErrors:=NumberofErrors+1;
Hangmanimage.picture.LoadFromFile('Hungman' + IntToStr(NumberofErrors) + '.bmp');
end;
if NumberFound = NumberofLetters
then Showmessage('Congratulations, you were saved from the Gallows!')
Else if NumberofErrors = 11
then ShowMessage('Nice try but you did not correctly guess the word');
end;
Mel