实例编程:迷宫探路II
当前位置:以往代写 > C/C++ 教程 >实例编程:迷宫探路II
2019-06-13

实例编程:迷宫探路II

实例编程:迷宫探路II

对《迷宫探路》做了一点改造。小人在行走进程中不走转头路,即不反复颠末同一点。

/* crazymaze.c*/
/* 2003-8-26 */
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <graphics.h>
#define N 22
#define M 22
#define MAXLEN 200;
int bg[M][N];
struct square{
    int x;
    int y;
    int direc;
}p[200];
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,x0=0,y0=0;
int i=0,j=0,k=0,count=0;
int gdriver=DETECT,gmode;
char ch;
int direc;
makebg(M,N);
/*  registerbgidriver(EGAVGA_driver);
 initgraph(&gdriver,&gmode,"c:\turboc2");*/
initgraph(&gdriver,&gmode,"c:\tc20\bgi");
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(GREEN);
outtextxy(60,120,"PRESS KEY <1> :YOU ,");
outtextxy(70,150,"OTHER KEY :AUTOMATIC");
setcolor(WH99vE);
x+=len;y+=len;
drawman(x,y,len);
x0=x;y0=y;
if((ch=getch())=='1'){
/* 人工节制 */
while((ch=getch())!='q'){
  delay(800);
  drawman(x,y,len);
  switch(ch){
    case 'a':
        if(j>0&&bg[i][j-1]==0){
            if(x>step){x-=step;j--;};
        }
        break;
    case 's':
        if(i<M-1&&bg[i+1][j]==0){
            if(y<479-step){y+=step;i++;};
        }
        break;
    case 'd':
        if(j<N-1&&bg[i][j+1]==0){
            if(x<639-step){x+=step;j++;}
        }
        break;
    case 'w':
        if(i>0&&bg[i-1][j]==0){
            if(y>step){y-=step;i--;}
        }
        break;
    default :break;
  }
  drawman(x,y,len);
 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;k=0;
while(i<M-1j<N-1){
    switch(direc){
    case 0:
        /* 以3,0,1,2的序次实验 */
        if(i<M-1&&bg[i+1][j]==0){
            y+=step;i++;
            direc=3;
        }
        else if(j>0&&bg[i][j-1]==0){
            x-=step;j--;
            direc=0;
        }
        else if(i>0&&bg[i-1][j]==0){
            y-=step;i--;
            direc=1;
        }
        else {
            x+=step;j++;
            direc=2;
        }
        p[k].x=x;
        p[k].y=y;
        p[k].direc=direc;
        if(k>0){
            count=k-1;
            while(count>=0
            &&(p[count].x!=p[k].xp[count].y!=p[k].y))
            count--;
            if(count>=0){
                k=count;
                p[k].direc=direc;
                i=(p[k].y-len)/step;
                j=(p[k].x-len)/step;
            }
        }
        k++;
        break;
    case 1:
        if(j>0&&bg[i][j-1]==0){
            x-=step;j--;
            direc=0;
        }
        else if(i>0&&bg[i-1][j]==0){
            y-=step;i--;
            direc=1;
        }
        else if(j<N-1&&bg[i][j+1]==0){
            x+=step;j++;
            direc=2;
        }
        else{
            y+=step;i++;
            direc=3;
        }
        p[k].x=x;
        p[k].y=y;
        p[k].direc=direc;
        if(k>0){
            count=k-1;
            while(count>=0
            &&(p[count].x!=p[k].xp[count].y!=p[k].y))
            count--;
       &

    关键字:

在线提交作业