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