⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d.c

📁 硬件试验的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
#include<stdio.h>
#include<graphics.h>
#include<time.h>
#include<stdlib.h>
#include<bios.h>
#include<dos.h>
#include<math.h>

#define MODEL_NUM 7
#define LENGTH 18
#define WIDTH 10
#define LEFT  0x4b00        /* move the block leftward */
#define RIGHT 0x4d00        /* move the block rightward */
#define UP    0x4800        /* turn round the block anticlockwise */
#define DOWN  0x5000        /* move the block downward */
#define ESC   0x011b        /* exit the game */
#define USER  0
#define AUTO  1
#define DEATH -1   /*  ai over */
#define LIVE 1

int n[2],*order[2];
int now_x[2],now_y[2];   /* x,y record block center */
int block[2][LENGTH+1][WIDTH+1];      /* value 0==void  1==active 2==done 3==ai temp position*/
int color[2][LENGTH][WIDTH];      /* color number */
int auto_path[10],*auto_p;        /* control ai to move */
int color_tab[MODEL_NUM]={RED,BLUE,GREEN,YELLOW,MAGENTA,LIGHTBLUE,BROWN};
int init_center[MODEL_NUM][2]={ {0,4},{1,4},{1,5},{0,4},{0,5},{0,4},{0,4} };
int block_order[1000];
int auto_state=LIVE;

void initialization(void);      /* draw graph */
int cal_speed(void);            /* compute computer speed,return delay time */
int newblock(int n,int type);   /* error return 0 else return 1;  type-- auto or user  */
void paint(int mode,int type);          /* mode 0 --paint a block ,1-- paint all,2-- clear a block, 4--level,5--score,6--line */
int judge(int n,int type);      /* able return 1  else return 0 */
void move(int n,int type);      /* move,turn round the block */
int downblock(int type);        /* down block,clear line and calculate level,score ,return level*/
void dokey(int key,int type);         /* do the key command */
void end(void);
void ai(int n);         /* first AI for common */
void ai2(int n); /* second AI for special */
void cal_pri(int x,int y,int *priority,int *px,int *py ,int *cir,int round);
    /* calculate priority for first AI */
void cal_pri2(int x,int y,int *priority,int *px,int *py,int *cir,int round);
    /* calculate priority for second AI */
void write_path(int x,int y ,int n);    /* used for ai */
int cal_nline(void);  /* calculate how many line can be clear ,return how many */
void clear(void);  /* clear the temp block in ai */


/*  block type:mode_num  shape  color
  0   I red
  1   rZ*   blue
  2   Z green
  3   L yellow
  4   J magenta
  5   O lightbule
  6   T brown
    *: rZ means reverse Z     */


main()
{  int i,j;
   int level[2]={1,1},time[2]={0,0};
   int del_time;   /*  delay time */
   int graphdriver=DETECT;
   int graphmode;
   initgraph(&graphdriver,&graphmode,"");
   del_time=cal_speed();
   initialization();
   while(1)
 { delay(del_time);
   time[USER]++;
   if(auto_state!=DEATH) time[AUTO]++;
   if(kbhit()) dokey(bioskey(0),USER);
   if(*auto_p!=NULL&&now_x[AUTO]!=0&&auto_state!=DEATH)
  { dokey(*auto_p,AUTO); auto_p++; }
   if(time[USER]==10-level[USER])
  { level[USER]=downblock(USER); time[USER]=0; }
   if(time[AUTO]==10-level[AUTO]&&auto_state!=DEATH)
  { level[AUTO]=downblock(AUTO); time[AUTO]=0; }
 }

}

