Board index » delphi » Help...converting TP program to Delphi program (newbie)

Help...converting TP program to Delphi program (newbie)

    Hello...I've just started using Delphi 3.0 after using Turbo Pascal 7.0
for about a year...in other words, I'm a newbie at both.  I am currently
attempting to rewrite one of my TP games in Delphi. Anyway my question is
this:  In TP7.0 I was able to ask for a user's input, read it, and determine
if it was correct.  I gave them three guesses to do this or if they guessed
the correct answer the program moved on.  The problem however is that I am
unsure how to do the following bit of code in Delphi 3 since of course the
'ReadLn' of the user's input is invalid.  Can any kind programmer please
help me out on this one?  I'm using an Edit box to receive the user's
input...  I want to call GetSecretWord and then wait for the user to either
enter a correct guess or make 3 incorrect guesses at which point the For
loop would be incremented and the next call to GetSecretWord would be
executed.

{This is the snippet of code from my TP game that I'd like to translate to
Delphi}
  For NumLetters:= 4 to 8 do
    Begin
      GetSecretWord(NumLetters,TheWord);
      Guesses:= 0;
      Repeat
        ClrScr;
        Write('Your Guess: ');
        ReadLn(GuessWord);
        Guesses:= Guesses + 1;
      Until (GuessWord = TheWord) or (Guesses = 3);
   .
   .
   .

Thank you in advance for any help you can offer me,
David Carvalho

 

Re:Help...converting TP program to Delphi program (newbie)


Quote
David Carvalho wrote...

[snip]
I'm using an Edit box to receive the user's
|input...  I want to call GetSecretWord and then wait for the user to
either
|enter a correct guess or make 3 incorrect guesses at which point the
For
|loop would be incremented and the next call to GetSecretWord would be
|executed.
|
|{This is the snippet of code from my TP game that I'd like to
translate to
|Delphi}
|  For NumLetters:= 4 to 8 do
|    Begin
|      GetSecretWord(NumLetters,TheWord);
|      Guesses:= 0;
|      Repeat
|        ClrScr;
|        Write('Your Guess: ');
|        ReadLn(GuessWord);
|        Guesses:= Guesses + 1;
|      Until (GuessWord = TheWord) or (Guesses = 3);

Hi David,

In Delphi (Windows) , you can not use lengthy repeat..until loops that
wait for using input, since this would freeze the system, or make it
behave very weird.
Instead, events should be used. In your specific case, you can trap
the Keypress() event of the EditBox to check if the user has pressed
CR to indicate input completion.
Connect this event to your procedure (double-click on "OnKeyPress" in
the Object Inspector); then do something like the following:

1. Make Guesses private; initialize to 0;
2. Make TheWord private;
3. Call Get Secretword() in the FormCreate event

procedure Edit1.OnKeyPress(Sender: TObject; var Key: Char);
var
 Go_On : boolean;
begin
  if Key = #13 then begin
    Go_On := Edit1.Text = TheWord;
    if not Go_On then begin
      inc(Guesses);
      Go_On := Guesses = 3;
    end;
    if Go_On then
        < start something,
          show something,
          call something,
          Set a flag... >
  end;
end;

Hope this puts you on track!

Regards,
Dirk Claessens
______________________________________________
Home   : <dirk.claess...@village.uunet.be>
Office : <dirk.claessens...@belgium.agfa.com>
A man's life is what his thoughts make of it.
 (Marcus Aurelius)
______________________________________________

Re:Help...converting TP program to Delphi program (newbie)


Quote
Dirk Claessens wrote in message <6d9prk$54...@xenon.inbe.net>...

<SNIP>

Quote
>Hope this puts you on track!

>Regards,
>Dirk Claessens
>______________________________________________
>Home   : <dirk.claess...@village.uunet.be>
>Office : <dirk.claessens...@belgium.agfa.com>
>A man's life is what his thoughts make of it.
> (Marcus Aurelius)
>______________________________________________

Dirk,
    Thank you for the help...it is appreciated! :-)

David Carvalho
Carvalho1 (at) worldnet (dot) att (dot) net

Re:Help...converting TP program to Delphi program (newbie)


