Code:
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<dos.h>
int dichchuyen=20;
int dem;
class Hinh_vuong
{
protected:
static int x,y;
int mau,dd; // dd = do dai canh hinh vuong
public:
void dichuyen(); //di chuyen hinh vuong
Hinh_vuong(int x1=441,int y1=261,int mau1=4,int dd1=18)
{
x=x1;y=y1;mau=mau1;dd=dd1;
}
void vehinhvuong()
{ if(dem%2)
{
setcolor(mau);
}
else setcolor(1);
rectangle(x,y,x+dd,y+dd);
}
void xoa() //hien lai hinh vuong voi mau trung voi mau ban co
{
setcolor(11);
rectangle(x,y,x+dd,y+dd);
}
void len()
{
if (y-dichchuyen>=100) // neu sau khi dich chuyen toa do y van lon hon 80 thi dich chuyen len
{
xoa();
y-=dichchuyen;
vehinhvuong();
}
}
void xuong()
{
if (y+dichchuyen<=440) // sau khi dich chuyen ma toa do y van nho hon 420 thi dich chuyen
{
xoa();
y+=dichchuyen;
vehinhvuong();
}
}
void trai()
{
if (x-dichchuyen>=280)
{
xoa();
x-=dichchuyen;
vehinhvuong();
}
}
void phai()
{
if (x+dichchuyen<=600)
{
xoa();
x+=dichchuyen;
vehinhvuong();
}
}
};//hinhvuong
int Hinh_vuong::x=0;
int Hinh_vuong::y=0;
void Hinh_vuong::dichuyen()
{
char ch;
do
{
ch=getch();
if (ch==0)
{
ch=getch();
switch(ch)
{
case 72: Hinh_vuong::len();break;
case 75: Hinh_vuong::trai();break;
case 77: Hinh_vuong::phai();break;
case 80: Hinh_vuong::xuong();break;
}
}
} while (ch!=13);
}
int A[16][17];
class Matran:public Hinh_vuong
{
private:
int a,b;
int i,j;
public:
// gan cho tat ca cac phan tu cua ma tran cac gia tri ban dau la 0
void gan()
{
for( i=0;i<16;i++)
for( j=0;j<17;j++)
{
A[i][j]=0;
}
};
//kiem tra xem phan tu thu A[a][b] mang gia tri gi
int kiemtra()
{
a=(x-281)/20;
b=(y-101)/20;
return A[a][b];
}
void ganmatran()
{
a=(x-281)/20;
b=(y-101)/20;
if(dem%2)
{
A[a][b]=1;
}else A[a][b]=2;
}
};//ma tran
class hinhtron:private Matran
{
private:
int r;
public:
hinhtron(int r1=8)
{r=r1;}
void vehinhtron()
{
setcolor(4);
circle(x+9,y+9,r);
ganmatran();
sound(500);
delay(50);
nosound();
dem++;
}
};
class Hinhchu_X:private Matran
{
public:
void vehinhchu_X()
{
setcolor(1);
// rectangler(x+2,y+2,x+17,y+17); // /
// rectangler(x+2,y+17,x+17,y+2); // \
rectangle(x+2,y+2,x+17,y+17);
ganmatran();
sound(1000);
delay(50);
nosound();
dem++;
}
} ;
void hien()
{
setfillstyle(2,14);
bar(0,0,639,70);
setfillstyle(3,11);
bar(0,70,200,479);
setcolor(3);
line(0,70,639,70);
line(0,72,639,72);
line(200,72,200,479);
line(202,72,202,479);
setcolor(7);
line(203,73,639,73);
line(203,73,203,479);
setcolor(14);
line(0,71,639,71);
line(201,72,201,479);
setcolor(12);
settextstyle(10,HORIZ_DIR,3);// dinh dang kieu chu
outtextxy(85,10," CO CARO GIUA 2 NGUOI" );
outtextxy(86,10," CO CARO GIUA 2 NGUOI" );
outtextxy(86,9," CO CARO GIUA 2 NGUOI" );
setcolor(2);
outtextxy(87,8," CO CARO GIUA 2 NGUOI" );
settextstyle(7,HORIZ_DIR,1);
setcolor(9);
outtextxy(15,100,"Di chuyen :");
//ve hinh ->
line(125,115,140,115);
line(135,110,140,115);
line(135,120,140,115);
//ve hinh mui ten quay len
line(150,105,150,120);
line(145,114,150,105);
line(155,114,150,105);
//ve hinh <-
line(160,115,175,115);
line(160,115,170,120);
line(160,115,170,110);
//ve hinh mui ten quay xuong
line(185,105,185,120);
line(185,120,180,112);
line(185,120,190,112);
setcolor(2);
outtextxy(15,120,"Chon : Enter ");
setcolor(13);
outtextxy(15,140,"Thoat : Esc ");
settextstyle(7,HORIZ_DIR,1);
outtextxy(15,420,"Truong Van Tam" );
outtextxy(15,440,"Do Duc Thach");
}
void vebanco()
{
int x1=280,y1=100,y2=440;
setcolor(14);
rectangle(279,99,601,441);
setcolor(4);
rectangle(278,98,602,442);
setcolor(5);
line(279,97,603,97);
line(603,97,603,441);
setfillstyle(1,11);
bar(280,100,600,440);
//ve cac duong ke doc
while(x1<=600)
{
setcolor(14);
line(x1,y1,x1,y2);
x1=x1+20;
}
x1=280;int x2=600;
// ve cac duong ke ngang
while(y1<=440)
{
setcolor(14);
line(x1,y1,x2,y1);
y1=y1+20;
}
y1=100;
}
int Menu()
{
int vt=1,vt1=0;//vi tri
char ch;
setcolor(7);
line(16,266,184,266);
line(184,266,184,352);
setcolor(2);
rectangle(15,267,183,353); //vien ngoai
rectangle(17,269,181,351); //vien trong
setcolor(14);
rectangle(16,268,182,352); //vien giua
setcolor(7);
line(18,315,180,315);
setcolor(10);
line(18,314,180,314);
setcolor(2);
line(18,313,180,313);
setfillstyle(1,11);//kieu to mau
bar(18,270,180,312);
setfillstyle(1,15);
bar(18,316,180,350);
setcolor(12);
settextstyle(7,HORIZ_DIR,2);
outtextxy(25,265,"ban co muon");
outtextxy(25,285,"choi tiep Ko ?");
do{
if(vt!=vt1)
{
vt1=vt;
setcolor(15);
rectangle(39,325,81,346);
rectangle(99,325,146,345);
setfillstyle(1,11);
bar(40,326,80,345);
bar(100,326,145,344);
setcolor(14);
settextstyle(8,HORIZ_DIR,2);
outtextxy(50,320,"Co");
outtextxy(110,320,"Ko");
setcolor(4);
if(vt==1)
{
rectangle(39,325,81,346);
setfillstyle(1,10);
bar(40,326,80,345);
outtextxy(50,320,"Co");
}
else {
rectangle(99,325,146,345);
setfillstyle(1,10);
bar(100,326,145,344);
outtextxy(110,320,"Ko");
}
} //if
ch=getch();
if(ch==0);
switch(ch)
{
case 75:if(vt>1)vt--;break;
case 77:if(vt<2)vt++;break;
}
}while(ch!=13);return vt;
} //menu
void main()
{
Hinh_vuong a;
hinhtron b;
Hinhchu_X c;
Matran f;
char ch;
int p,luot;
int gd=0,gm=0;
initgraph(&gd,&gm,"C:\\laptrinh\\TC30\\BGI");
setbkcolor(15);
int z=1;
while(z==1)
{
dem=1;
luot=1;
f.gan();
Hinh_vuong();
vebanco();//goi ham ve ban co
hien();
a.vehinhvuong();
do{
if(luot%2)
{
a.dichuyen();
p=f.kiemtra();
if(p!=1&&p!=2)
{
b.vehinhtron();
luot++;
}
}
else{
a.dichuyen();
p=f.kiemtra();
if(p!=1&&p!=2)
{
c.vehinhchu_X();
luot++;
}
}
ch=getch();
}while(ch!=27);
z=Menu();
}//while
closegraph();
}//main()