void initialization(void)
{  int i,j;
   char a[]="Level",b[]="Score",c[]="Line";
   char temp[2];

   settextstyle(TRIPLEX_FONT,HORIZ_DIR,6);
   outtextxy(120,30,"Russian Block");
   setlinestyle(SOLID_LINE,0,THICK_WIDTH);
   rectangle(78,98,282,462);
   rectangle(378,98,582,462);
   settextstyle(SMALL_FONT,HORIZ_DIR,0);
   outtextxy(30,140,a);
   outtextxy(30,200,b);
   outtextxy(30,260,c);
   outtextxy(330,140,a);
   outtextxy(330,200,b);
   outtextxy(330,260,c);
   setviewport(0,0,640,480,0);
   settextjustify(RIGHT_TEXT,BOTTOM_TEXT);
   sprintf(temp,"%d",0);
   outtextxy(60,220,temp);
   outtextxy(60,280,temp);
   outtextxy(360,220,temp);
   outtextxy(360,280,temp);
   sprintf(temp,"%d",1);
   outtextxy(60,160,temp);
   outtextxy(360,160,temp);
   randomize();
   for(j=0;j<10;j++)
      { block[0][18][j]=2;
 block[1][18][j]=2;
      }
   for(i=0;i<18;i++)
      { block[0][i][10]=2;
 block[1][i][10]=2;
      }
   for(i=0;i<1000;i++)
       block_order[i]=random(MODEL_NUM);

   auto_p=auto_path;
   setviewport(80,100,280,460,0);
   clearviewport();
   order[USER]=block_order;
   n[USER]=*(order[USER])++;
   newblock(n[USER],USER);
   order[AUTO]=block_order;
   n[AUTO]=*(order[AUTO])++;
   newblock(n[AUTO],AUTO);
   now_x[USER]=now_x[AUTO]=init_center[n[USER]][0];
   now_y[USER]=now_y[AUTO]=init_center[n[USER]][1];
}

int cal_speed(void)
{  long i,time1,time2,d_time;
   time1=biostime(0,0);
   for(i=0;i<100000000;i++);
   time2=biostime(0,0);
   d_time=time2-time1;
/* printf("speed: %f,%ld,%ld,%ld \n",100000000/d_time*18.2,d_time,time1,time2);
*/
   return 10000/d_time*18*2;
}


int newblock(int n,int type)
{  int i,j,k;
   int model[MODEL_NUM][8] = {
 { 0, 3, 0, 4, 0, 5, 0, 6 },
 { 0, 4, 1, 4, 1, 5, 2, 5 },
 { 0, 5, 1, 4, 1, 5, 2, 4 },
 { 0, 3, 0, 4, 0, 5, 1, 3 },
 { 0, 4, 0, 5, 0, 6, 1, 6 },
 { 0, 4, 0, 5, 1, 4, 1, 5 },
 { 0, 3, 0, 4, 0, 5, 1, 4 },
    };
   int *p;
   p=model[n];
   now_x[type]=init_center[n][0];
   now_y[type]=init_center[n][1];
   if(type==AUTO&&auto_state!=DEATH) ai(n);
   for(k=0;k<4;k++)
     { i=*p++;
       j=*p++;
       if(block[type][i][j]!=2)
   { block[type][i][j]=1;
     color[type][i][j]=color_tab[n];
   }
       else
   { paint(0,type); return 0; }
     }
   paint(0,type);
   return 1;
}

void paint(int mode,int type)
{  int i,j;
   setviewport(type*300+80,100,type*300+280,460,0);
   if(mode==0)
 { for(i=0;i<LENGTH;i++)
      for(j=0;j<WIDTH;j++)
  if(block[type][i][j]==1)
     { setfillstyle(SOLID_FILL,color[type][i][j]);
       bar(20*j+1,20*i+1,20*(j+1)-1,20*(i+1)-1);
     }
 }
   if(mode==1)
      { clearviewport();
 for(i=0;i<LENGTH;i++)
    for(j=0;j<WIDTH;j++)
      if( block[type][i][j]==0) continue;
      else
  { setfillstyle(SOLID_FILL,color[type][i][j]);
    bar(20*j+1,20*i+1,20*(j+1)-1,20*(i+1)-1);
  }
       }
    if(mode==2)
 { for(i=0;i<LENGTH;i++)
      for(j=0;j<WIDTH;j++)
  if(block[type][i][j]==1)
     { setviewport(20*j+type*300+80,20*i+100,20*(j+1)+type*300+80,20*(i+1)+100,0);
       clearviewport();
     }
 }
}


