#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
int board[10], turn = 1, flag = 0, player, comp;
int x1 = 220, y1 = 70, x2 = 230, y2 = 130;
int key, cp = 1, cx = 0, cy = 0;
enum { up = 72, left = 75, right = 77, down = 80} direction;
enum { yes , no } selected;
void menu();
void reset();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void take_input();
void status_bar();
void player_first();
void put_X_O(char *ch,int pos);
void user_choice(int ycolor,int ncolor,char *str1,char *str2);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
reset();
menu();
getch();
}
void reset()
{
int i;
for(i=1;i<10;i++)
board[i] = 2;
turn = 1;
flag = 0;
x1 = 170, y1 = 70, x2 = 230, y2 = 130;
}
void menu()
{
cleardevice();
if(yesno(0))
{
player = 0;
comp = 1;
cleardevice();
start_game();
}
else
{
player = 1;
comp = 0;
cleardevice();
player_first();
}
}
int make2()
{
if(board[5] == 2)
return 5;
if(board[2] == 2)
return 2;
if(board[4] == 2)
return 4;
if(board[6] == 2)
return 6;
if(board[8] == 2)
return 8;
return 0;
}
int make4()
{
if(board[1] == 2)
return 1;
if(board[3] == 2)
return 3;
if(board[7] == 2)
return 7;
if(board[9] == 2)
return 9;
return 0;
}
int posswin(int p)
{
// p==1 then X p==0 then O
int i;
int check_val,pos;
if(p == 1)
check_val = 18;
else
check_val = 50;
i = 1;
while(i<=9)//row check
{
if(board[i] * board[i+1] * board[i+2] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+1] == 2)
return i+1;
if(board[i+2] == 2)
return i+2;
}
i+=3;
}
i = 1;
while(i<=3)//column check
{
if(board[i] * board[i+3] * board[i+6] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+3] == 2)
return i+3;
if(board[i+6] == 2)
return i+6;
}
i++;
}
if(board[1] * board[5] * board[9] == check_val)
{
if(board[1] == 2)
return 1;
if(board[5] == 2)
return 5;
if(board[9] == 2)
return 9;
}
if(board[3] * board[5] * board[7] == check_val)
{
if(board[3] == 2)
return 3;
if(board[5] == 2)
return 5;
if(board[7] == 2)
return 7;
}
return 0;
}
void go(int n)
{
if(turn % 2)
board[n] = 3;
else
board[n] = 5;
turn++;
}
void player_first()
{
check_draw();
draw_board();
status_bar();
take_input();
}
void start_game()
{
// p==1 then X p==0 then O
if(posswin(comp))
{
go(posswin(comp));
flag = 1;
}
else
if(posswin(player))
go(posswin(player));
else
if(make2())
go(make2());
else
go(make4());
draw_board();
if(flag)
{
settextstyle(1,0,4);
settextjustify(1,1);
setcolor(128+RED);
outtextxy(320,380,"Computer wins");
getch();
exit(0);
}
else
player_first();
}
void check_draw()
{
if(turn > 9)
{
settextstyle(1,0,4);
settextjustify(1,1);
setcolor(128+RED);
outtextxy(320,380,"Game Draw");
getch();
exit(0);
}
}
void put_X_O(char *ch,int pos)
{
int m;
int x = 170, y = 70;
m = pos;
if(m > 3)
{
while(m > 3)
{
y += 100;
m -= 3;
}
}
if(pos % 3 == 0)
x += 200;
else
{
pos %= 3;
pos--;
while(pos)
{
x += 100;
pos--;
}
}
settextstyle(1,0,7);
settextjustify(1,1);
setcolor(9);
outtextxy(x+30,y+20,ch);
}
void draw_board()
{
int j;
setfillstyle(1,3);
bar(150,50,450,350);
setlinestyle(0,0,3);
setcolor(4);
rectangle(148,48,452,352);
line(250,50,250,350);
line(350,50,350,350);
line(150,150,450,150);
line(150,250,450,250);
setcolor(9);
rectangle(x1,y1,x2,y2);
for(j=1;j<10;j++)
{
if(board[j] == 3)
put_X_O("X",j);
else
if(board[j] == 5)
put_X_O("O",j);
}
}
void take_input()
{
status_bar();
while(1)
{
if(kbhit())
{
flushall();
key = getch();
//if status bar invoked
switch (key)
{
case 13:
if(board[cp] != 2)
break;
if(cp == posswin(player))
{
go(cp);
draw_board();
settextstyle(1,0,4);
settextjustify(1,1);
setcolor(128+RED);
outtextxy(320,380,"Player Wins");
getch();
exit(0);
}
go(cp);
draw_board();
start_game();
break;
case 32:
settextstyle(1,0,4);
settextjustify(1,1);
setcolor(128+BLUE);
outtextxy(320,240,"Press Space bar to continue...");
while(getch() != 32)
continue;
setcolor(0);
outtextxy(320,240,"Press Space bar to continue...");
draw_board();
break;
case 78:
case 110:
if(yesno(1))
{
cp = 1;
cx = 0;
cy = 0;
reset();
main();
}
cleardevice();
draw_board();
status_bar();
break;
case 88:
case 120:
if(yesno(1))
exit(0);
cleardevice();
status_bar();
draw_board();
break;
case up:
if(cx > 0 )
{
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(9);
rectangle(x1,y1-100,x2,y2-100);
y1=y1-100;
y2=y2-100;
cx=cx-1;
cp=cp-3;
}
break;
case down:
if(cx < 2 )
{
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(9);
rectangle(x1,y1+100,x2,y2+100);
y1=y1+100;
y2=y2+100;
cx=cx+1;
cp=cp+3;
}
break;
case left:
if(cy > 0 )
{
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(9);
rectangle(x1-100,y1,x2-100,y2);
x1=x1-100;
x2=x2-100;
cy=cy-1;
cp=cp-1;
}
break;
case right:
if(cy < 2 )
{
setcolor(3);
rectangle(x1,y1,x2,y2);
setcolor(9);
rectangle(x1+100,y1,x2+100,y2);
x1=x1+100;
x2=x2+100;
cy=cy+1;
cp=cp+1;
}
break;
}
}
}
}
int yesno(int choice)
{
int user_key;
char *s1, *s2, *s3;
selected = no;
settextstyle(1,0,4);
settextjustify(1,1);
setcolor(128+BLUE);
if(choice)
{
s1 = "Are you sure ?";
s2 = "YES";
s3 = "NO";
}
else
{
s1 = "Select your Symbol";
s2 = "O";
s3 = "X";
}
outtextxy(320,200,s1);
user_choice(0,2,s2,s3);
while(1)
{
user_key = getch();
switch(user_key)
{
case 75:
case 77:
if(selected == no)
{
user_choice(2,0,s2,s3);
selected=yes;
}
else
{
user_choice(0,2,s2,s3);
selected=no;
}
break;
case 13:
if(selected == yes)
return 1;
return 0;
}
}
}
void user_choice(int ycolor,int ncolor,char *str1,char *str2)
{
setfillstyle(1,ycolor);
bar(215,240,305,280);
outtextxy(260,260,str1);
setfillstyle(1,ncolor);
bar(335,240,425,280);
outtextxy(380,260,str2);
}
void status_bar()
{
setfillstyle(1,BLUE);
bar(95,430,490,460);
setcolor(GREEN);
settextstyle(1,0,3);
settextjustify(0,0);
outtextxy(100,450,"N : New");
outtextxy(220,450,"Space : Pause");
outtextxy(400,450,"X : Exit");
setlinestyle(0,1,4);
}