使用easyX图形库
安装easyX
运行效果
图片资源
res.h
#pragma once
#include
#include
//行数
#define ROWS 10
//列数
#define COLS 10
//情况
enum state{ road, wall };
//试探方向顺序
enum dir{ p_up, p_right, p_down, p_left };
//试探节点类型
typedef struct pathNode{
int dir; //当前点的当前试探方向
bool isFind; //是否走过
}pathNode;
//点类型
typedef struct MyPoint{
int row, col;
}MyPoint;
#define MYPOINT_SIZE sizeof(struct MyPoint)
myStack.h
#pragma once
#include "res.h"
typedef struct MyStack{
struct MyPoint* pBuff;
int len;
int maxLen;
}MyStack;
//初始化栈
void init(MyStack* s);
//入栈
void push(MyStack* s, MyPoint* data);
//出栈
void pop(struct MyStack* s);
//获取栈顶元素
struct MyPoint* getTop(MyStack* s);
//判断栈是否为空
bool isEmpty(MyStack* s);
myStack.cpp
#include "myStack.h"
#include
#include
//初始化栈
void init(MyStack* s){
if (NULL == s) return;
s->pBuff = NULL;
s->len = s->maxLen = 0;
}
//入栈
void push(MyStack* s, MyPoint* data){
if (NULL == s || NULL == data) return;
if (s->maxLen <= s->len){
s->maxLen = s->maxLen +
(((s->maxLen / 2) > 1) ? (s->maxLen / 2) : 1);
MyPoint* pNew = (MyPoint*)malloc(s->maxLen * MYPOINT_SIZE);
if (s->pBuff){
memcpy(pNew, s->pBuff, s->len * MYPOINT_SIZE);
free(s->pBuff);
}
s->pBuff = pNew;
}
s->pBuff[s->len].col = data->col;
s->pBuff[s->len].row = data->row;
s->len++;
}
//出栈
void pop(MyStack* s){
if (NULL == s) return;
if (s->len > 0) s->len--;
}
//获取栈顶元素
struct MyPoint* getTop(MyStack* s){
if (NULL == s) return NULL;
return s->pBuff + (s->len - 1);
}
//判断栈是否为空
bool isEmpty(MyStack* s){
if (NULL == s) return true;
return (0 == s->len);
}
main.cpp
#include "res.h"
#include "myStack.h"
#include
#define SPACE 50
IMAGE img_pos, img_ren, img_road, img_wall;
void printMap(int map[ROWS][COLS], MyPoint* pos);
void initGame();
void showMap(int map[ROWS][COLS], MyPoint* pos);
int main(){
initGame();
//地图
int map[ROWS][COLS] = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 0, 1, 1, 0, 1, 0, 0, 0, 1 },
{ 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 1, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 1, 1, 0, 1 },
{ 1, 0, 0, 0, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 0, 0, 1, 0, 1, 1 },
{ 1, 0, 1, 0, 0, 1, 1, 0, 0, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
//辅助地图
pathNode pathMap[ROWS][COLS] = { 0 };
//准备一个栈
MyStack stack;
init(&stack);
//起点
MyPoint begPos = { 1, 1 };
//终点
MyPoint endPos = { 8, 8 };
//标记起点走过
pathMap[begPos.row][begPos.col].isFind = true;
//起点入栈
push(&stack, &begPos);
MyPoint currentPos = begPos;
MyPoint searchPos;
bool isFindEnd = false;
while (1){//循环寻路
//找出试探点
searchPos = currentPos;
switch (pathMap[currentPos.row][currentPos.col].dir){
case p_up:
searchPos.row--;
//当前点的当前试探方向改变
pathMap[currentPos.row][currentPos.col].dir = p_right;
//判断能不能走
if (wall != map[searchPos.row][searchPos.col] &&//不是障碍物
//没有走过
true != pathMap[searchPos.row][searchPos.col].isFind){
//能走
//入栈
push(&stack, &searchPos);
//标记走过
pathMap[searchPos.row][searchPos.col].isFind = true;
//走
currentPos = searchPos;
}
break;
case p_right:
searchPos.col++;
//当前点的当前试探方向改变
pathMap[currentPos.row][currentPos.col].dir = p_down;
//判断能不能走
if (wall != map[searchPos.row][searchPos.col] &&//不是障碍物
//没有走过
true != pathMap[searchPos.row][searchPos.col].isFind){
//能走
//入栈
push(&stack, &searchPos);
//标记走过
pathMap[searchPos.row][searchPos.col].isFind = true;
//走
currentPos = searchPos;
}
break;
case p_down:
searchPos.row++;
//当前点的当前试探方向改变
pathMap[currentPos.row][currentPos.col].dir = p_left;
//判断能不能走
if (wall != map[searchPos.row][searchPos.col] &&//不是障碍物
//没有走过
true != pathMap[searchPos.row][searchPos.col].isFind){
//能走
//入栈
push(&stack, &searchPos);
//标记走过
pathMap[searchPos.row][searchPos.col].isFind = true;
//走
currentPos = searchPos;
}
break;
case p_left:
searchPos.col--;
//当前点的当前试探方向改变
pathMap[currentPos.row][currentPos.col].dir = p_right;
//判断能不能走
if (wall != map[searchPos.row][searchPos.col] &&//不是障碍物
//没有走过
true != pathMap[searchPos.row][searchPos.col].isFind){
//能走
//入栈
push(&stack, &searchPos);
//标记走过
pathMap[searchPos.row][searchPos.col].isFind = true;
//走
currentPos = searchPos;
}
else{//不能走
//出栈
pop(&stack);
//跳到当前栈顶元素处
struct MyPoint* temp = getTop(&stack);
currentPos.row = temp->row;
currentPos.col = temp->col;
}
break;
}
Sleep(100);
printMap(map, ¤tPos);
showMap(map, ¤tPos);
//判断是否找到终点
if (endPos.row == currentPos.row &&
endPos.col == currentPos.col){
isFindEnd = true;
break;
}
//判断栈是否为空
if (isEmpty(&stack)) break;
}
int x, y;
//找到终点了
if (isFindEnd){
printf("找到终点了!\n");
//打印路径
printf("path:");
while (!isEmpty(&stack)){
x = getTop(&stack)->col;
y = getTop(&stack)->row;
printf("(%d,%d)", y,x);
putimage(x*SPACE + SPACE / 4,
y*SPACE + SPACE / 4,
&img_pos);
pop(&stack);
}
printf("\n");
}
else{
printf("木有找到终点!\n");
}
while (1);
return 0;
}
void initGame(){
initgraph(COLS*SPACE, ROWS*SPACE, SHOWCONSOLE);
loadimage(&img_ren, L"ren.bmp", SPACE, SPACE, true);
loadimage(&img_road, L"road.bmp", SPACE, SPACE, true);
loadimage(&img_wall , L"wall.bmp", SPACE, SPACE, true);
loadimage(&img_pos, L"pos.bmp", SPACE/2, SPACE/2, true);
}
void printMap(int map[ROWS][COLS], struct MyPoint* pos){
system("cls");
for (int i = 0; i < ROWS; i++){
for (int j = 0; j < COLS; j++){
if (pos->row == i && pos->col == j){
printf("人");
}
else{
if (map[i][j]){
printf("墙");
}
else{
printf(" ");
}
}
}
printf("\n");
}
}
void showMap(int map[ROWS][COLS], struct MyPoint* pos){
for (int i = 0; i < ROWS; i++){
for (int j = 0; j < COLS; j++){
if (pos->row == i && pos->col == j){
putimage(j*SPACE, i*SPACE, &img_ren);
}
else{
if (map[i][j]){
putimage(j*SPACE, i*SPACE, &img_wall);
}
else{
putimage(j*SPACE, i*SPACE, &img_road);
}
}
}
}
}