This project is based on game connect. It is single player game. The code is very simple. Please follow the instruction to run this game properly..
COMM.h should be place in include directory to make this code work. After copying just compile this and enjoy the game…
#include <iostream.h>
#include <graphics.h>
#include <stdlib.h>0
#include <conio.h>
#include <comm.h>
#include <dos.h>
#define DEL 15
int a[20][10],xcerc=1,ycerc=0,ray=20,player=1;
void init()
{
int graphdriver =DETECT, graphmode;
initgraph(&graphdriver, &graphmode, “..\\bgi”);
}
void intro()
{
cleardevice();
setcolor(2);
settextstyle(1,0,9);
outtextxy(75,125,”CONNECT 4″);
getch();
cleardevice();
}
void drawround(int x, int y, int ray, int col)
{
setcolor(col);
setfillstyle(1,col);
fillellipse((x-1)*50+45,(y-1)*50+90,ray,ray);
}
void drawrect()
{
setcolor(2);
setfillstyle(1,1);
rectangle(10,1,635,470);
floodfill(15,60,2);
}
void slide(int f,int m,int dir,int h)
{
if(dir==1)
for(int i=((f-1)*50+45);i<((m-1)*50+45);i+=5)
{
setcolor(1);
setfillstyle(1,1);
fillellipse(i,40,ray,ray);
setcolor(h);
setfillstyle(1,h);
fillellipse(i+5,40,ray,ray);
delay(DEL);
}
if(dir==2)
for(i=((f-1)*50+45);i>((m-1)*50+45);i-=5)
{
setcolor(1);
setfillstyle(1,1);
fillellipse(i,40,ray,ray);
setcolor(h);
setfillstyle(1,h);
fillellipse(i-5,40,ray,ray);
delay(DEL);
}
}
void animate()
{
for(int ycerc=0;ycerc<=6;ycerc++)
{
if(a[xcerc][ycerc+2]!=0)
{
if(player==1) a[xcerc][ycerc+1]=1;
if(player==2) a[xcerc][ycerc+1]=5;
return;
}
if(ycerc==0)
drawround(xcerc,ycerc,ray,1);
else
drawround(xcerc,ycerc,ray,0);
if(player==1) { drawround(xcerc,ycerc+1,ray,14); delay(35); }
if(player==2) { drawround(xcerc,ycerc+1,ray,2); delay(35); }
}
if(player==1) a[xcerc][ycerc+1]=1;
if(player==2) a[xcerc][ycerc+1]=5;
}
inline void aux1()
{
delay(2000);
cleardevice();
setcolor(14);
settextstyle(1,0,7);
outtextxy(30,150,”YELLOW Chip WINS!!!”);
getch();
exit(0);
}
inline void aux2()
{
delay(2000);
cleardevice();
setcolor(2);
settextstyle(1,0,7);
outtextxy(30,150,”GREEN Chip WINS!!!”);
getch();
exit(0);
}
inline void aux3()
{
cleardevice();
setcolor(9);
settextstyle(1,0,11);
outtextxy(200,125,”TIE!!!”);
getch();
exit(0);
}
void win()
{
int s=0;
for(int i=1;i<=12;i++)
for(int j=1;j<=8;j++)
{
s+=a[i][j];
if(a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j]==4) aux1();
if(a[i][j]+a[i+1][j]+a[i+2][j]+a[i+3][j]==20) aux2();
if(a[i][j]+a[i][j+1]+a[i][j+2]+a[i][j+3]==4) aux1();
if(a[i][j]+a[i][j+1]+a[i][j+2]+a[i][j+3]==20) aux2();
if(a[i][j]+a[i+1][j+1]+a[i+2][j+2]+a[i+3][j+3]==4) aux1();
if(a[i][j]+a[i+1][j+1]+a[i+2][j+2]+a[i+3][j+3]==20) aux2();
if(a[i][j]+a[i-1][j-1]+a[i-2][j-2]+a[i-3][j-3]==4) aux1();
if(a[i][j]+a[i-1][j-1]+a[i-2][j-2]+a[i-3][j-3]==20) aux2();
if(a[i][j]+a[i-1][j+1]+a[i-2][j+2]+a[i-3][j+3]==4) aux1();
if(a[i][j]+a[i-1][j+1]+a[i-2][j+2]+a[i-3][j+3]==20) aux2();
if(a[i][j]+a[i+1][j-1]+a[i+2][j-2]+a[i+3][j-3]==4) aux1();
if(a[i][j]+a[i+1][j-1]+a[i+2][j-2]+a[i+3][j-3]==20) aux2();
if(s==252) aux3();
}
}
void movecircle()
{
char t;
while(t!=ESC)
{
t=0;
t=getch();
if(t==RIGHT && xcerc!=12)
{
if(player==1) { slide(xcerc,xcerc+1,1,14); }
if(player==2) { slide(xcerc,xcerc+1,1,2); }
xcerc++;
}
if(t==LEFT && xcerc!=1)
{
if(player==1) { slide(xcerc,xcerc-1,2,14); }
if(player==2) { slide(xcerc,xcerc-1,2,2); }
xcerc–;
}
if((t==ENTER) && (a[xcerc][2]==0))
{
animate();
if(player==1) { player=2; xcerc=12; ycerc=0; drawround(xcerc,ycerc,ray,2); win();}
else { player=1; xcerc=1; ycerc=0; drawround(xcerc,ycerc,ray,14); win();}
}
}
}
void drawmap()
{
int i,j,k,l;
drawrect();
setcolor(9);
setfillstyle(1,4);
rectangle(20,65,625,415);
floodfill(200,200,9);
for(i=1;i<=12;i++)
for(j=1;j<=7;j++)
{
drawround(i,j,ray,0);
}
for(k=70;k<=570;k=k+50)
{
setcolor(0);
line(k,65,k,415);
}
for(l=115;l<=365;l=l+50)
{
setcolor(0);
line(20,l,625,l);
}
}
main()
{
clrscr();
init();
intro();
drawmap();
drawround(xcerc,ycerc,ray,14);
movecircle();
}