/*
Criss-Cross is a clone of popular game Tic-Tac-Toe
Copyright (C) 2013 Abhishek Baddi
This program is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
int board[3][3], r=20, play=1, coorx[9], coory[9], midx, midy, cellspc=50, x, y, i, j;
int xwin=0, owin=0;
void xoxo(int , int, int );
void drawboard();
int checkr(int );
int checkc(int );
int main() {
int gd=DETECT, gm, winner=-1,count=0;
char cha[8];
initgraph(&gd, &gm, "C:\\TC\\BGI");
midx=getmaxx()/2;
midy=getmaxy()/2;
itoa(midx,cha,10);
outtext(cha);
itoa(midy,cha,10);
outtext(cha);
start: count=1;
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
board[i][j]=2;
for(x=0,j=midy+cellspc;j>=midy-cellspc;j-=cellspc) {
for(i=midx-cellspc;i<=midx+cellspc;x++,i+=cellspc) {
coorx[x]=i;
coory[x]=j;
// outtext("x=");
// itoa(coorx[x],cha,10);
// outtext(cha);
// outtext("y=");
// itoa(coory[x],cha,10);
// outtext(cha);
// circle(coorx[x],coory[x],r);
// getch();
}
}
while(1) {
setcolor(WHITE);
drawboard();
if(kbhit()){
getcharacter:
setcolor(WHITE);
/*
itoa(board[0][0],cha,10);
outtextxy(0,midy,cha);
itoa(board[0][1],cha,10);
outtextxy(10,midy,cha);
itoa(board[0][2],cha,10);
outtextxy(20,midy,cha);
itoa(board[1][0],cha,10);
outtextxy(0,midy+30,cha);
itoa(board[1][1],cha,10);
outtextxy(10,midy+30,cha);
itoa(board[1][2],cha,10);
outtextxy(20,midy+30,cha);
itoa(board[2][0],cha,10);
outtextxy(0,midy+30+30,cha);
itoa(board[2][1],cha,10);
outtextxy(10,midy+30+30,cha);
itoa(board[2][2],cha,10);
outtextxy(20,midy+30+30,cha);*/
cha[0]=getch();
switch(cha[0]) {
case '1':
if(board[2][0]!=0&&board[2][0]!=1) {
xoxo(coorx[0],coory[0],play);
board[2][0]=play;
count++;
play^=0x1;
}
break;
case '2':
if(board[2][1]!=0&&board[2][1]!=1) {
xoxo(coorx[1],coory[1],play);
board[2][1]=play;
count++;
play^=0x1;
}
break;
case '3':
if(board[2][2]!=0&&board[2][2]!=1) {
xoxo(coorx[2],coory[2],play);
board[2][2]=play;
count++;
play^=0x1;
}
break;
case '4':
if(board[1][0]!=0&&board[1][0]!=1) {
xoxo(coorx[3],coory[3],play);
board[1][0]=play;
count++;
play^=0x1;
}
break;
case '5':
if(board[1][1]!=0&&board[1][1]!=1) {
xoxo(coorx[4],coory[4],play);
board[1][1]=play;
count++;
play^=0x1;
}
break;
case '6':
if(board[1][2]!=0&&board[1][2]!=1) {
xoxo(coorx[5],coory[5],play);
board[1][2]=play;
count++;
play^=0x1;
}
break;
case '7':
if(board[0][0]!=0&&board[0][0]!=1) {
xoxo(coorx[6],coory[6],play);
board[0][0]=play;
count++;
play^=0x1;
}
break;
case '8':
if(board[0][1]!=0&&board[0][1]!=1) {
xoxo(coorx[7],coory[7],play);
board[0][1]=play;
count++;
play^=0x1;
}
break;
case '9':
if(board[0][2]!=0&&board[0][2]!=1) {
xoxo(coorx[8],coory[8],play);
board[0][2]=play;
count++;
play^=0x1;
}
break;
case 'q':
goto end;
case 'r':
goto start;
case 'x':
owin=0;
xwin=0;
goto start;
default:
break;
}
}
winner=analyse();
if(winner>=0&&winner!=2){
if(winner==0){
setcolor(YELLOW);
outtextxy(midx,10,"The winner is O.");
owin++;
setcolor(WHITE);
goto getcharacter;
}
else if (winner==1) {
setcolor(GREEN);
outtextxy(midx,10,"The winner is X.");
xwin++;
setcolor(WHITE);
goto getcharacter;
}
}
if(count>9) {
setcolor(RED);
outtextxy(midx,10,"The game's a draw.");
setcolor(WHITE);
goto getcharacter;
}
delay(500);
clearviewport();
}
end:
getch();
closegraph();
return 0;
}
int analyse() {
int i,j;
for(i=0;i<3;i++){
if(checkr(i))
continue;
if(board[i][0]==board[i][1]&&board[i][0]==board[i][2]){
return board[i][0];
}
}
for(i=0;i<3;i++){
if(checkc(i))
continue;
if(board[0][i]==board[1][i]&&board[0][i]==board[2][i]){
return board[0][i];
}
}
if(board[0][0]==board[1][1]&&board[0][0]==board[2][2]) {
if(board[0][0]==2&&board[1][1]==2&&board[2][2]==2)
return 2;
else
return board[0][0];
}
if(board[0][2]==board[1][1]&&board[0][2]==board[2][0]) {
if(board[0][2]==2&&board[1][1]==2&&board[2][0]==2)
return 2;
else
return board[0][2];
}
return -1;
}
void xoxo(int x, int y,int p) {
if(p==0) {
setcolor(YELLOW);
circle(x,y,r);
}
else {
setcolor(GREEN);
line(x-r,y-r,x+r,y+r);
line(x+r,y-r,x-r,y+r);
}
setcolor(WHITE);
}
void drawboard() {
int i, j, x;
char cha[10];
setcolor(GREEN);
outtextxy(1,10,"X wins:");
itoa(xwin,cha,10);
outtextxy(110,10,cha);
setcolor(YELLOW);
outtextxy(1,30,"O wins:");
itoa(owin,cha,10);
outtextxy(110,30,cha);
setcolor(WHITE);
outtextxy(1,50,"Press r to restart, q to quit.");
line(midx-cellspc/2,midy-1.5*cellspc,midx-cellspc/2,midy+1.5*cellspc);
line(midx+cellspc/2,midy-1.5*cellspc,midx+cellspc/2,midy+1.5*cellspc);
line(midx-1.5*cellspc,midy-cellspc/2,midx+1.5*cellspc,midy-cellspc/2);
line(midx-1.5*cellspc,midy+cellspc/2,midx+1.5*cellspc,midy+cellspc/2);
for(x=0,j=2;j>=0;j--) {
for(i=0;i<3;i++,x++) {
if(board[j][i]!=2) {
// outtext(" ");
// itoa(x,cha,10);
// outtext(cha);
// outtext(",");
// itoa(coorx[x],cha,10);
// outtext(cha);
// outtext(",");
// itoa(coory[x],cha,10);
// outtext(cha);
xoxo(coorx[x],coory[x],board[j][i]);
}
}
}
}
int checkr(int i) {
if((board[i][0]==2)&&(board[i][1]==2)&&(board[i][2]==2))
return 1;
else
return 0;
}
int checkc(int i) {
if((board[0][i]==2)&&(board[1][i]==2)&&(board[2][i]==2))
return 1;
else
return 0;
}
int getcoor(int i, int j) {
for(x=0,j=2;j>=0;j--) {
for(i=0;i<3;i++,x++) {
if(board[j][i]!=2) {
return x;
}
}
}
}
Comments
Post a Comment