## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

Pascal Timer Posted by Liony on 20 Mar 2010 at 3:53 PM
I'm sorry to disturb you...
There's this little game I'm developing and I'm stuck.

So the game is about guessing a random number and I want to put a timer to see how long did the user to guess the number.

Here's the code for the timer:

```Procedure Timer ;
Begin
time := 0 ;
While a <> b do
Begin
Writeln ('Seconds: ',time,'') ;
Delay (1000) ;
Inc (time) ;
ClrScr ;```

"a" is the input number and "b" is the randomized number.
The problem is that the program keeps repeating the delay of 1 second and it doesn't let me input a number.

I thought on executing the main program and the procedure at the same time but I guess there is another way of doing it.

Here's the code for the rest of the game:

```Program Guess_the_number ;
Uses crt ;
var a, b, time : Integer ;
Begin
ClrScr ;
Timer ;
b := 1 ;
b := random (100) ;
Writeln ('Write a number between 1 and 100:') ;
Repeat
If a < b
then Writeln ('Too small...') ;
If a > b
then Writeln ('Too big...') ;
Until a = b ;
If a = b
then Writeln ('You did it! The correct number was ',b,'!') ;
Writeln ('You took ',time,' seconds to guess it.') ;
End ;```

You guys have any ideas?
Re: Pascal Timer Posted by _Atex_ on 20 Mar 2010 at 11:38 PM
```program guess_the_number;

var a,b,tries:byte;
time:longint;

begin
randomize;
tries:=0;
a:=succ(random(100));
time:=meml[0:\$46c]; { <-- Get time from system counter, it gets updated every 55ms }
repeat
write(#13#10'Enter a number between 1 and 100 (0 to Exit) : ');readln(b);
inc(tries);
if b=0 then begin writeln(#13#10'Exiting...');halt;end;
if a<b then writeln('Too big...') else
if a>b then writeln('Too small...');
until a=b;
time:=trunc((meml[0:\$46c]-time)/18.2); { <-- calculate elapsed time }
writeln(#13#10#13#10'You took ',time,' seconds and ',tries,' tries to guess it.');readln;
end.```

Re: Pascal Timer Posted by Liony on 21 Mar 2010 at 11:04 AM
Works like charm on Turbo Pascal, although I use Free Pascal more frequently, and there the code for the system counter and the elapsed time doesn't work.

Anyway thanks for the code for Turbo.
Re: Pascal Timer Posted by _Atex_ on 21 Mar 2010 at 5:20 PM
```program guess_the_number;
uses windows;
var a,b,tries:byte;
time:dword;

begin
randomize;
tries:=0;
a:=succ(random(100));
time:=gettickcount; { <-- Get time from system counter, it gets updated every millisecond }
repeat
write(#13#10'Enter a number between 1 and 100 (0 to Exit) : ');readln(b);
inc(tries);
if b=0 then begin writeln(#13#10'Exiting...');halt;end;
if a<b then writeln('Too big...') else
if a>b then writeln('Too small...');
until a=b;
time:=trunc((gettickcount-time)/1000); { <-- calculate elapsed time }
writeln(#13#10#13#10'You took ',time,' seconds and ',tries,' tries to guess it.');readln;
end.```

Re: Pascal Timer Posted by Liony on 22 Mar 2010 at 2:58 PM
Worked exactly like I expected to.
Anyway, the code I put was just a part of the original game.
The original has 3 difficulty modes (easy, medium and hard) and has the option of playing by tries, by time or both.
Grace to your resolution, I managed to do it all.

Looks like the game will keep entertained some of my classmates (if not all)

From now on, I'll do my best to make better games.

Thanks, mate.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic