/*
Syed Mehroz Alam
CIS-61, First Year, Sec-A, Batch 2002-03
Computer & Information Systems Dept,
NED University, Karachi, Pakistan.
Email: smehrozalam@yahoo.com
13-June-2003
*/
#include
#include
#include
#include
#include
#include
#define ENTER_KEY '\r'
#define ESCAPE_KEY 27
#define SPECIAL_KEY 0
#define UP_ARROW 72
#define DOWN_ARROW 80
#define LEFT_ARROW 75
#define RIGHT_ARROW 77
#define pos_cursor(x,y) gotoxy(x*4+2,y*2+2)
// The following two parameters can be changed as per needed
const ORDER=3; // determines the order of the board
const level=3; // determines the computer level (1=easiest)
char board[ORDER][ORDER];
int computer=1;
int pos;
//The functions' purposes are evident from their names
void display(void);
void empty_board(void);
void input(int player);
int chkboard(void); //checks if any player has won
void counter_move(int player, int *best_i, int *best_j, int *best_pos);
void best_move(int player);
void main(void)
{
clrscr();
empty_board();
textcolor(15);
cprintf("Tic Tac Toe\n\r\n\r");
cprintf("Made by Syed Mehroz Alam\n\r");
cprintf("CIS-61, First Year, Batch 2002-03, \n\r");
cprintf("Computer & Information Systems Dept, \n\rNED University.\n\r" );
cprintf("Email: smehrozalam@yahoo.com" );
textcolor(7);
printf("\n\n\nDo you want to play versus COMPUTER ('n' for no) :");
char opt=getch();
if ( opt=='n' || opt=='N') computer=0;
int player=1, win=0,turn=1;
display();
do
{
printf("\nPlayer %d move \n", player);
printf("\nPress arrow keys to move, then press to place your mark");
input(player);
display();
win=chkboard();
player=(player==1)?2:1;
turn++;
}
while ( win==0 && turn<=ORDER*ORDER );
if (win) printf("\nPlayer %d won",win);
else printf("\nDraw");
printf("\n\nPlay again (y for yes) ");
opt=getch();
if ( opt=='y' || opt=='Y')
main();
else
exit(0);
getch();
}
void empty_board(void)
{
for (int i=0;iORDER-1) x=ORDER-1;
else if (y<0) y=0;
else if (y>ORDER-1) y=ORDER-1;
pos_cursor(x,y);
}
}
}
while(board[y][x]!=NULL);
char mark=(player==1)?'X':'O';
board[y][x]=mark;
}
int chkboard(void)
{
int player2_position=0;
int player1,player2;
//rows and cols check
for (int k=0;k<2;k++)
for (int i=0;i=0 && tmp_i[cnt]!=-1)
{
board[tmp_i[cnt]][tmp_j[cnt]]=NULL;
cnt--;
}
if (player==2)
{
if ( tot_postemp_pos )
{
best_i=i;
best_j=j;
tot_pos=temp_pos;
}
board[i][j]=NULL;
} // end if (board==NULL)
board[best_i][best_j]=mark[player];
// printf("i=%d,j=%d,pos=%d", best_i,best_j,best_pos);
}
void counter_move(int player, int *best_i, int *best_j, int *best_pos)
{
*best_i=*best_j=-1;
char mark=(player==1)?'X':'O';
*best_pos=(player==1)?3276:-3276;
for (int i=0;ipos )
{
*best_i=i;
*best_j=j;
*best_pos=pos;
}
board[i][j]=NULL;
} // end if (board==NULL)
if (*best_i==-1)
*best_pos=0;//(player==2)?3276:-3276;
return;
}
               (
geocities.com/smehrozalam)