forked from DarthJDG/NumGuess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
num_oop.pas
139 lines (114 loc) · 2.87 KB
/
num_oop.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
program NumGuessPascalOOP;
type NumGuess = object
num, limit, guess, tries, max_tries : Integer;
name : String;
procedure Init;
procedure InputName;
procedure InputLimit;
procedure GenerateNumber;
procedure InputGuess;
function GetName : String;
function GetTries : Integer;
function GetLimit : Integer;
function IsOutOfRange : Boolean;
function IsCorrect : Boolean;
function IsTooHigh : Boolean;
function CustomMessage : String;
end;
var number : NumGuess;
again : String;
{ NumGuess implementation }
procedure NumGuess.Init;
begin
Randomize;
end;
procedure NumGuess.InputName;
begin
ReadLn(name);
if name = '' then name := 'Player';
end;
procedure NumGuess.InputLimit;
begin
ReadLn(limit);
if limit < 10 then limit := 10;
max_tries := trunc(ln(limit) / ln(2)) + 1;
end;
procedure NumGuess.GenerateNumber;
begin
num := Random(limit) + 1;
tries := 0;
end;
procedure NumGuess.InputGuess;
begin
ReadLn(guess);
if not(IsOutOfRange) then Inc(tries);
end;
function NumGuess.GetName : String;
begin
GetName := name;
end;
function NumGuess.GetTries : Integer;
begin
GetTries := tries;
end;
function NumGuess.GetLimit : Integer;
begin
GetLimit := limit;
end;
function NumGuess.IsOutOfRange : Boolean;
begin
IsOutOfRange := (guess < 1) or (guess > limit);
end;
function NumGuess.IsCorrect : Boolean;
begin
IsCorrect := (num = guess);
end;
function NumGuess.IsTooHigh : Boolean;
begin
IsTooHigh := num < guess;
end;
function NumGuess.CustomMessage : String;
begin
if tries = 1 then CustomMessage := 'You''re one lucky bastard!'
else if tries < max_tries then CustomMessage := 'You are the master of this game!'
else if tries = max_tries then CustomMessage := 'You are a machine!'
else if tries <= max_tries * 1.1 then CustomMessage := 'Very good result!'
else if tries > limit then CustomMessage := 'I find your lack of skill disturbing!'
else CustomMessage := 'Try harder, you can do better!';
end;
{ Main program }
begin
number.Init;
WriteLn('Welcome to NumGuess Pascal version!');
WriteLn;
Write('Enter your name: ');
number.InputName;
WriteLn;
Write('Welcome ', number.GetName, ', enter upper limit: ');
number.InputLimit;
repeat
number.GenerateNumber;
WriteLn;
WriteLn('Guess my number between 1 and ', number.GetLimit, '!');
WriteLn;
repeat
Write('Guess: ');
number.InputGuess;
if number.IsOutOfRange then begin
WriteLn('Out of range.');
end else begin
if not(number.IsCorrect) then begin
if number.IsTooHigh then WriteLn('Too high!') else WriteLn('Too low!');
end;
end;
until number.IsCorrect;
WriteLn;
Write('Well done ', number.GetName, ', you guessed my number from ', number.GetTries, ' ');
if number.GetTries = 1 then WriteLn('try!') else WriteLn('tries!');
WriteLn(number.CustomMessage);
Write('Play again [y/N]? ');
ReadLn(again);
until not(Upcase(again) = 'Y');
WriteLn;
WriteLn('Okay, bye.');
end.