int judge(int n,int type)
{  int i,j,x,y;
   x=now_x[type];
   y=now_y[type];
   if(x==0&&n<10) return 0;     /* when x==0,it can move left right and down but can't rotate */
   switch (n)
     {  case LEFT : if(y==0) return 0;
      for(i=x-1;i<=x+2;i++)
         { if(i<0) continue;
    for(j=y-1;j<y+2;j++)
      if(block[type][i][j]==1&&(block[type][i][j-1]==2||j==0))
        return 0;
         }
      break;
 case RIGHT: if(y==WIDTH-1) return 0;
      for(i=x-1;i<=x+2;i++)
   { if(i<0) continue;
     for(j=y+2;j>=y-1;j--)
        if(block[type][i][j]==1&&(block[type][i][j+1]==2||j==WIDTH-1))
          return 0;
   }
      break;
 case DOWN : for(i=0;i<LENGTH;i++)
   for(j=0;j<WIDTH;j++)
       if(block[type][i][j]==1&&(block[type][i+1][j]==2||i==LENGTH-1))
     return 0;
      break;
 case 0 :    if(x==0||y==0||y==WIDTH-1||y==WIDTH-2) return 0;
      if(block[type][x+1][y]==1)
         if(block[type][x][y-1]==2||block[type][x][y+1]==2||block[type][x][y+2]==2)
      return 0;
      else
         if(block[type][x-1][y]==2||block[type][x+1][y]==2||block[type][x+2][y]==2)
      return 0;
      break;
 case 1 :    if(y==0) return 0;
      if(block[type][x][y+1]==1)
         if(block[type][x][y-1]==2||block[type][x-1][y+1]==2)
      return 0;
      else
         if(block[type][x+1][y+1]==2||block[type][x][y+1]==2)
      return 0;
      break;
 case 2 :    if(y==WIDTH-1) return 0;
      if(block[type][x][y-1]==1)
         if(block[type][x-1][y-1]==2||block[type][x][y+1]==2)
      return 0;
      else
         if(block[type][x][y-1]==2||block[type][x+1][y-1]==2)
      return 0;
      break;
 case 3 :    if(y==0||y==WIDTH-1) return 0;
      if(block[type][x+1][y-1]==1)
   if(block[type][x+1][y]==2||block[type][x+1][y+1]==2||block[type][x-1][y]==2)
      return 0;
      if(block[type][x+1][y+1]==1)
   if(block[type][x][y-1]==2||block[type][x][y+1]==2||block[type][x-1][y+1]==2)
      return 0;
      if(block[type][x-1][y+1]==1)
   if(block[type][x+1][y]==2||block[type][x-1][y-1]==2||block[type][x-1][y]==2)
      return 0;
      if(block[type][x-1][y-1]==1)
   if(block[type][x][y-1]==2||block[type][x][y+1]==2||block[type][x+1][y-1]==2)
      return 0;
      break;
 case 4 :    if(y==0||y==WIDTH-1) return 0;
      if(block[type][x+1][y+1]==1)
   if(block[type][x+1][y]==2||block[type][x-1][y+1]==2||block[type][x-1][y]==2)
      return 0;
      if(block[type][x-1][y+1]==1)
   if(block[type][x][y-1]==2||block[type][x][y+1]==2||block[type][x-1][y-1]==2)
      return 0;
      if(block[type][x-1][y-1]==1)
   if(block[type][x+1][y]==2||block[type][x+1][y-1]==2||block[type][x-1][y]==2)
      return 0;
      if(block[type][x+1][y-1]==1)
   if(block[type][x][y-1]==2||block[type][x][y+1]==2||block[type][x+1][y+1]==2)
      return 0;
      break;
 case 5 :    break;
 case 6 :    if(y==0||y==WIDTH-1) return 0;
      if(block[type][x][y-1]!=1&&block[type][x][y-1]==2)
      return 0;
      if(block[type][x-1][y]!=1&&block[type][x-1][y]==2)
      return 0;
      if(block[type][x][y+1]!=1&&block[type][x][y+1]==2)
      return 0;
      if(block[type][x+1][y]!=1&&block[type][x+1][y]==2)
      return 0;
      break;
     }
   return 1;
}

