flying-egg 迷宫的实现
话不多说
1.定义一个二维数组
2.数组为0的时候为空,1的时候打印墙
用户自己实现迷宫,自己放置墙和空的位置
3.设置好之后,寻找路径,由于自己的思想比较笨,所以寻找的路径虽然是成功的,但是有点难以理解
4.头文件
#include<stdio.h>
#include <windows.h>
#include<conio.h>
5.全局变量
static int start_x = 0;//记录的是起点横坐标
static int start_y = 0;//记录起点纵坐标
static int end_x = 0;//记录终点横坐标
static int end_y = 0;// 记录终点纵坐标
static int start_Home = 0;//当已经有起点的时候,start_Home=1
static int end_Home = 0;//已经有终点的时候,end_Home=1
typedef struct
{
int row;
int col;
}Pos;//坐标结构体
void CursorJump(int x, int y)
{
COORD pos; //定义光标位置的结构体变量
pos.X = x;
pos.Y = y;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); //获取控制台句柄
SetConsoleCursorPosition(handle, pos); //设置光标位置
}
由于设置了,需要用到光标的函数,所以,定义光标
typedef enum
{
cRED = FOREGROUND_RED,
cBLUE = FOREGROUND_BLUE,
cGREEN = FOREGROUND_GREEN,
cYELLOW = FOREGROUND_RED | FOREGROUND_GREEN,
cWHITE = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN
} CONSOLE_COLOR;
void SetConsoleTextColor(CONSOLE_COLOR color)//设置颜色的函数,头文件windows.h
{
static HANDLE console = NULL;
if (console == NULL) console = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console, color);
}
设置颜色;
void menu()
{
printf("*********************n");
printf("*******1.play********n");
printf("******2.设置起点*******n");
printf("******3.设置终点*******n");
printf("***4.显示当前样式****n");
printf("*******5.清除********n");
printf("*******6.显示足迹********n");
printf("*******0.exit********n");
printf("*********************n");
}
开始界面,打印菜单
1.初始化迷宫,在这之前需要输入的是自己想要的迷宫的大小
2和3的功能差不多,因为调用的是同一个函数
4.设置好之后,在查看当前的迷宫
5,将迷宫清除,但是大小不会初始化
6.打印足迹
0.退出
初始化迷宫:如下
int* Initmaze(int(*array)[20], int x, int y)//初始化迷宫
{
system("cls");
SetConsoleTextColor(cRED);
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
if (j == 0 || j == x - 1)
{
array[i][j] = WALL; //标记该位置为墙
CursorJump(2 * j, i);//将光标移动到这个位置
printf("■");
}
else if (i == 0 || i == y - 1)
{
array[i][j] = WALL; //标记该位置为墙
printf("■");
}
else
{
array[i][j] = KONG; //标记该位置为空
}
}
}
return array[20];
}
展示当前的迷宫的样子:
int* Showmaze(int(*array)[20], int x, int y)//显示迷宫
{
system("cls");
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
if (array[i][j] == WALL)
{
SetConsoleTextColor(cRED);//显示墙
CursorJump(2 * j, i);
printf("■");
}
}
}
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
if (array[i][j] == KONG)
{
if (i == start_x && j == start_y && start_Home != 0)//显示起点
{
SetConsoleTextColor(cYELLOW);
CursorJump(2 * j, i);
printf("●");
}
}
}
}
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
if (array[i][j] ==4)
{
if (i == end_x && j == end_y && start_Home != 0)//显示终点
{
SetConsoleTextColor(cGREEN);
CursorJump(2 * j, i);
printf("●");
}
}
}
}
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
if (array[i][j] == PATH)
{
SetConsoleTextColor(cGREEN);
CursorJump(2 * j, i);
printf("○");
}
}
}
return array[20];
}
清空迷宫
int* clear_maze(int(*array)[20], int x, int y)
{
start_x = 0;//记录的是起点横坐标
start_y = 0;//记录起点纵坐标
end_x = 0;//记录终点横坐标
end_y = 0;// 记录终点纵坐标
start_Home = 0;//当已经有起点的时候,start_Home=1
end_Home = 0;//已经有终点的时候,end_Home=1
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
array[i][j] = KONG;
}
}
Showmaze(array, x, y);
return array[20];
}
展示迷宫的足迹
int* show_footmaze(int(*array)[20], int x, int y)
{
system("cls");
int input = 1;
if (start_Home == 0)
{
printf("Error:You must set startplace.");
return array[20];
}
if (end_Home == 0)
{
printf("Error:You must set endplace.");
return array[20];
}
while (input)
{
PathStack* p;
p = PathStackCreate();
Pos nowpos, startpos, endpos, lastpos;
startpos.row = start_x;
startpos.col = start_y;
endpos.row = end_x;
endpos.col = end_y;
nowpos = lastpos = startpos;
while (nowpos.row != endpos.row || nowpos.col != endpos.col)
{
if (0 == array[nowpos.row + 1][nowpos.col] || 4 == array[nowpos.row + 1][nowpos.col] && (nowpos.row + 1 != lastpos.row || nowpos.col != lastpos.col))
{
array[nowpos.row + 1][nowpos.col] = PATH;
lastpos = nowpos;
nowpos.row++;
Input(p, nowpos);
}
else if (0 == array[nowpos.row - 1][nowpos.col] || 4 == array[nowpos.row - 1][nowpos.col] && (nowpos.row - 1 != lastpos.row || nowpos.col != lastpos.col))
{
array[nowpos.row - 1][nowpos.col] = PATH;
lastpos = nowpos;
nowpos.row--;
Input(p, nowpos);
}
else if (0 == array[nowpos.row][nowpos.col + 1] || 4 == array[nowpos.row][nowpos.col + 1] && (nowpos.row != lastpos.row || nowpos.col + 1 != lastpos.col))
{
array[nowpos.row][nowpos.col + 1] = PATH;
lastpos = nowpos;
nowpos.col++;
Input(p, nowpos);
}
else if (0 == array[nowpos.row][nowpos.col - 1] || 4 == array[nowpos.row][nowpos.col - 1] && (nowpos.row != lastpos.row || nowpos.col - 1 != lastpos.col))
{
array[nowpos.row][nowpos.col - 1] = PATH;
lastpos = nowpos;
nowpos.col--;
Input(p, nowpos);
}
else
{
array[nowpos.row][nowpos.col] = WRONG;
nowpos = lastpos = startpos;
clear(p);
}
}
if (nowpos.row == endpos.row && nowpos.col == endpos.col)
{
Showmaze(array, x, y);
break;
}
//Showmaze(array, x, y);
}
return array[20];
}
这一步是进行设置起点,终点,设置墙的函数
如下:
int* lalaCursormove(int(*array)[20], int x, int y, int position_x, int position_y)//上下左右移动,设置、删除墙
{
int input = 1;
while (input)
{
int ch1;
int ch2;
if (ch1 = _getch())
{
if (ch1 == 224)
{
ch2 = _getch();
if (ch2 == 77 && position_y != (2 * x - 2))//右移
{
position_y = position_y + 2;
CursorJump(position_y, position_x);
}
if (ch2 == 80 && position_x != (y - 1))//下移
{
position_x = position_x + 1;
CursorJump(position_y, position_x);
}
if (ch2 == 75 && position_y != 0)//左移
{
position_y = position_y - 2;
CursorJump(position_y, position_x);
}
if (ch2 == 72 && position_x != 0)//上移
{
position_x = position_x - 1;
CursorJump(position_y, position_x);
}
if (ch2 == 83 && position_x != 0 && position_y != 0 && position_x != (y - 1) && position_y != (2 * x) - 2)//删除墙
{
array[position_x][position_y / 2] = KONG;
Showmaze(array, x, y);
}
if (ch2 == 115)//放起点,起点在墙内的任意位置
{
if (!(position_x == 0 || position_y == 0 || position_x == (y - 1) || position_y == (2 * x) - 2))
{
if (start_Home == 0)
{
start_Home = 1;
array[position_x][position_y / 2] = KONG;
start_x = position_x;
start_y = position_y / 2;
Showmaze(array, x, y);
}
}
}
if (ch2 == 116)//放终点,在任意位置,任意位置可以作为终点
{
if (end_Home == 0)
{
end_Home = 4;
array[position_x][position_y / 2] = 4;
end_x = position_x;
end_y = position_y / 2;
Showmaze(array, x, y);
}
}
}
if (ch1 == 13 && array[position_x][position_y / 2] == KONG)//添加墙
{
array[position_x][position_y / 2] = WALL;
Showmaze(array, x, y);
}
}
if (ch1 == 27)//退出编辑状态
{
system("cls");
input = 0;
}
}
return array[20];
}
分别是home设置起点,end设置终点,enter设置墙,delete删除墙
然后还有一系列操作;为了防止搬运,修改了函数里的一些内容
当然还有一个次要的函数没有给出来
但是谢谢理解:
需要代做各种系统,请添加qq:2914061332或微信 18198422692
价格实惠,当然如果是看到这篇文章来找的,可以凭借赞来获取更多优惠哦!
下面是部分截图
设置的迷宫大小,输入的10 10,初始化之后如图
退出到菜单,然后输入2,开始编辑迷宫的内容,我随便添加了点东西
4.清空迷宫,如图,会将屏幕全部清空
然后1.初始化之后,接下来就会这样
谢谢阅读!!!