Friday, June 21, 2013

Tic Tac Toe clone in Turbo C

/*

    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;
   }
  }
 }
}

Fruits in the basket Turbo C game

/*

    Fruits in the basket is a fun and addicting game. Catch falling

    fruits and score points.

    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>
#include<math.h>
#define LEFT 75
#define RIGHT 77
#define UP 72
#define DOWN 80
#define ENTER 13

int r=20, coorx[9], x, midx, midy, cellspc=50;

int score=0, miss=0, speed=10;

char cha[8];

void drawboard(struct basket );
void drawfruit(struct fruit * );

int fruity;

struct fruit {
 int x, y;
};

struct basket {
 int x, y, rad;
};

int main() {
 int gd=DETECT, gm, i;
 char ch;
 struct basket one;
 struct fruit f1;
 randomize();
 initgraph(&gd, &gm, "C:\\TC\\BGI");
 midx=getmaxx()/2;
 midy=getmaxy()/2;
 one.y=getmaxy()-40;
 coorx[0]=170-100;
 coorx[1]=220-100;
 coorx[2]=270-100;
 coorx[3]=320-100;
 coorx[4]=370-100;
 coorx[5]=420-100;
 coorx[6]=470-100;
 one.x=coorx[random(7)];
 one.rad=20;
 f1.x=coorx[random(7)];
 f1.y=0;
 settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);
 outtext("Welcome to ");
 setcolor(MAGENTA);
 outtext("F");
 setcolor(GREEN);
 outtext("r");
 setcolor(RED);
 outtext("u");
 setcolor(YELLOW);
 outtext("i");
 setcolor(CYAN);
 outtext("t");
 setcolor(BROWN);
 outtext("s");
 setcolor(WHITE);
 outtext(" in the basket.");
 outtextxy(0,40,"Use \'a\' & \'d\' or the arrow keys to move");
 outtextxy(0,80,"the basket.");
 outtextxy(0,120,"Press \'q\' to quit anytime.");
 getch();

 while(1) {
  if(kbhit()) {
   ch=getch();
   switch(ch) {
    case 72:
     //up arrow pressed

     break;

    case 75:
     //left arrow pressed
     one.x-=cellspc;
     if(one.x<coorx[0])
      one.x+=cellspc;
     break;
    case 77:
     //right arrow pressed
     one.x+=cellspc;
     if(one.x>coorx[6])
      one.x-=cellspc;
     break;
    case 80:
     //down arrow pressed

     break;


case 'a':

     //'a' = left arrow pressed


one.x-=cellspc;
     if(one.x<coorx[0])
      one.x+=cellspc;
     break;
    case 'd':

     //'d' = right arrow pressed


one.x+=cellspc;
     if(one.x>coorx[6])
      one.x-=cellspc;
     break;
    case '\r':
     goto end;
    case 'q':
     goto end;
    default:
     break;
   }
  }
  delay(50);
  drawboard(one);
  drawfruit(&f1);

  if(((f1.y+50)>=one.y)&&(f1.x==one.x)) {
   ++score;
   f1.x=coorx[random(7)];
   f1.y=0;
  }
  if(f1.y>one.y) {
   ++miss;
   f1.x=coorx[random(7)];
   f1.y=0;
  }
  speed=0.2*score+10;
  if(speed<10)
   speed=10;
  if(speed>30)
   speed=30;
  if(miss>10) {
   end:
   clearviewport();
   setcolor(WHITE);
   itoa(score,cha,10);
   outtextxy(2,10,"Your score is:");
   setcolor(GREEN);
   outtextxy(2,40,cha);
   getch();
   return 0;
  }
 }
}

void drawboard(struct basket one) {
 char st[8];
 clearviewport();
 setcolor(WHITE);
 line(440,0,440,480);
 itoa(miss,st,10);
 outtextxy(450,20,"Your misses are:");
 setcolor(RED);
 outtextxy(450,50,st);
 itoa(score,st,10);
 setcolor(WHITE);
 outtextxy(450,80,"Your score is:");
 setcolor(GREEN);
 outtextxy(450,110,st);
 setcolor(YELLOW);
 setfillstyle(XHATCH_FILL, YELLOW);
 pieslice(one.x, one.y, 180, 360, one.rad);
 setcolor(WHITE);
 itoa(one.x,st,10);
 outtextxy(550,130,"x=");
 outtextxy(565,130,st);
 itoa(one.y,st,10);
 outtextxy(550,150,"y=");
 outtextxy(565,150,st);
}

void drawfruit(struct fruit *f) {
 char st[8];
 setcolor(RED);
 setfillstyle(SOLID_FILL, RED);
 pieslice(f->x, f->y, 0, 360, r-2);
 f->y+=speed;
 setcolor(WHITE);
 itoa(f->x,st,10);
 outtextxy(450,130,"x=");
 outtextxy(465,130,st);
 itoa(f->y,st,10);
 outtextxy(450,150,"y=");
 outtextxy(465,150,st);

}