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

I'm sorry but I don't see the logic error here. Did you actually cut-and-paste this code in or did you eyeball-to-fingers type it in?

(BTW. Please use [[b][/b]code[b][/b]=Delphi] ... [[B][/B]/[B][/B]code[B][/B]] tags.)

A couple of observations:

1. ComputerWord should be filled with uppercase letters, but you are using a lowercase 'a' in your button click method. It shouldn't ever match. (Hence my first question. That and ShowMessage doesn't terminate the game either...)

2. I hope you didn't have to repeat a click method for each letter of the alphabet. You can get away with just one by using the button's caption.

procedure THangmanForm.AlphaButtonClick( Sender: TObject );
  var
    letter: char;
    count:  integer;
  begin
  with Sender as TButton do
    begin
    enabled := false;
    letter  := caption
    end;
  
  for count := 1 to ...
  end;

A good rule of thumb is that if you notice yourself doing something almost exactly the same more than once or twice, that's a good candidate for a single, parameterized procedure. In this case, you can just create the above procedure once, and attach every one of your alphabet buttons' OnClick event to it.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.