話不多說
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.初始化之后,接下來就會這樣
謝謝閱讀!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394169.html
標籤:其他