Quote
David Carvalho wrote:
>     Hello...I've just started using Delphi 3.0 after using Turbo Pascal 7.0
> for about a year...in other words, I'm a newbie at both.  I am currently
> attempting to rewrite one of my TP games in Delphi. Anyway my question is
> this:  In TP7.0 I was able to ask for a user's input, read it, and determine
> if it was correct.  I gave them three guesses to do this or if they guessed
> the correct answer the program moved on.  The problem however is that I am
> unsure how to do the following bit of code in Delphi 3 since of course the
> 'ReadLn' of the user's input is invalid.  Can any kind programmer please
> help me out on this one?  I'm using an Edit box to receive the user's
> input...  I want to call GetSecretWord and then wait for the user to either
> enter a correct guess or make 3 incorrect guesses at which point the For
> loop would be incremented and the next call to GetSecretWord would be
> executed.

> {This is the snippet of code from my TP game that I'd like to translate to
> Delphi}
>   For NumLetters:= 4 to 8 do
>     Begin
>       GetSecretWord(NumLetters,TheWord);
>       Guesses:= 0;
>       Repeat
>         ClrScr;
>         Write('Your Guess: ');
>         ReadLn(GuessWord);
>         Guesses:= Guesses + 1;
>       Until (GuessWord = TheWord) or (Guesses = 3);
>    .
>    .
>    .

> Thank you in advance for any help you can offer me,
> David Carvalho

David,

Lets examine what your snippet does and try to recreate that in a windows
environment. A 'game' consists of 5 turns where on each turn, a user gets
3 guesses to figure out the word. The length of the word on the first turn is
4 letters, second turn 5 letters, ..., fifth turn 8 letters.

We are going to need 3 components on our main form: an edit box for the user
to type his/her guess, a label to identify the edit box (this is purely for the
users
benefit), and a button to check if the guess is correct. We also need 3 global
variables,

     TheWord : String;
     NumLetters : Integer;
     Guesses : Integer;

For this example, I changed your GetSecretWord procedure to a function that
has one arguement, ALength, and returns a string.

     function GetSecretWord(ALength:Integer):String;
     begin
       ... {Code to generate a random word}
     end;

Now, your code generates a 4 letter word and initializes Guesses to 0 before the

user can do anything. We can do that in Delphi by using a form's FormCreate
method:

     procedure TForm1.FormCreate(Sender: TObject);
     begin
       Guesses := 0;
       NumLetters := 4;
       TheWord := GetSecretWord(WordLength);
     end;

Now we need some code to see if the user guessed right. So, we use the button's
Button1Click method:

     procedure TForm1.Button1Click(Sender: TObject);
     begin
       {First check to see if the user got the word right...}
       if (Edit1.Text = TheWord) then
         begin
           {The word is correct so
            if the number of letters is less then 8, generate a new word}
           if (NumLetters < 8) then
             begin
               {First, make the user feel good, then increment the number of
                letters, generate a new word, and reset Guesses to 0}
               ShowMessage('That Right!!!!  Let''s try a longer word');
               Inc(NumLetters);
               TheWord := GetSecretWord(NumLetters);
               Guesses := 0;
             end {End of NumLetters < 8}
           else
             {The guess was correct, but the NumLetters is not < 8,
              therefore game is over!}
             begin
               ShowMessage('That''s Right!!!! Game Over.');
             end;
         end
       else
         {User Guessed wrong}
         begin
           {First, increment number of guesses}
           Inc(Guesses);
           {Check value of guesses}
           Case Guesses of
             1  :  ShowMessage('WRONG!  Try again');
             2  :  ShowMessage('WRONG!  Try one more time.');
             3  :  begin
                     {If 3rd guess, and number of letters < 8,
                      display correct answer and go to new word}
                     if (NumLetters < 8) then
                       begin
                         ShowMessage('WRONG! The correct answer is '
                           + TheWord + '. Lets try another word.');
                         Inc(NumLetters);
                         TheWord := GetSecretWord(NumLetters);
                         Guesses := 0;
                       end
                     else
                       {If 3rd guess and number of letters is not < 8,
                        display correct answer and game is over.}
                       ShowMessage('WRONG! The correct answer is ' +
                         TheWord + '. Game Over.');
                   end;
           end;
         end;
     end;

Compile and have fun!

-Andy

Other Threads