File: MAIN.C | Size: 6,703 bytes | Download file | Back to directory listing | BWPOW's homepage
#include <allegro.h>
#include <tdgui.h>
#include "data.h"
 
char pole[19][19];
 
// vnutorne premenne
DATAFILE *data;
 
int white,red,blue,gray;
 
char chess;
char wx,wy,tah,jx,jy;
 
typedef struct BOX{
  int y;
  char text[30];
  char p;
}BOX;
 
BOX box[]={
{10,"HUMAN STARTS"},
{60,"COMPUTER STARTS"},
{150,"UNDO LAST MOVE"},
{200,"ABOUT"},
{270,"SODA IQ",1},
{320,"C'ANDY"},
//{370,"SODA POWER"},
{430,"EXIT"},
{NULL}
};
 
void napis_about(void)
{
 
 
 
 
 
}
 
void draw_s_gui(BITMAP *bmp)
{
  int i;
  for(i=0;box[i].y;i++){
    if(box[i].p==0){
      draw_sprite(bmp,data[button].dat,485,box[i].y);
      textout_centre(bmp,font,box[i].text,558,box[i].y+18,0);
    }
    else{
      draw_sprite(bmp,data[pbutton].dat,485,box[i].y);
      textout_centre(bmp,font,box[i].text,558,box[i].y+18,white);
    }
  }
}
 
int s_gui(int x,int y){
  int i;
  for(i=0;box[i].y;i++){
    if(x>=485&&x<=630&&
       y>=box[i].y&&y<=box[i].y+43) return i;
  }
  return -1;
}
//145,558
 
char line_x,line_y,line_o;
 
int _cfc_l(int xx,int yy)
{
  char i,x,y,l,p,pp;
  char op[8]={1,-1,1,0,1,1,0,1};
 
  p=pole[xx][yy];
  if(p==0) return 0;
 
  line_x=xx;
  line_y=yy;
 
  for(i=0;i<4;i++){
    x=xx;y=yy;l=0;pp=p;
    while(pp==p){
      x+=op[i*2];
      y+=op[i*2+1];
      if(x>=0&&y>=0&&x<19&&y<19){
        pp=pole[x][y];
        l++;
        if(l==5){
          line_o=i;
          return 1;
        }
      }
      else break;
    }
  }
  return 0;
}
 
void check_for_chess()
{
  char x,y;
  for(x=0;x<19;x++)
    for(y=0;y<19;y++){
       if(_cfc_l(x,y)==1){
         chess=pole[x][y]+2;
         return;
       }
    }
}
 
void chyba(char text[50])
{
  allegro_exit();
  printf("%s\n",text);
  exit(1);
}
 
void koniec()
{
  allegro_exit();
 
  printf("*** PisqWork 1.1 ***\n");
  printf("SODA IQ inside\n");
  printf("By Samuel Kupka, bwpow@ideaz.sk\n");
 
  exit(0);
}
 
void generate_pole(BITMAP *bmp)
{
  char x,y;
 
  rectfill(bmp,0,0,475,475,0);
 
  for(x=0;x<20;x++) vline(bmp,x*25,0,475,white);
  for(y=0;y<20;y++) hline(bmp,0,y*25,475,white);
}
 
void new_game(BITMAP *bmp)
{
  char x,y;
  memset(pole,0,361);
  chess=0;
  generate_pole(bmp);
}
 
 
void undo()
{
  if(chess) return;
  pole[wx][wy]=0;
  pole[jx][jy]=0;
}
 
void kresli_ciaru(BITMAP *bmp,int c)
{
  char op[8]={1,-1,1,0,1,1,0,1};
  char i,x,y;
 
  x=line_x;
  y=line_y;
 
  for(i=0;i<5;i++){
    if(line_o==0){
      line(bmp,x*25   ,y*25+24,
               x*25+24,y*25   ,c);
    }
    if(line_o==1){
      line(bmp,x*25   ,y*25+11,
               x*25+24,y*25+11,c);
    }
    if(line_o==2){
      line(bmp,x*25   ,y*25   ,
               x*25+24,y*25+24,c);
    }
    if(line_o==3){
      line(bmp,x*25+11,y*25   ,
               x*25+11,y*25+24,c);
    }
 
 
    x+=op[line_o*2];
    y+=op[line_o*2+1];
  }
}
 
