uses crt,graph,dos;
const
sdht=3;{so den mo hien tai}
sdkt=6;{so den ket thuc}
tocdo=100; {You can edit this statement small value for higher speed, large value for lowwer speed}
var ds:string;
diem:byte;t,n:integer;
p:char;thoat,used,thua:boolean;
bo_nho:array[1..9]of boolean;
{_}
procedure batdohoa;
var
Gd,Gm: Integer;
begin
Gd := Detect; InitGraph(Gd, Gm,' ');
if GraphResult <> grOk then Halt(1);
end;
{_}
procedure tatdohoa;
begin
closegraph;
end;
{_}
procedure re_setting;
var a:byte;
begin
for a:=1 to 9 do
bo_nho[a]:=false;
diem:=0;thua:=false;
settextstyle(0,0,1);
end;
{_}
procedure statusbar(a,b,c:boolean);
begin
settextstyle(0,0,1);
if a=true then setcolor(white)
else setcolor(darkgray);
outtextxy(10,getmaxy-20,'Esc: Quit');
if b=true then setcolor(white)
else setcolor(darkgray);
outtextxy(150,getmaxy-20,'Space: Replay');
if c=true then setcolor(white)
else setcolor(darkgray);
outtextxy(300,getmaxy-20,'Enter: Pause');
line(1,getmaxy-25,getmaxx,getmaxy-25);
end;
{_}
procedure rand;
var r:1..sdht;
begin
randomize;
statusbar(true,false,false);
setfillstyle(1,lightgray);
bar(95,75,500,125);
setcolor(white);
rectangle(100,80,495,120);
Outtextxy(100,100,' Nhan phim Enter de tiep tuc');
settextstyle(0,0,2);
outtextxy(150,200,'Luat Choi');
settextstyle(0,0,1);
outtextxy(120,240,'Co mot bang gom 9 o tuong ung voi cac den');
outtextxy(120,260,'Nhiem vu: Dung cac phim so tu 1 den 9 nhu');
outtextxy(120,270,'cac cong tac de tat cac den, khi co 6 den');
outtextxy(120,280,'duoc bat thi tro choi ket thuc');
outtextxy(120,300,'An phim Esc bat ki luc nao de thoat');
outtextxy(120,310,'An phim Space de choi lai');
outtextxy(120,320,'An phim Enter de tam dung tro choi');
repeat
re_setting;
for r:=1 to sdht do
bo_nho[random(
+1]:=true;
until keypressed;
if readkey =#27 then halt(1);
cleardevice;
end;
{_}
procedure draw;
const canh=50;
var e:byte;f,g:word;
begin
for e:= 1 to 9 do
begin
g:=4-((e-1) div 3 + 1);
f:=(e-1) mod 3 + 1;
if bo_nho[e]=true then
begin
setfillstyle(1,e); bar(100+f*canh,100+g*canh,100+canh+f*canh,100+canh+g*canh);
setfillstyle(9,e); bar(102+f*canh,102+g*canh,98+canh+f*canh,98+canh+g*canh);
setfillstyle(11,e); bar(104+f*canh,104+g*canh,96+canh+f*canh,96+canh+g*canh);
setfillstyle(10,e); bar(106+f*canh,106+g*canh,94+canh+f*canh,94+canh+g*canh);
setfillstyle(0,0); bar(108+f*canh,108+g*canh,92+canh+f*canh,92+canh+g*canh);
outtextxy(f*canh+97+canh div 2,g*canh+97+canh div 2,#4);
end
else begin
setfillstyle(0,0);
bar(100+f*canh,100+g*canh,100+canh+f*canh,100+canh+g*canh);
setfillstyle(10,e);
bar(100+f*canh,100+g*canh,100+canh+f*canh,100+canh+g*canh);
end;
end;
end;
{_}
function dem_den:byte;
var t,k:byte;
begin
k:=0;
for t:=1 to 9 do
if bo_nho[t]=true then
k:=k+1;
dem_den:=k;
end;
{_}
function time:boolean;
var a,b,c,d:word;
begin
time:=false;
gettime(a,b,c,d); c:=c*100+d;
if (c mod tocdo in[(tocdo div 3)..(tocdo div 3*2)]) and (used=false) then
begin time:=true; used := true;end;
if c mod tocdo in[(tocdo div 3*2)..tocdo] then used := false;
end;
{_}
procedure BaoDiem;
begin
settextstyle(0,0,1);
outtextxy(50,150,'Ban da ket thuc luot choi voi so diem dat duoc la:');
settextstyle(0,0,5);
outtextxy(100,250,ds);
statusbar(true,true,false);
repeat
p:=readkey;
if p =#27 then thoat:=true;
if p =' ' then thua:=true;
until p in[#27,' '];
end;
{----Main Program----}
begin
batdohoa;
rand;
repeat
repeat
draw;statusbar(true,true,true);
if keypressed then
begin
p:=readkey;
if p=#13 then
begin
cleardevice;
setfillstyle(1,lightgray);
bar(95,75,500,125);
setcolor(white);
rectangle(100,80,495,120);
Outtextxy(100,100,' An phim bat ki de tiep tuc');
statusbar(false,false,false);
readkey;
cleardevice;
end;
if p=' ' then thua:=true;
if p in['1'..'9'] then
begin
val(p,n,t);
if bo_nho[n]=true then diem:=diem+1;
bo_nho[n]:=not bo_nho[n];
end;
if p=#27 then
begin thoat:=true;
cleardevice;baodiem; end;
end;
if dem_den>=sdkt then begin
cleardevice;
setfillstyle(1,lightgray);
settextstyle(0,0,4);
outtextxy(50,100,'You are lost');
baodiem; end;
repeat until keypressed or time;
if not keypressed then bo_nho[random(
+1]:=true;
str(diem,ds); draw;
until (thua=true)or(thoat=true);
re_setting; cleardevice;
until thoat=true;
tatdohoa;
end.