Tic Tac Toe

Previous Page       C Projects Home          Next Page

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <dos.h>

int location ;
void makeborder(int sr,int sc,int er,int ec)
{
int i;
for(i=sr;i<=er;i++)
{
gotoxy(sc,i);
putchar(179);
gotoxy(ec,i);
putchar(179);
}
for(i=sc;i<=ec;i++)
{
gotoxy(i,sr);
putchar(196);
gotoxy(i,er);
putchar(196);
}
gotoxy(sc,sr);
putchar(218);
gotoxy(sc,er);
putchar(192);
gotoxy(ec,sr);
putchar(191);
gotoxy(ec,er);
putchar(217);
}

void writexy(int r, int c ,char *str)
{
gotoxy(c,r);
printf("%s" ,str);
}

void makescreen(void)
{
int i;
for(i=10;i<15;i++)
{
writexy(i,37,"³"); //179

writexy(i,41,"³");
}
for(i=35;i<44;i++)
{
writexy(11,i,"Ä"); //196
writexy(13,i,"Ä");
}
writexy(11,37,"Å"); //197
writexy(11,41,"Å");
writexy(13,37,"Å");
writexy(13,41,"Å");
}
int array[9] = { 0,0,0,0,0,0,0,0,0 };
int winbox[9] = { 0,0,0,0,0,0,0,0,0 };
int getrow(int l)
{
int r;
if ( l == 1 || l == 2 || l == 3 )
r = 10;
if ( l == 4 || l == 5 || l == 6 )
r = 12;
if ( l == 7 || l == 8 || l == 9 )
r = 14;
return r;
}
int getcol(int l)
{
int c;
if ( l == 1 || l == 4 || l == 7 )
c = 36;
if ( l == 2 || l == 5 || l == 8 )
c = 39;
if ( l == 3 || l == 6 || l == 9 )
c = 42;
return c;
}
void place(int loc, int per)
{
int cr,cc;
cr = getrow(loc);
cc = getcol(loc);
if ( per == 1 )
writexy(cr,cc,"X");
else
writexy(cr,cc,"O");
}
void player1read(void)
{
int again = 0;
while ( again == 0 )
{
writexy(20,2,"Player 1 Chance . Select (1..9, 0 for Exit) : ");
writexy(20,2,"Player 1 Chance . Select (1..9, 0 for Exit) :");
scanf("%d",&location);
if ( location > 9 || location < 0 )
again = 0;
else
again = 1;
}
}
void player2read(void)
{
int again = 0;
while ( again == 0 )
{
writexy(20,2,"Player 2 Chance . Select (1..9, 0 for Exit) : ");
writexy(20,2,"Player 2 Chance . Select (1..9, 0 for Exit) :");
scanf("%d",&location);
if ( location > 9 || location < 0 )
again = 0 ;
else
again = 1;
}
}
int checktowin(void)
{
int win = 0;
if ( winbox[0] == 1 && winbox[1] == 1 && winbox[2] == 1 )
win = 1;
if ( winbox[3] == 1 && winbox[4] == 1 && winbox[5] == 1 )
win = 1;
if ( winbox[6] == 1 && winbox[7] == 1 && winbox[8] == 1 )
win = 1;
if ( winbox[0] == 1 && winbox[3] == 1 && winbox[6] == 1 )
win = 1;

if ( winbox[1] == 1 && winbox[4] == 1 && winbox[7] == 1 )
win = 1;
if ( winbox[2] == 1 && winbox[5] == 1 && winbox[8] == 1 )
win = 1;
if ( winbox[0] == 1 && winbox[4] == 1 && winbox[8] == 1 )
win = 1;
if ( winbox[2] == 1 && winbox[4] == 1 && winbox[6] == 1 )
win = 1;
/* Player 2 */
if ( winbox[0] == 2 && winbox[1] == 2 && winbox[2] == 2 )
win = 2;
if ( winbox[3] == 2 && winbox[4] == 2 && winbox[5] == 2 )
win = 2;
if ( winbox[6] == 2 && winbox[7] == 2 && winbox[8] == 2 )
win = 2;
if ( winbox[0] == 2 && winbox[3] == 2 && winbox[6] == 2 )
win = 2;
if ( winbox[1] == 2 && winbox[4] == 2 && winbox[7] == 2 )
win = 2;
if ( winbox[2] == 2 && winbox[5] == 2 && winbox[8] == 2 )
win = 2;
if ( winbox[0] == 2 && winbox[4] == 2 && winbox[8] == 2 )
win = 2;
if ( winbox[2] == 2 && winbox[4] == 2 && winbox[6] == 2 )
win = 2;

return win;
}

char player1[20],player2[20];

main()
{
int i,gameend,found,draw,quit,chance,ok,again,moves=0 ;
char *temp;
clrscr();
makeborder(1,1,23,79);
writexy(2,26," CROSS & BALL GAME DEMO ");
makescreen();
writexy(20,2," Give I Player Name : ");
scanf("%s",&player1);
writexy(20,2," ");
writexy(20,2," Give II Player Name : ");
scanf("%s",&player2);
writexy(20,2," ");
writexy(8,2,"Player 1 ( X ) :");
writexy(8,20,player1);
writexy(8,42,"Player 2 ( O ):");
writexy(8,62,player2);
draw = 0;
quit = 0;
gameend = 0 ;
chance = 0;
while (draw != 1 && quit != 1 && gameend != 1 )
{
moves = moves + 1;
if ( moves == 9 ) quit = 1;
if ( chance % 2 == 0 )
{
player1read();
while ( array[location-1] == 1 )
player1read();

if ( location == 0 )
quit = 1;
place(location,1);
array[location-1] = 1;
winbox[location-1] = 1;
}
else
{
player2read();
while ( array[location-1] == 1 )
player2read();
if ( location == 0 )
quit = 1;
place(location,2);
array[location-1] = 1;
winbox[location-1] = 2;
}
chance = chance + 1;
found = checktowin();
if ( found != 0 )
{
gameend = 1;
if ( found == 1 )
writexy(23,30," Player 1 Win ");
else
writexy(23,30," Player 2 Win ");
getch();
}
} /* End of While */
}

Previous Page       C Projects Home          Next Page