-
Notifications
You must be signed in to change notification settings - Fork 5
/
numguess.pas
57 lines (48 loc) · 1.47 KB
/
numguess.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
program NumGuess;
var num, limit, tries, max_tries, guess : Integer;
name, again : String;
begin
Randomize;
WriteLn('Welcome to NumGuess Pascal version!');
WriteLn;
Write('Enter your name: ');
ReadLn(name);
if name = '' then name := 'Player';
WriteLn;
Write('Welcome ', name, ', enter upper limit: ');
ReadLn(limit);
if limit < 10 then limit := 10;
max_tries := trunc(ln(limit) / ln(2)) + 1;
repeat
num := Random(limit) + 1;
WriteLn;
WriteLn('Guess my number between 1 and ', limit, '!');
WriteLn;
tries := 0;
repeat
Write('Guess: ');
ReadLn(guess);
if (guess < 1) or (guess > limit) then begin
WriteLn('Out of range.');
end else begin
Inc(tries);
if not(num = guess) then begin
if num < guess then WriteLn('Too high!') else WriteLn('Too low!');
end;
end;
until guess = num;
WriteLn;
Write('Well done ', name, ', you guessed my number from ', tries, ' ');
if tries = 1 then WriteLn('try!') else WriteLn('tries!');
if tries = 1 then WriteLn('You''re one lucky bastard!')
else if tries < max_tries then WriteLn('You are the master of this game!')
else if tries = max_tries then WriteLn('You are a machine!')
else if tries <= max_tries * 1.1 then WriteLn('Very good result!')
else if tries <= limit then WriteLn('Try harder, you can do better!')
else WriteLn('I find your lack of skill disturbing!');
Write('Play again [y/N]? ');
ReadLn(again);
until not(Upcase(again) = 'Y');
WriteLn;
WriteLn('Okay, bye.');
end.