void move(int n,int type)
{  int i,j,x,y;
   x=now_x[type];
   y=now_y[type];
   switch (n)
     {  case LEFT : for(i=x-1;i<=x+2;i++)
   for(j=y-1;j<=y+2;j++)
      if(block[type][i][j]==1)
        { block[type][i][j-1]=1;
          block[type][i][j]=0;
          color[type][i][j-1]=color[type][i][j];
          color[type][i][j]=BLACK;
        }
      break;
 case RIGHT: for(i=x-1;i<=x+2;i++)
   for(j=y+2;j>=y-1;j--)
       if(block[type][i][j]==1)
         { block[type][i][j+1]=1;
    block[type][i][j]=0;
    color[type][i][j+1]=color[type][i][j];
    color[type][i][j]=BLACK;
          }
      break;
 case DOWN : while(judge(DOWN,type))
   { for(i=x+2;i>=x-1;i--)
        for(j=y+2;j>=y-1;j--)
     if(block[type][i][j]==1)
         {  block[type][i+1][j]=1;
     block[type][i][j]=0;
     color[type][i+1][j]=color[type][i][j];
     color[type][i][j]=BLACK;
         }
      now_x[type]++;
      x++;
   }
      break;
 case 0 :    if(block[type][x+1][y]==1)
         { block[type][x][y-1]=block[type][x][y+1]=block[type][x][y+2]=1;
    block[type][x-1][y]=block[type][x+1][y]=block[type][x+2][y]=0;
    color[type][x][y-1]=color[type][x][y+1]=color[type][x][y+2]=color[type][x][y];
    color[type][x-1][y]=color[type][x+1][y]=color[type][x+2][y]=BLACK;
         }
      else
         { block[type][x-1][y]=block[type][x+1][y]=block[type][x+2][y]=1;
    block[type][x][y-1]=block[type][x][y+1]=block[type][x][y+2]=0;
    color[type][x-1][y]=color[type][x+1][y]=color[type][x+2][y]=color[type][x][y];
    color[type][x][y-1]=color[type][x][y+1]=color[type][x][y+2]=BLACK;
         }
      break;
 case 1 :    if(block[type][x][y+1]==1)
         { block[type][x][y-1]=block[type][x-1][y+1]=1;
    block[type][x][y+1]=block[type][x+1][y+1]=0;
    color[type][x][y-1]=color[type][x-1][y+1]=color[type][x][y];
    color[type][x][y+1]=color[type][x+1][y+1]=BLACK;
         }
      else
         { block[type][x][y+1]=block[type][x+1][y+1]=1;
    block[type][x][y-1]=block[type][x-1][y+1]=0;
    color[type][x][y+1]=color[type][x+1][y+1]=color[type][x][y];
    color[type][x][y-1]=color[type][x-1][y+1]=BLACK;
         }
      break;
 case 2 :    if(block[type][x][y-1]==1)
         { block[type][x][y+1]=block[type][x-1][y-1]=1;
    block[type][x][y-1]=block[type][x+1][y-1]=0;
    color[type][x][y+1]=color[type][x-1][y-1]=color[type][x][y];
    color[type][x][y-1]=color[type][x+1][y-1]=BLACK;
         }
      else
         { block[type][x][y-1]=block[type][x+1][y-1]=1;
    block[type][x][y+1]=block[type][x-1][y-1]=0;
    color[type][x][y-1]=color[type][x+1][y-1]=color[type][x][y];
    color[type][x][y+1]=color[type][x-1][y-1]=BLACK;
         }
      break;
 case 3 :    if(block[type][x+1][y-1]==1)
   { block[type][x+1][y]=block[type][x+1][y+1]=block[type][x-1][y]=1;
     block[type][x][y-1]=block[type][x][y+1]=block[type][x+1][y-1]=0;
     color[type][x+1][y]=color[type][x+1][y+1]=color[type][x-1][y]=color[type][x][y];
     color[type][x][y-1]=color[type][x][y+1]=color[type][x+1][y-1]=BLACK;
   }
      else if(block[type][x+1][y+1]==1)
   { block[type][x][y-1]=block[type][x][y+1]=block[type][x-1][y+1]=1;
     block[type][x+1][y]=block[type][x+1][y+1]=block[type][x-1][y]=0;
     color[type][x][y-1]=color[type][x][y+1]=color[type][x-1][y+1]=color[type][x][y];
     color[type][x+1][y]=color[type][x+1][y+1]=color[type][x-1][y]=BLACK;
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -