【 tulaoshi.com - 编程语言 】
                             
                                曾经听说过一个走迷宫的诀窍:顺着墙沿一侧走。
  (一直沿左侧或一直沿右侧)。本程序实现了这一
  思想,小人一直沿左侧走。
      迷宫是随机生成的。
      开始时,按数字 1 键进入人工控制模式;按w,s,a,d分别代表上,下,左,右方向。
      开始时,按除数字 1 以外的任意键进入自动模式;
  小人由电脑控制。    按 Q键结束程序。 /*
    Name:    maze.c
    Author:      zhuqing
    Description:     迷宫探险  Date: 28-08-03 10:15
    Copyright: 
  */
  #include stdlib.h
  #include time.h
  #include math.h
  #include stdio.h
  #include graphics.h
  #define N 22
  #define M 22
  int bg[M][N];void makebg(int,int);
  void drawbg(int[][],int,int,int,int,int);
  void drawman(int,int,int);
  void rect(int,int,int,int);void main(){/* main()开始 */
  int step=20;
  int len=10;
  int size=20;
  int x=0,y=0;
  int i=0,j=0;
  int gdriver=DETECT,gmode;
  char ch;
  int direc;
  makebg(M,N);
  /*  registerbgidriver(EGAVGA_driver);*/
  /* initgraph(&gdriver,&gmode,"c:urboc2"); */initgraph(&gdriver,&gmode,"c:c20gi");
  cleardevice();
  setwritemode(XOR_PUT);
  settextstyle(1,0,3);
  setcolor(GREEN);
  outtextxy(100,180,"Press Q to quit");
  setcolor(BLUE);
  setfillstyle(LINE_FILL,BLUE);drawbg(bg,M,N,size,0,0);
  setcolor(WH99vE);
  x+=len;y+=len;
  drawman(x,y,len);
  setcolor(GREEN);
  outtextxy(60,120,"PRESS KEY 1 :YOU ,");
  outtextxy(70,150,"OTHER KEY :AUTOMATIC");
  setcolor(WH99vE);
  if((ch=getch())=='1'){
  /* 人工控制 */while((ch=getch())!='q'){
    drawman(x,y,len);
    switch(ch){
      case 'a':
          if(j0&&bg[i][j-1]==0){
              if(xstep){x-=step;j--;};
          }
          break;
      case 's':
          if(iM-1&&bg[i+1][j]==0){
              if(y479-step){y+=step;i++;};
          }
          break;
      case 'd':
          if(jN-1&&bg[i][j+1]==0){
              if(x639-step){x+=step;j++;}
          }
          break;
      case 'w':
          if(i0&&bg[i-1][j]==0){
              if(ystep){y-=step;i--;}
          }
          break;
      default :break;
    }
    drawman(x,y,len);
   delay(800);
   if(i=M-1&&j=N-1){
      settextstyle(4,0,3);
      setcolor(RED);
      outtextxy(150,260,"YOU WIN!");
    }
    setcolor(WH99vE);
  }
   closegraph();
  }/* 人工控制结束 */     
  else{
  /* 电脑控制 */
  /* direc表示上一步运动方向 */
  /* 并表示下一步运动方向 */
  /* 0~3分别表示 西、北、东、南 */direc=2;
  i=j=0;
  while(iM-1jN-1){
      delay(80000);
      drawman(x,y,len);
      switch(direc){
      case 0:
          /* 以3,0,1的次序尝试 */ 
          if(iM-1&&bg[i+1][j]==0){
              y+=step;i++;
              direc=3; 
          }    
          else if(j0&&bg[i][j-1]==0){
              x-=step;j--;
              direc=0;            
          }
          else if(i0&&bg[i-1][j]==0){
              y-=step;i--;
              direc=1;            
          }
          else {
              x+=step;j++;
              direc=2;            
          }              
          break;
      case 1:
          if(j0&&bg[i][j-1]==0){
              x-=step;j--;
              direc=0;            
          }
          else if(i0&&bg[i-1][j]==0){
              y-=step;i--;
              direc=1;            
          }
          else if(jN-1&&bg[i][j+1]==0){
              x+=step;j++;
              direc=2;            
          }
          else{
              y+=step;i++;
              direc=3; 
          }                      
          break;
      case 2:
          if(i0&&bg[i-1][j]==0){
              y-=step;i--;
              direc=1;  
          }    
          else if(jN-1&&bg[i][j+1]==0){
              x+=step;j++;
              direc=2;            
          }
          else if(iM-1&&bg[i+1][j]==0){
              y+=step;i++;
              direc=3