Instance programming: Maze Pathfinder II

Source: Internet
Author: User
Tags manual int size

A little improvement has been made to the maze of the Pathfinder. The villain does not go backwards in the course of walking, that is, does not repeat through the same point.

/* 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 () starts/*
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 ') {
/* Manual Control * *
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 ();
}/* Manual Control End * *

else{
/* Computer control *
/* Direc indicates the direction of the last move
/* and indicate the direction of the next move * *
* * 0~3, respectively, west, north, East, South * *

direc=2;
i=j=0;k=0;
while (i<m-1j<n-1) {
Switch (DIREC) {
Case 0:
* * Try the 3,0,1,2 order
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--;
&

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.