program GiocoDelCento;
uses crt;
type tstr=string[200];
numero=object
n,f,dn:word;
t:byte;
constructor inizio(a,b:word);
procedure gioca;
function test(x:byte):boolean;
procedure domanda;
procedure genera;
procedure stampa;
destructor distruggi;
end;
var g:numero;
procedure pausa(fr:tstr; x,y:word); forward;
procedure linea(t:char; c:word); forward;
procedure frase(fr:tstr; fine,col:word); forward;
constructor numero.inizio(a,b:word);
begin
f:=a;
dn:=b;
n:=0;
end;
procedure numero.gioca;
var giocate:byte;
begin
writeln('Gioco del 100');
giocate:=0;
while test(giocate) do
begin
t:=giocate and 1;
case t of
0: begin
domanda;
end;
1: begin
genera;
stampa
end;
else
begin
textcolor(red);
pausa('Bug! ', 1, wherey+2);
halt(0);
end;
end; { fine case of }
inc(giocate);
linea('-', cyan);
end;
end;
procedure numero.genera;
var d,x:word;
begin
d:=f-n;
if d<=dn then x:=d
else x:=1+random(dn);
inc(n, x);
end;
procedure numero.stampa;
begin
frase('Il computer gioca ', 30, green);
textcolor(yellow);
write(n);
end;
procedure numero.domanda;
var domn,diff:word;
ch:boolean;
begin
repeat
frase('Numero ', 30, green);
textcolor(yellow);
readln(domn);
GotoXY(60, WhereY-1);
diff:=domn-n;
ch:=(diff>0) and (diff<=dn);
if not ch then
begin
textcolor(red);
writeln;
writeln('Numero non accettato! ');
writeln('I limiti ammissibili sono tra ',n+1,' e ',n+dn);
end;
until ch;
n:=domn;
end;
function numero.test(x:byte):boolean;
var ch:boolean;
begin
ch:=n<100;
if x>50 then
begin
pausa('Superato il limite massimo di giocate! ',1, wherey+2);
ch:=false;
end;
test:=ch;
end;
destructor numero.distruggi;
begin
{ distruttore }
end;
procedure linea(t:char; c:word);
var i:byte;
begin
writeln;
textcolor(c);
for i:=1 to 80 do write(t);
end;
procedure pausa(fr:tstr; x,y:word);
begin
GotoXY(x, y);
textcolor(7);
write(fr);
while not keypressed do; writeln(readkey);
end;
procedure frase(fr:tstr; fine,col:word);
var i:word;
begin
textcolor(col);
write(fr);
for i:=length(fr) to fine do write('.');
write(' ');
end;
begin
clrscr;
randomize;
g.inizio(100, 10);
g.gioca;
pausa('Fine del programma. ', 1, wherey+2);
g.distruggi;
end.