int main(void)
{
  BITMAP *bmp;
  char obt=0;
  char lx=-1,ly=-1;
  char c=1,x,y,k=0,r,i;
  char now=0,comp=1;
  int col;
 
  allegro_init();
  bill_init();
 
  install_keyboard();
  install_timer();
  if(install_mouse()==-1)
    chyba("Mouse driver not loaded!");
 
  set_color_depth(16);
 
  if(set_gfx_mode(0,640,480,0,0)<0)
    chyba("Error setting gfx mode 640x480 16bit!");
  set_gfx_mode(0,640,480,0,0);
 
  bmp=create_bitmap(640,480);
  if(bmp==0) chyba("Not enough memory!");
 
  white=makecol(255,255,255);
  blue=makecol(0,0,255);
  red=makecol(255,0,0);
  gray=makecol(100,200,100);
 
  packfile_password("Kards");
  data=load_datafile("pisqwork.dat");
 
  clear(bmp);
  generate_pole(bmp);
 
  blit(data[sidebar].dat,bmp,0,0,477,0,163,480);
  text_mode(-1);
 
  draw_s_gui(bmp);
 
  c=0;
  blit(bmp,screen,0,0,0,0,640,480);
  show_mouse(screen);
 
  while(k!=1)
  {
    if(c==1){
 
      for(x=0;x<19;x++)
        for(y=0;y<19;y++){
          rectfill(bmp,x*25+1,y*25+1,x*25+23,y*25+23,0);
          if(pole[x][y]!=0){
 
            if(x==wx&&y==wy){
              rectfill(bmp,x*25,y*25,x*25+24,y*25+24,gray);
              tdbox(bmp,wx*25,wy*25,24,24,2);
 
              if(pole[wx][wy]==1){
                line(bmp,wx*25+4,wy*25+4,wx*25+22,wy*25+22,red);
                line(bmp,wx*25+22,wy*25+4,wx*25+4,wy*25+22,red);
              }
              else
                circle(bmp,wx*25+12,wy*25+12,9,blue);
 
            }
            else{
              tdbox(bmp,x*25,y*25,24,24,6);
              if(pole[x][y]==1){
                line(bmp,x*25+3,y*25+3,x*25+21,y*25+21,red);
                line(bmp,x*25+21,y*25+3,x*25+3,y*25+21,red);
              }
              else
                circle(bmp,x*25+11,y*25+11,9,blue);
            }
          }
      }
 
      c=0;
 
      lx=wx;ly=wy;
 
      check_for_chess();
 
      if(chess>0){
        if(chess==3) col=makecol(255,255,0);
        else col=blue;
 
        kresli_ciaru(bmp,col);
      }
 
      show_mouse(0);
      blit(bmp,screen,0,0,0,0,640,480);
//      save_pcx("pisq.pcx",bmp,0);
      show_mouse(screen);
 
      while(mouse_b);
 
    }
 
    if(chess==0&&(mouse_b&1)&&now!=comp&&mouse_x<475&&mouse_y<475){
      if(pole[mouse_x/25][mouse_y/25]==0){
        c=1;
        pole[mouse_x/25][mouse_y/25]=1;
        wx=mouse_x/25;
        wy=mouse_y/25;
        jx=wx;
        jy=wy;
        now=!now;
      }
    }
 
    if((mouse_b&1)&&mouse_x>475){
      r=s_gui(mouse_x,mouse_y);
      if(r>=0){
        for(i=0;i<8;i++) if(i!=4&&i!=5&&i!=6) box[i].p=0;
        box[r].p=1;
      }
 
      if(r==0){new_game(bmp);c=1;now=0;chess=0;}
      if(r==1){new_game(bmp);c=0;now=1;chess=0;}
      if(r==2&&chess==0){undo();c=1;now=0;}
      if(r==3){
        show_mouse(0);
        napis_about();
        show_mouse(screen);
        c=1;
      }
      if(r==4){
        obt=0;
        box[4].p=1;
        box[5].p=0;
        box[6].p=0;
 
        draw_s_gui(bmp);
        c=1;
      }
 
      if(r==5){
        obt=1;
        box[4].p=0;
        box[5].p=1;
        box[6].p=0;
 
        draw_s_gui(bmp);
        c=1;
      }
/*
 
      if(r==6){
        obt=2;
        box[4].p=0;
        box[5].p=0;
        box[6].p=1;
 
        draw_s_gui(bmp);
        c=1;
      }
*/
 
      if(r==6) k=1;
 
    }
 
    if(chess==0&&now==comp&&c==0){
      now=!now;
 
      if(obt==0) ai_soda(&wx,&wy);
      if(obt==1) ai_candy(&wx,&wy);
 
      if(wx<0) wx=0;
      if(wy<0) wy=0;
      if(wx>=19) wx=0;
      if(wy>=19) wy=0;
      if(pole[wx][wy]==0) pole[wx][wy]=2;
      c=1;
    }
 
    if(key[KEY_ESC]) k=1;
  }
 
  koniec();
  return 0;
}