User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: May 2008
Posts: 1
Reputation: mel1 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
mel1 mel1 is offline Offline
Newbie Poster

Problems with code

  #1  
May 28th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,875
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Problems with code

  #2  
May 28th, 2008
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.
  1. procedure THangmanForm.AlphaButtonClick( Sender: TObject );
  2. var
  3. letter: char;
  4. count: integer;
  5. begin
  6. with Sender as TButton do
  7. begin
  8. enabled := false;
  9. letter := caption
  10. end;
  11.  
  12. for count := 1 to ...
  13. 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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 9:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC