Pascal Timer

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:

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

"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:

[code]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
Readln (a) ;
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.') ;
ReadKey ;
End ;[/code]

You guys have any ideas?

Comments

  • [code][color=Blue]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.[/color][/code]
  • 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.
  • [code][color=Blue]program guess_the_number;
    [red]uses windows;[/red]
    var a,b,tries:byte;
    time:[red]dword[/red];

    begin
    randomize;
    tries:=0;
    a:=succ(random(100));
    time:=[red]gettickcount;[/red] { <-- 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(([red]gettickcount[/red]-time)/[red]1000[/red]); { <-- calculate elapsed time }
    writeln(#13#10#13#10'You took ',time,' seconds and ',tries,' tries to guess it.');readln;
    end.[/color][/code]
  • 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.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion