•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 422,411 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,017 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 428 | Replies: 1
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,875
Reputation:
Rep Power: 11
Solved Threads: 193
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 [code=Delphi] ... [/code] 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.
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.
(BTW. Please use [code=Delphi] ... [/code] 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.
Delphi Syntax (Toggle Plain Text)
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;
Hope this helps.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access activation api blogger blogging blogs code code injection combo dani daniweb data debugging development dreamweaver dropdownlist epilepsy gdata google gpl griefers hackers html innovation javascript key linux microsoft module net news openbsd product programming reuse rss serial source tags vista web wysiwyg xml
- having problems with code not performing the full program (C)
- Please help me figure out whats wrong with my code (C++)
- Finally Almost Finished 2 Problems!! :( (C)
- Communication Problems (Java)
- Again please someone check my code (C++)
- code problems (C++)
- Problems with displaying total payroll and allowing user to quit by entering a neg. (C++)
- need help with code (C++)
- How do you write a code which compiles in c but not in c++? (C++)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: [DELPHI] how to set event procedure runtime?
- Next Thread: Stringgrid cell color



Linear Mode