View Ravichandran J.V.'s profile on LinkedIn RJV 's Blog: TicTacToe - please ignore the missing include or the return statements in the C code as they have been modified for Blogger Tweet @ravichandranjv

Translate

TicTacToe - please ignore the missing include or the return statements in the C code as they have been modified for Blogger

GREYcells Tic-Tac-Toe
/************************************************************************/
/*   Designed & developed by J.V.RAVICHANDRAN.               */
/*  Started on 02/10/1997.     */
/*  Author's name --  J.V.RAVICHANDRAN        */
/*  Company's name  -- Semantix Computers.    */
/*  Company's address -- B-10, Triveni Apartments,   */
/*         Vikas puri, New Delhi - 110018.  */
/*  Completed on -- 10th October, 1997.    */
/*  Name of software -- GREYcells Tic-tac-toe    */
/*  Nature of software -- Game     */
/************************************************************************/
#include <\tc\library\graphics.h> 
#include <\tc\library\process.h> 
#include <\tc\library\io.h>
#include <\tc\library\dos.h>
#include <\tc\library\conio.h>
#include <\tc\library\string.h>
#include <\tc\library\stdlib.h>
#include <\tc\library\stdio.h>
#include <\tc\library\alloc.h>
#include <\tc\library\fcntl.h>
#include <\tc\library\stat.h>
#define ALT 1
#define ASCII 1
int gdriver = VGA, gmode=VGAHI, err_code,time_spent=0,boardvisible=0,counter=0;
int nkey,ikey,key,ik=1,boxrr=150,boxcc=175,boxr=125,boxc=150,ctr=0,cx=125,rx=25,start=0;
int player=0,color=15,ir,once=0,entered=1,rctr,menuflag;
int playertype1=0, playertype2=0,checkxflag=0,checkoflag=0,length,handle,handl;
int checkxflag,checkoflag,checkx1flag,checko1flag,menuitem=0,firsttime=0;
char str,firstletter,secondletter;
int playerno1=0, playerno2=0, err_flag=0,gameover=0,checkflag=0,once;
int menucol,menurow,menucoll,menuroww,item,player1lastcounter=10,player2lastcounter=0;
int messageboxc,messageboxr,messageboxcc,messageboxrr,rctr=0;
void *sbox;
int sctr=0,bctr=0,count,counter,lastcount,lastcounter;
int menuon=0,lastrctr,menuerror=0,sharecounter=0;
unsigned size;
int oxx1,oxx2,oxx3,oxx4,oxx5,oxx6,oxx7,oxx8,oxx9;
int letter3=0,letter4=1,winflag=0;
char *box[9],*playername[20];
int letter1, letter2,letter;
char score1,score2;
int soundon=1,incolor=12,soundctr,lettercolor=15,ct,midx,mee=0;
char terminator,leng,newstr;
main(){
 initgraph(&gdriver, &gmode, "");
 err_code = graphresult();
 if (err_code !=grOk)
 {
 printf("Graphic initialization error...\n");
  time_spent=0;
  while (time_spent<50 0l="" _fmode="O_BINARY;" _write="" counter="" ct="=-1){" d="" delay="" delaytic.="" exit="" fa_hidden="" handle="" if="" lseek="" pre="" seek_set="" sprintf="" terminator="" time_spent="">

   ct=access("termtic.$$$",0);
   if (ct==-1){
   setfillstyle(SOLID_FILL,15);
   bar(100,100,400,200);
   setfillstyle(SOLID_FILL,0);
   bar(101,101,399,120);
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   setcolor(15);
   outtextxy(220,111,"GREYcells");
   setcolor(0);
   outtextxy(110,135,"Intruder alert ! Please contact");
   outtextxy(110,150,"the developer immediately.");
   sound(100);
   delay(75);
   nosound();
   getch();
   cleardevice();
   closegraph();
   _close(handle);
   exit(0);
   ik=1;}
   if (ct==0){
   handle=open("termtic.$$$", O_RDONLY | O_BINARY);
   lseek(handle, 0L, SEEK_SET);
   counter=0;
   leng=filelength(handle);
   sscanf(&terminator,"%d",&leng);
   while (!eof(handle)){
   read(handle,&terminator,4);}
   }
   chmod("termtic.$$$",S_IWRITE);
   unlink("termtic.$$$");
   _fmode=O_BINARY;
   handle=_creat("termtic.$$$", FA_HIDDEN);
   lseek(handle, 0L, SEEK_SET);
   sscanf(&terminator,"%d",&counter);
   counter++;
   sprintf(&terminator,"%d",counter);
   _write(handle,&terminator,4);
   if (counter<28 counter="" if="" ik="0;}">28){
   sound(100);
   delay(25);
   sound(150);
   delay(25);
   sound(125);
   delay(25);
   nosound();
   alertformoney();
   getch();
   cleardevice();
   chmod("termtic.$$$",S_IREAD);
   _close(handle);
   ik=0;
   sharecounter=1;}
 if (ik<1 32:="" case="" color="15;}" cx="400;" delay="" firstscreen="" if="" ik="" item="=1){" key="" menu="" menuon="=2" nosound="" rx="250;" setcolor="" settextjustify="" settextstyle="" sound="" soundon="" switch="" while="">1){
   soundon=0;}
   if (soundon==1){
   setcolor(7);
   outtextxy(menucol+75,menurow+13,"Sound ( OFF )");
   setcolor(15);
   outtextxy(menucol+75,menurow+13,"Sound ( ON )");}
   if (soundon==0){
   setcolor(7);
   outtextxy(menucol+75,menurow+13,"Sound ( ON )");
   setcolor(15);
   outtextxy(menucol+75,menurow+13,"Sound ( OFF )");}
   }
   break;
  case ALT==1:
  case 129:
   if (menuon==0){
   menucol=15;
   menurow=14;
   menucoll=164;
   menuroww=57;
   messageboxc=menucol;
   messageboxr=menurow;
   messageboxcc=menucoll;
   messageboxrr=menuroww;
   savebox();
   dropdownmenu();
  
   setcolor(12);
   settextstyle(DEFAULT_FONT,VERT_DIR,1);
   settextjustify(0,1);
   outtextxy(20,11,"0");
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(8);
   if (soundon==1){
   outtextxy(menucol+75,menurow+13,"Sound ( ON )");}
   if (soundon==0){
   outtextxy(menucol+75,menurow+13,"Sound ( OFF )");}
   outtextxy(menucol+75,menurow+33,"Levels (1-3)");
   item=0;
   menuon=2;}
   break;
  case 35:
   if (menuon==0){
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   cx=120;
   setcolor(12);
   outtextxy(cx,43,"H");
   setcolor(15);
   cx+=textwidth("H");
   cx=cx+1;
   outtextxy(cx,43,"elp");
   menuitem=2;
   menuflag=0;
   menuon=1;}
   break;
  case 34:
   if (menuon==0){
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   cx=20;
   setcolor(12);
   outtextxy(cx,43,"G");
   cx+=textwidth("G");
   cx=cx+1;
   setcolor(15);
   outtextxy(cx,43,"ame");
   menuitem=1;
   menuflag=0;
   menuon=1;}
   break;
  case 27:
   if (menuon==2){
   restorebox();
   setcolor(7);
   settextstyle(DEFAULT_FONT,VERT_DIR,1);
   settextjustify(0,1);
   outtextxy(20,11,"0");
   menuon=0;}
   if (menuon==0){
   sound(50);
   delay(50);
   nosound();}
   if (menuon==1 && menuflag==1 && menuitem==1){
   restorebox();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   cx=20;
   setcolor(15);
   outtextxy(cx,43,"G");
   cx+=textwidth("G");
   cx=cx+1;
   setcolor(7);
   outtextxy(cx,43,"ame");
   menuon=0;
   menuflag=0;
   item=0;
   menuitem=1;
   }
   if (menuon==1 && menuflag==1 && menuitem==2){
   restorebox();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   cx=120;
   setcolor(15);
   outtextxy(cx,43,"H");
   setcolor(7);
   cx+=textwidth("H");
   cx=cx+1;
   outtextxy(cx,43,"elp");
   menuon=0;
   menuflag=0;
   menuitem=1;
   item=0;
   }
   break;
  case 75:
  if (start==4 && menuon==0){
  /* left arrow */
   boxoff();
   if (boxc>150){
   boxc=boxc-100;
   boxcc=boxcc-100;}
   if (boxc<150 77:="" arrow="" boxc="210;" boxcc="" boxoff="" boxon="" break="" case="" ctr="" if="" menuon="=0){" right="" start="=4">0){
   boxc=boxc+100;
   boxcc=boxcc+100;}
   boxon();
   }
   ctr++;
   break;
  case 80:
   if (menuon==2){
   if (item==0){
   threed_menu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   if (soundon==1){
   setcolor(7);
   outtextxy(menucol+75,menurow+13,"Sound ( OFF )");
   setcolor(15);
   outtextxy(menucol+75,menurow+13,"Sound ( ON )");}
   if (soundon==0){
   setcolor(7);
   outtextxy(menucol+75,menurow+13,"Sound ( ON )");
   setcolor(15);
   outtextxy(menucol+75,menurow+13,"Sound ( OFF )");}
   }
   if (item==1){
   twod_menu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(8);
   if (soundon==1){
   outtextxy(menucol+75,menurow+13,"Sound ( ON )");}
   if (soundon==0){
   outtextxy(menucol+75,menurow+13,"Sound ( OFF )");}
   setlinestyle(SOLID_LINE,1,1);
   threed_menuitem2();
   setcolor(15);
   outtextxy(menucol+75,menurow+33,"Levels (1-3)");}
   if (item==2){
   twod_menuitem2();
   setcolor(8);
   outtextxy(menucol+75,menurow+33,"Levels (1-3)");}
   sound(20);
   delay(75);
   nosound();
   item++;
   if (item==3){
   item=0;}
   }
   if (menuon==1 && menuflag==1 && menuitem==1){
   if (item==0){
   threed_menu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(15);
   outtextxy(menucol+50,menurow+13,"Start");}
   if (item==1){
   twod_menu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(8);
   outtextxy(menucol+50,menurow+13,"Start");
   setlinestyle(SOLID_LINE,1,1);
   threed_menuitem2();
   setcolor(15);
   outtextxy(menucol+50,menurow+33,"Quit");}
   if (item==2){
   twod_menuitem2();
   setcolor(8);
   outtextxy(menucol+50,menurow+33,"Quit");}
   sound(20);
   delay(75);
   nosound();
   item++;
   if (item==3){
   item=0;}
   }
   if (menuon==1 && menuflag==1 && menuitem==2){
   if (item==0){
   threed_menu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(15);
   outtextxy(menucol+75,menurow+13,"How to use...");}
   if (item==1){
   twod_menu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(8);
   outtextxy(menucol+75,menurow+13,"How to use...");
   setlinestyle(SOLID_LINE,1,1);
   threed_menuitem2();
   setcolor(15);
   outtextxy(menucol+70,menurow+33,"About");}
   if (item==2){
   twod_menuitem2();
   setcolor(8);
   outtextxy(menucol+70,menurow+33,"About");}
   sound(20);
   delay(75);
   nosound();
   item++;
   if (item==3){
   item=0;}
   }
   if (menuon==1 && menuitem==1 && menuflag==0){
   menucol=20;
   menurow=48;
   menucoll=120;
   menuroww=91;
   messageboxc=menucol;
   messageboxr=menurow;
   messageboxcc=menucoll;
   messageboxrr=menuroww;
   savebox();
   dropdownmenu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(8);
   outtextxy(menucol+50,menurow+13,"Start");
   outtextxy(menucol+50,menurow+33,"Quit");
   item=0;
   menuflag=1;}
   if (menuon==1 && menuitem==2 && menuflag==0){
   menucol=120;
   menurow=48;
   menucoll=259;
   menuroww=91;
   messageboxc=menucol;
   messageboxr=menurow;
   messageboxcc=menucoll;
   messageboxrr=menuroww;
   savebox();
   dropdownmenu();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(1,1);
   setcolor(8);
   outtextxy(menucol+75,menurow+13,"How to use...");
   outtextxy(menucol+70,menurow+33,"About");
   item=0;
   menuflag=1;}
   if (start==4 && menuon==0){
   if (boxc>150){
   boxoff();
   if (boxrr<320 72:="" boxoff="" boxon="" boxr="" boxrr="boxrr+85;}" break="" case="" if="" menuon="=0){" start="=4">150){
   boxr=boxr-85;
   boxrr=boxrr-85;
   }
   boxon();}
   break;   
  case 88:
  /*  caps  x */
  if (start==4 && checkxflag==0 && menuon==0){
   checkflag=1;
   boxoff();
   mee=2;
   error();
   if (gameover==-1){gameover=0;checkxflag=0;}
   checkflag=0;}
  if (start==4 && checkxflag==1 && menuon==0){
   boxoff();
  if (playertype1==1 || playertype2==1){
   if (winflag==0){
   scanboxes();
   setstatus();
   boxrr=150;
   boxcc=175;
   boxr=125;
   boxc=150;
   boxon();}}
   if (winflag==0 && err_flag==0){
   setplayer();}
   if (winflag==1 && err_flag==0){
   start=5;}
   }
  if (playertype2==2 && winflag==0 && err_flag==0){
   once=0;
   computer_o();
   writectext();
   if (once==2){display_win();}
   boxrr=150;
   boxcc=175;
   boxr=125;
   boxc=150;
   boxon();
   if (winflag==0 && err_flag==0){
   setplayer();}
   if (winflag==1 && err_flag==0){
   start=5;}}
   if (entered>=9 && winflag==0 && gameover==0){
   gameover=1;
   error();
   start=5;}
   break;
  case 79:
  /* caps O*/
   if (start==4 && checkoflag==0 && menuon==0){
   checkflag=1;
   boxoff();
   mee=2;
   error();   
   checkflag=0;}
  if (start==4 && checkoflag==1 && menuon==0){
   boxoff();
  if (playertype1==1 || playertype2==1){
   if (winflag==0){
   scanboxes();
   setstatus();
   boxrr=150;
   boxcc=175;
   boxr=125;
   boxc=150;
   boxon();}}
   if (winflag==0 && err_flag==0){
   setplayer();}
   if (winflag==1 && err_flag==0){
   start=5;}
   }
   if (playertype2==2 && winflag==0){
   once=0;
   computer_o();
   writeotext();
   if (once==2){display_win();}
   boxrr=150;
   boxcc=175;
   boxr=125;
   boxc=150;
   boxon();
   if (winflag==0 && err_flag==0){
   setplayer();}
   if (winflag==1 && err_flag==0){
   start=5;}}
   if (entered>=9 && winflag==0 && gameover==0){
   gameover=1;
   error();
   start=5;}
   break;
  case 13:
   if (menuon==1 && menuitem==1){
   restorebox();
   if (item==0){
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   cx=20;
   setcolor(15);
   outtextxy(cx,43,"G");
   cx+=textwidth("G");
   cx=cx+1;
   setcolor(7);
   outtextxy(cx,43,"ame");}
   if (item==1){
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   cx=20;
   setcolor(15);
   outtextxy(cx,43,"G");
   cx+=textwidth("G");
   cx=cx+1;
   setcolor(7);
   outtextxy(cx,43,"ame");
   gameover=0;   
   clearboard();
   boxrr=150;
   boxcc=175;
   boxr=125;
   boxc=150;
   ctr=1;
   while (ctr<10 about="" ame="" box="" checkoflag="1;}" checkxflag="0;" ctr="1;" cx="" delay="" elp="" entered="1;" howtouse="" if="" ik="1;" item="=2){" letter1="=2){" lettercolor="12;" menuflag="0;" menuitem="=2){" menuon="0;}</pre" nosound="" outtextxy="" player="1;" quit="" restorebox="" setcolor="" setplayer="" settextjustify="" settextstyle="" sound="" start="4;" strcpy="" textwidth="" winflag="0;}">

   if (menuon==2){
   restorebox();
   setcolor(7);
   settextstyle(DEFAULT_FONT,VERT_DIR,1);
   settextjustify(0,1);
   outtextxy(20,11,"0");
   if (item==2){
   shareware();}
   menuon=0;}
   break;
 }
 }
cleardevice();
closegraph();
return 0;}
int scanboxes(){
 sscanf(&box[1],"%d",&oxx1);
 sscanf(&box[2],"%d",&oxx2);
 sscanf(&box[3],"%d",&oxx3);
 sscanf(&box[4],"%d",&oxx4);
 sscanf(&box[7],"%d",&oxx7);
 sscanf(&box[5],"%d",&oxx5);
 sscanf(&box[6],"%d",&oxx6);
 sscanf(&box[8],"%d",&oxx8);
 sscanf(&box[9],"%d",&oxx9);
return 1;}
int computer_o(){
 scanboxes();
 if (entered==2){
 if (oxx5==2 && once==0){
 /*  capture box5 */
 boxfive();
 once=1;}
 if (oxx5==0 && once==0){
 if (oxx1==2){
 /*  capture box1 */
 boxone();}
 once=1;}
 }
 if (entered==4 || entered==5){
 defensivemoves();
 if (once==0){
 firstoffence();}
}
 if (entered>5){
 if (once==0){
 offensivemoves();}
 if (once==0){
 defensivemoves();}
 if (once==0){
 firstoffence();}
 }
 if (once!=0){writeforcomputer();scanboxes();checkcomputerwin();}
return (once);}

int firstoffence(){
/* offensive moves for computer*/
if (oxx1==1 && oxx2==2 && once==0){
boxtwo();once=1;}
if (oxx1==1 && oxx3==2 && once==0){
boxthree();once=1;}
if (oxx5==1 && oxx2==2 && once==0){
boxtwo();once=1;}
if (oxx5==1 && oxx3==2 && once==0){
boxthree();once=1;}
if (oxx5==1 && oxx4==2 && once==0){
boxfour();once=1;}
if (oxx5==1 && oxx6==2 && once==0){
boxsix();once=1;}
if (oxx5==1 && oxx9==2 && once==0){
boxnine();once=1;}
if (oxx5==1 && oxx7==2 && once==0){
boxseven();once=1;}
if (oxx1==1 && oxx4==2 && once==0){
boxfour();once=1;}
if (oxx1==1 && oxx7==2 && once==0){
boxseven();once=1;}
if (oxx1==1 && oxx9==2 && once==0){
boxnine();once=1;}
if (oxx1==1 && oxx5==2 && once==0){
boxfive();once=1;}
return (once);}
int offensivemoves(){
/* offensive moves for computer*/
if (oxx1==1 && oxx2==1 && oxx3==2 && once==0){
boxthree();once=2;}
if (oxx1==1 && oxx4==1 && oxx7==2 && once==0){
boxseven();once=2;}
if (oxx1==1 && oxx7==1 && oxx4==2 && once==0){
boxfour();once=2;}
if (oxx1==1 && oxx9==1 && oxx5==2 && once==0){
boxfive();once=2;}
if (oxx2==1 && oxx3==1 && oxx1==2 && once==0){
boxone();once=2;}
if (oxx1==1 && oxx3==1 && oxx2==2 && once==0){
boxtwo();once=2;}
if (oxx4==1 && oxx7==1 && oxx1==2 && once==0){
boxone();once=2;}
if (oxx2==1 && oxx5==1 && oxx8==2 && once==0){
boxeight();once=2;}
if (oxx2==1 && oxx8==1 && oxx5==2 && once==0){
boxfive();once=2;}
if (oxx5==1 && oxx8==1 && oxx2==2 && once==0){
boxtwo();once=2;}
if (oxx3==1 && oxx6==1 && oxx9==2 && once==0){
boxnine();once=2;}
if (oxx6==1 && oxx9==1 && oxx3==2 && once==0){
boxthree();once=2;}
if (oxx3==1 && oxx9==1 && oxx6==2 && once==0){
boxsix();once=2;}
if (oxx4==1 && oxx5==1 && oxx6==2 && once==0){
boxsix();once=2;}
if (oxx5==1 && oxx6==1 && oxx4==2 && once==0){
boxfour();once=2;}
if (oxx4==1 && oxx6==1 && oxx5==2 && once==0){
boxfive();once=2;}
if (oxx7==1 && oxx8==1 && oxx9==2 && once==0){
boxnine();once=2;}
if (oxx7==1 && oxx9==1 && oxx8==2 && once==0){
boxeight();once=2;}
if (oxx8==1 && oxx9==1 && oxx7==2 && once==0){
boxseven();once=2;}
if (oxx1==1 && oxx5==1 && oxx9==2 && once==0){
boxnine();once=2;}
if (oxx5==1 && oxx9==1 && oxx1==2 && once==0){
boxone();once=2;}
if (oxx1==1 && oxx9==1 && oxx5==2 && once==0){
boxfive();once=2;}
if (oxx3==1 && oxx5==1 && oxx7==2 && once==0){
boxseven();once=2;}
if (oxx3==1 && oxx7==1 && oxx5==2 && once==0){
boxfive();once=2;}
if (oxx7==1 && oxx5==1 && oxx3==2 && once==0){
boxthree();once=2;}
return (once);}
int defensivemoves(){
 /* Defensive moves for computer*/
  if (oxx7==0 && oxx4==0 && oxx1==2 && once==0){
  boxone();once=1;}
  if (oxx1==0 && oxx2==0 && oxx3==2 && once==0){
  boxthree();once=1;}
  if (oxx5==0 && oxx3==0 && oxx7==2 && once==0){
  boxseven();once=1;}
  if (oxx5==0 && oxx7==0 && oxx3==2 && once==0){
  boxthree();once=1;}
  if (oxx1==0 && oxx4==0 && oxx7==2 && once==0){
  boxseven();once=1;}
  if (oxx1==0 && oxx7==0 && oxx4==2 && once==0){
  boxfour();once=1;}
  if (oxx3==0 && oxx7==0 && oxx5==2 && once==0){
  boxfive();once=1;}
  if (oxx2==0 && oxx5==0 && oxx8==2 && once==0){
  boxeight();once=1;}
  if (oxx2==0 && oxx3==0 && oxx1==2 && once==0){
  boxone();once=1;}
  if (oxx2==0 && oxx8==0 && oxx5==2 && once==0){
  boxfive();once=1;}
  if (oxx5==0 && oxx8==0 && oxx2==2 && once==0){
  boxtwo();once=1;}
  if (oxx3==0 && oxx6==0 && oxx9==2 && once==0){
  boxnine();once=1;}
  if (oxx3==0 && oxx9==0 && oxx6==2 && once==0){
  boxsix();once=1;}
  if (oxx6==0 && oxx9==0 && oxx3==2 && once==0){
  boxthree();once=1;}
  if (oxx4==0 && oxx5==0 && oxx6==2 && once==0){
  boxsix();once=1;}
  if (oxx4==0 && oxx6==0 && oxx5==2 && once==0){
  boxfive();once=1;}
  if (oxx6==0 && oxx5==0 && oxx4==2 && once==0){
  boxfour();once=1;}
  if (oxx7==0 && oxx8==0 && oxx9==2 && once==0){
  boxnine();once=1;}
  if (oxx7==0 && oxx9==0 && oxx8==2 && once==0){
  boxeight();once=1;}
  if (oxx9==0 && oxx8==0 && oxx7==2 && once==0){
  boxseven();once=1;}
return (once);}
int drawboard(){
 setfillstyle(SOLID_FILL,8);
 bar(0,0,640,480);
 setlinestyle(SOLID_LINE,1,1);
 setcolor(15);
 line(5,50,634,50);
 setcolor(0);
 line(4,21,4,480);
 line(0,475,640,475);
 line(636,21,636,480);
 setfillstyle(SOLID_FILL,0);
 bar(1,1,639,20);
 setcolor(15);
 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 settextjustify(0,1);
 outtextxy(290,12,"Tic-tac-toe");
 setcolor(7);
 settextstyle(DEFAULT_FONT,VERT_DIR,1);
 settextjustify(0,1);
 outtextxy(20,11,"0");
 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 settextjustify(0,1);
 cx=20;
 setcolor(15);
 outtextxy(cx,43,"G");
 cx+=textwidth("G");
 cx=cx+1;
 setcolor(7);
 outtextxy(cx,43,"ame");
 cx=120;
 setcolor(15);
 outtextxy(cx,43,"H");
 setcolor(7);
 cx+=textwidth("H");
 cx=cx+1;
 outtextxy(cx,43,"elp");
 outtextxy(420,27,"Player -");
 outtextxy(420,43,"Score -");
 outtextxy(45,450,"Player 1 -");
 outtextxy(45,465,"Score -");
 outtextxy(445,450,"Player 2 -");
 outtextxy(445,465,"Score -");
 setcolor(15);
 /* board */
 setlinestyle(SOLID_LINE,1,3);
 setcolor(15);
 line(200,175,450,175);
 line(200,275,450,275);
 line(275,100,275,350);
 line(375,100,375,350);
 setfillstyle(SOLID_FILL,8);
 bar(520,23,625,47);
 setcolor(15);
 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 settextjustify(0,1);
 counter=0;
 cx=495;
 while (counter

 if (letter1==1){
 lettercolor=15;}
 if (letter1==2){
 lettercolor=12;}
 boardvisible=1;
 entered=1;
 player=1;
 if (letter1==1){
 checkxflag=1;
 lettercolor=15;
 checkoflag=0;}
 if (letter1==2){
 checkxflag=0;
 lettercolor=12;
 checkoflag=1;}
 gameover=0;
 checkflag=0;
 boxrr=150;
 boxcc=175;
 boxr=125;
 boxc=150;
 boxon();
 setcolor(15);
 outtextxy(510,45,&score1);
 ctr=1;
 while (ctr<10 1="" bar="" box="" boxc="" boxcc="" boxoff="" boxr="" boxrr="" ctr="1;" if="" int="" return="" setfillstyle="" strcpy="">150){
 setlinestyle(SOLID_LINE,1,1);
 setcolor(8);
 line(boxc,boxr,boxcc+5,boxr);
 line(boxc,boxr,boxc,boxrr);
 line(boxc,boxrr,boxcc+5,boxrr);
 line(boxcc+5,boxr,boxcc+5,boxrr);}
return 1;}
int boxon(){
 if (boxc==150){
 setfillstyle(SOLID_FILL,15);
 bar(boxc,boxr,boxcc,boxrr); 
 setfillstyle(SOLID_FILL,0);
 bar(boxc+1,boxr+1,boxcc-1,boxrr-1);}
 if (boxc>150){
 setlinestyle(SOLID_LINE,1,1);
 setcolor(15);
 line(boxc,boxr,boxcc+5,boxr);
 line(boxc,boxr,boxc,boxrr);
 line(boxc,boxrr,boxcc+5,boxrr);
 line(boxcc+5,boxr,boxcc+5,boxrr);
 }
return 1;}
int clearboard(){
 setfillstyle(SOLID_FILL,8);
 bar(25,100,575,400);
 boardvisible=0;
 /* board */
 setlinestyle(SOLID_LINE,1,3);
 setcolor(15);
 line(200,175,450,175);
 line(200,275,450,275);
 line(275,100,275,350);
 line(375,100,375,350);
 boardvisible=1;
 entered=1;
 player=1;
 gameover=0;
 checkflag=0;
 boxrr=150;
 boxcc=175;
 boxr=125;
 boxc=150;
 boxon();
 setfillstyle(SOLID_FILL,8);
 bar(520,23,625,47);
 setcolor(15);
 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 settextjustify(0,1);
 counter=0;
 cx=525;
 while (counter0){
   rctr--;}
   if (rctr==0){
   rctr=0;}}
   if (rx==350){
   if (rctr>player1lastcounter){
   rctr--;}}
   curs_off();
   cx-=textwidth(&nkey);
   setfillstyle(SOLID_FILL,8);
   bar(cx,rx-10,cx+10,rx+10);
   if (cx<=250){
   cx=250;}
   curs_on();
   break;
  case 13:
   if (playertype1==1 && playertype2==1 && rctr==0){
   messageboxc=125;
   messageboxr=125;
   messageboxcc=450;
   messageboxrr=200;
   midx=265;
   sound(100);
   delay(75);
   nosound();
   savebox();
   errorscreen();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   setusercharsize(1,3,1,1);
   settextjustify(0,1);
   setcolor(12);
   outtextxy(messageboxc+5,messageboxr+5,"Error");
   setcolor(0);
   outtextxy(messageboxc+5,messageboxr+35,"Mandatory to enter a name.");
   getch();
   restorebox();
   rctr=0;
   curs_off();
   if (rx==325 || rx==322){
   cx=250;
   rx=322;
   curs_on();}
   if (rx==350 || rx==346){
   cx=250;
   rx=346;
   curs_on();}
   err_flag=1;}
   if (playertype1==2 && playertype2==1 && rctr>0){
   curs_off();
   rctr=0;
   cx=250;
   rx=350;}
   if (playertype1==1 && rctr>0 && entered==1 && err_flag==0){
   if (rx==325 || rx==322){
   curs_off();
   player1lastcounter=rctr;
   if (playertype2==1){
   cx=250;
   rx=350;}
   curs_on();
   entered=0;}}
   if (playertype2==2 && err_flag==0){
   curs_off();
   entered=2;}
   if (playertype2==1 && err_flag==0 && rctr>0 && entered==1){
   if (rx==346 || rx==350){
   player2lastcounter=player1lastcounter+rctr;
   rctr=0;
   curs_off();
   entered=2;}}
   err_flag=0;
   break;
  default:  
   if (start==0){
   if (rctr<10 if="" rctr="" rx="" writename="">=player1lastcounter && rctr<20 1="" break="" pre="" return="" rx="=350){" writename="">


int writename(){
 entered=1;
 err_flag=0;
 curs_off();
 setcolor(15);
 outtextxy(cx,rx,&nkey);
 cx+=textwidth(&nkey);
 curs_on();
 strcpy(&playername[rctr],&nkey);
 rctr++;
return (rctr);}
int menu(){
drawmenuscreen();
while (once<4 if="" incolor="">15){
 incolor=1;}
 setcolor(incolor);
 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 settextjustify(0,1);   
 outtextxy(10,35,"Press  for help");
 outtextxy(300,13,"GREYcells");
 ikey=getch();
 switch(ikey){
  case 59:
   if (rx==50 || rx==75){
   messageboxc=125;
   messageboxr=125;
   messageboxcc=515;
   messageboxrr=250;
   midx=295;
   savebox();
   errorscreen();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   setusercharsize(1,3,1,1);
   settextjustify(0,1);
   setcolor(12);
   outtextxy(messageboxc+5,messageboxr+5,"Help");
   scrollplayerhelp();
   getch();
   restorebox();
   }
   if (rx==250 || rx==275){
   messageboxc=125;
   messageboxr=125;
   messageboxcc=475;
   messageboxrr=275;
   midx=275;
   savebox();
   errorscreen();
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   setusercharsize(1,3,1,1);
   settextjustify(0,1);
   setcolor(12);
   outtextxy(messageboxc+5,messageboxr+5,"Help");
   playerletterhelp();
   getch();
   restorebox();
   }
   break;
  case 80:
   if (sharecounter==1 && rx<250 bar="" ctr="" if="" outtextxy="" setcolor="" setfillstyle="" settextjustify="" settextstyle="" uman="">1){
   alertformoney();
   getch();
   cleardevice();
   drawmenuscreen();
   ctr=0;}
   }
   if (once<2 bar="" ctr="" if="" omputer="" outtextxy="" setcolor="" setfillstyle="" settextjustify="" settextstyle="" sharecounter="=0){" uman="">2){
   ctr=0;}
   }
   break;
  case 72:
   if (sharecounter==1 && rx<250 13:="" 79:="" 88:="" bar="" break="" caps="" case="" checkoflag="1;" checkxflag="0;" ctr--="" ctr="" curs_off="" cx="" firstletter="" if="" ikey="" letter1="2;" letter2="1;" o="" omputer="" once--="" once="=0" outtextxy="" playerno1="1;" playerno2="1;" rx="5;}" secondletter="" setcolor="" setfillstyle="" settextjustify="" settextstyle="" sharecounter="=0){" strcpy="" uman="" x="">0){
   if (ctr==1){
   settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
   settextjustify(0,1);
   outtextxy(cx,rx,"Human");
   setfillstyle(SOLID_FILL,8);
   bar(145,135,300,165);
   setcolor(15);
   outtextxy(150,150,"Human");
   rx=rx+25;
   playertype1=1;}
   if (ctr==2){
   settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
   settextjustify(0,1);
   outtextxy(cx,rx,"Computer");
   setfillstyle(SOLID_FILL,8);
   bar(145,185,300,215);
   setcolor(15);
   outtextxy(150,200,"Computer");
   playertype1=2;}
   ctr=0;
   }
   if (once>0 && once<2 ctr="">0){
   if (ctr==1){
   settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
   settextjustify(0,1);
   outtextxy(cx,rx,"Human");
   playertype2=1;
   player=0;}
   if (ctr==2){
   settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
   settextjustify(0,1);
   outtextxy(cx,rx,"Computer");
   playertype2=2;
   player=0;}
   }
   if (playertype1==2){
   menuerror=2;
   gameover=2;
   checkflag=1;
   error();
   ctr=0;
   once=-1;
   player=1;
   setfillstyle(SOLID_FILL,8);
   bar(10,25,600,125);
   setcolor(15);
   settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
   settextjustify(0,1);
   outtextxy(100,50,"Player 1:");
   outtextxy(100,75,"Player 2:");
   playertype1=0;
   playertype2=0;
   cx=225;
   rx=50;}
   if (playertype1==2 && playertype2==2){
   menuerror=1;
   gameover=2;
   checkflag=1;
   error();
   ctr=0;
   once=-1;
   player=1;
   setfillstyle(SOLID_FILL,0);
   bar(10,10,600,125);
   setcolor(15);
   settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
   settextjustify(0,1);
   outtextxy(100,50,"Player 1:");
   outtextxy(100,75,"Player 2:");
   playertype1=0;
   playertype2=0;
   cx=225;
   rx=50;}
   if (player==0){
   cx=425;
   rx=250;
   curs_on();
   player=1;
   once=3;}
   if (rx==5){
   ctr=0;
   rctr=0;
   lastrctr=0;
   entered=1;
   if (playertype1==1){
   cx=250;
   rx=325;}
   if (playertype1==2){
   cx=250;
   rx=350;}
   curs_on();
   start=0;
   name();
   curs_off();
   cleardevice();
   drawboard();
   boxrr=150;
   boxcc=175;
   boxr=125;
   boxc=150;
   boxon();
   start=4;
   menuon=0;
   ik=0;
   rx=0;
   once=4;
   }
   if (once<2 1="" bar="" break="" curs_off="" curs_on="" cx="" int="" once="" pre="" return="" rx-5="" rx="" setfillstyle="">

int dropdownmenu(){
 settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
 settextjustify(1,1);
 setfillstyle(SOLID_FILL,15);
 bar(menucol,menurow+2,menucoll,menuroww);
 setfillstyle(SOLID_FILL,0);
 bar(menucol+1,menurow+3,menucoll-1,menuroww-1);
 setfillstyle(SOLID_FILL,7);
 bar(menucol+4,menurow+5,menucoll-3,menurow+20);
 bar(menucol+4,menurow+25,menucoll-3,menurow+41);
#if 0
 bar(menucol+4,menurow+37,menucoll-4,menurow+53);
#endif
 setlinestyle(SOLID_LINE,1,2);
 setcolor(15);
 line(menucol+4,menurow+5,menucoll-3,menurow+5);
 line(menucol+4,menurow+5,menucol+4,menurow+20);
 line(menucol+4,menurow+25,menucoll-3,menurow+25);
 line(menucol+4,menurow+25,menucol+4,menurow+41);
 setcolor(0);
 line(menucol+4,menurow+20,menucoll-3,menurow+20);
 line(menucoll-3,menurow+5,menucoll-3,menurow+20);
 line(menucol+4,menurow+41,menucoll-3,menurow+41);
 line(menucoll-3,menurow+25,menucoll-3,menurow+41);
 /* line in between menu items */
 setcolor(15);
 line(menucol,menurow+22,menucoll,menurow+22);
return 1;}
int setstatus(){
 switch(boxr){
 case 125:
  if (boxc==150){
  mee=2;
  error();}
  if (boxc==210){
   if (oxx1!=2){
   mee=2;
   error();}
   if (oxx1==2){
   writetext();}}
  if (boxc==310){
   if (oxx4!=2){
   mee=2;
   error();}
   if (oxx4==2){
   writetext();}
   }
  if (boxc==410){
   if (oxx7!=2){
   mee=2;
   error();}
   if (oxx7==2){
   writetext();}
   }
   break;
 case 210:
  if (boxc==150){
  mee=2;
  error();}
  if (boxc==210){
   if (oxx2!=2){
   mee=2;
   error();}
   if (oxx2==2){
   writetext();}
   }
  if (boxc==310){
   if (oxx5!=2){
   mee=2;
   error();}
   if (oxx5==2){
   writetext();}
   }    
  if (boxc==410){
   if (oxx8!=2){
   mee=2;
   error();}
   if (oxx8==2){
   writetext();}
   }
   break;
  case 295:
  if (boxc==150){
  mee=2;
  error();}
  if (boxc==210){
   if (oxx3!=2){
   mee=2;
   error();}
   if (oxx3==2){
   writetext();}
   }
  if (boxc==310){
   if (oxx6!=2){
   mee=2;
   error();}
   if (oxx6==2){
   writetext();}
   }
  if (boxc==410){
   if (oxx9!=2){
   mee=2;
   error();}
   if (oxx9==2){
   writetext();}
   }
   break;}
  if (player==1 && playertype1==1 && winflag==0){
  writeforcomputer();check_xwin();}
  if (player==2 && playertype2==1 && winflag==0){
  writeforcomputer();check_owin();}
return (winflag);}

int check_xwin(){
 scanboxes();
 if (winflag==0){
 if (oxx1==0 && oxx4==0 && oxx7==0){
 display_win();}
 if (oxx1==0 && oxx2==0 && oxx3==0){
 display_win();}
 if (oxx1==0 && oxx5==0 && oxx9==0){
 display_win();}
 if (oxx4==0 && oxx5==0 && oxx6==0){
 display_win();}
 if (oxx7==0 && oxx8==0 && oxx9==0){
 display_win();}
 if (oxx3==0 && oxx5==0 && oxx7==0){
 display_win();}
 if (oxx3==0 && oxx6==0 && oxx9==0){
 display_win();}
 if (oxx2==0 && oxx5==0 && oxx8==0){
 display_win();}
 }
return (winflag);}
int check_owin(){
 scanboxes();
 if (winflag==0){
 if (oxx1==1 && oxx4==1 && oxx7==1){
 display_win();}
 if (oxx1==1 && oxx2==1 && oxx3==1){
 display_win();}
 if (oxx1==1 && oxx5==1 && oxx9==1){
 display_win();}
 if (oxx4==1 && oxx5==1 && oxx6==1){
 display_win();}       
 if (oxx7==1 && oxx8==1 && oxx9==1){
 display_win();}
 if (oxx3==1 && oxx5==1 && oxx7==1){
 display_win();}
 if (oxx3==1 && oxx6==1 && oxx9==1){
 display_win();}
 if (oxx2==1 && oxx5==1 && oxx8==1){
 display_win();}
 }
return (winflag);}
int writetext(){
 boxoff();
 setcolor(lettercolor);
 settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
 settextjustify(0,1);
 outtextxy(boxcc-15,boxrr-15,&key);
 err_flag=0;
return (err_flag);}
int writectext(){
 boxoff();
 setcolor(lettercolor);
 settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
 settextjustify(0,1);
 outtextxy(boxcc-15,boxrr-15,"O");
 err_flag=0;
return (err_flag);}
int writeotext(){
 boxoff();
 setcolor(lettercolor);
 settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
 settextjustify(0,1);
 outtextxy(boxcc-15,boxrr-15,"X");
 err_flag=0;
return (err_flag);}
int error(){
   messageboxc=150;
   messageboxr=173;
   messageboxcc=500;
   messageboxrr=300;
   savebox();
   midx=295;
   errorscreen();
   if (mee==1){
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   setusercharsize(1,3,1,1);
   settextjustify(0,1);
   setcolor(4);
   outtextxy(messageboxc+5,messageboxr+5,"Game");}
   if (mee==2){
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   setusercharsize(1,3,1,1);
   settextjustify(0,1);
   setcolor(4);
   outtextxy(messageboxc+5,messageboxr+5,"Error");}
   if (soundon==1){
   sound(275);
   delay(150);
   sound(375);
   delay(250);
   nosound();}
   if (menuerror==1){
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   setcolor(1);
   outtextxy(messageboxc+15,messageboxr+40,"Computer cannot");
   outtextxy(messageboxc+15,messageboxr+55,"play from both sides !");
   gameover=3;
   menuerror=0;}
   if (menuerror==2){   
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   setcolor(1);
   outtextxy(messageboxc+10,messageboxr+40,"Computer cannot play the first move in");
   outtextxy(messageboxc+10,messageboxr+55,"this version. This feature will be");
   outtextxy(messageboxc+10,messageboxr+70,"provided in the next version of the");
   outtextxy(messageboxc+10,messageboxr+85,"software !");
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   gameover=3;
   menuerror=0;}
   if (gameover==0 && checkflag==1){
   settextstyle(TRIPLEX_FONT,HORIZ_DIR,3);
   settextjustify(0,1);
   if (player==1){
   letter=letter1;
   counter=0;
   cx=messageboxc+15;
   while (counter

   if (playertype2==2){
   outtextxy(cx,messageboxr+90,"COMPUTER");
   cx=cx+135;}
   outtextxy(cx,messageboxr+90,"wins !");
   setcolor(15);
   settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
   settextjustify(0,1);
   sprintf(&score2,"%d",bctr);
   setcolor(15);
   outtextxy(535,465,&score2);
   outtextxy(495,43,&score2);}
   
   if (entered<9 1="" bar="" bctr="" boxc="150;" boxcc="175;" boxon="" boxr="125;" boxrr="150;" checkoflag="1;}" checkxflag="0;" count="" cx="" d="" entered="=9){" getch="" if="" int="" letter2="=2){" lettercolor="12;" outtextxy="" player2lastcounter="" player="2;" playername="" playertype2="=2){" pre="" restorebox="" return="" score2="" setcolor="" setfillstyle="" setplayer="" settextjustify="" settextstyle="" sprintf="" textwidth="" while="" winflag="1;">

if (entered==2 || entered==4 || entered==6 || entered==8 || entered==10){
setfillstyle(SOLID_FILL,8);
bar(490,23,635,48);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(0,1);
if (playertype1==1){
setcolor(15);
counter=0;
cx=495;
while (counter


No comments: