c++实现无限关卡迷宫
这是一个c++的迷宫
·
首先这个迷宫有无数关,每一次边长加1,起点为上一次终点,这一次终点随机。
游戏照片:
首先我们需要一点头文件和宏
#include<stdio.h>
#include<Windows.h>
#include<time.h>
#include<math.h>
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define WALL 0
#define ROUTE 1
#define REN 2
我们还需要一些变量
static int Rank = 0;//迷宫复杂度
int xx=2,yy=1,zx,zy,L=10;
char a;
int **Maze = (int**)malloc(L * sizeof(int *));//两维数组
还需要两个函数
void CreateMaze(int **maze, int x, int y);
void print();
CreateMaze是通过dfs实现的生成迷宫(自己上网上搜)
void CreateMaze(int **maze, int x, int y) {
maze[x][y] = ROUTE;
int direction[4][2] = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
for (int i = 0; i < 4; i++) {
int r = rand() % 4;
int temp = direction[0][0];
direction[0][0] = direction[r][0];
direction[r][0] = temp;
temp = direction[0][1];
direction[0][1] = direction[r][1];
direction[r][1] = temp;
}
for (int i = 0; i < 4; i++) {
int dx = x;
int dy = y;
int range = 1 + (Rank == 0 ? 0 : rand() % Rank);
while (range>0) {
dx += direction[i][0];
dy += direction[i][1];
if (maze[dx][dy] == ROUTE) {
break;
}
int count = 0;
for (int j = dx - 1; j < dx + 2; j++) {
for (int k = dy - 1; k < dy + 2; k++) {
if (abs(j - dx) + abs(k - dy) == 1 && maze[j][k] == ROUTE) {
count++;
}
}
}
if (count > 1) {
break;
}
--range;
maze[dx][dy] = ROUTE;
}
if (range <= 0) {
CreateMaze(maze, dx, dy);
}
}
}
print是输出函数
void print(){
for (int i = 0; i < L; i++) {
for (int j = 0; j < L; j++) {
if(i == zx && j == zy) cout<<"终";
else if(Maze[i][j] == REN){
printf("人");
}
else if (Maze[i][j] == ROUTE) {
printf(" ");
}
else {
printf("墙");
}
}
printf("\n");
}
}
主函数,首先我们要随机化种子
srand((unsigned)time(NULL));
接着是,无限循环,里面每次迷宫大小加1。
L++;
再把四个角当作道路,生成道路
for (int i = 0; i < L; i++){
Maze[i][0] = ROUTE;
Maze[0][i] = ROUTE;
Maze[i][L - 1] = ROUTE;
Maze[L - 1][i] = ROUTE;
}
CreateMaze(Maze, 2, 2);
默认设置上一次的终点为这次的起点
Maze[xx][yy] = REN;
随机生成终点(我调了好久)
do{
zx=rand()%(L-2),zy=rand()%(L-2);
if(zx==0) zx+=2;
if(zy==0) zy+=2;
if(zx>=L-1) zx-=2;
if(zy>=L-1) zy-=2;
}
while(Maze[zx][zy]!=ROUTE);
移动
while(scanf("%c",&a)&&(xx!=zx||yy!=zy)){
system("cls");
print();
int nx=xx,ny=yy;
if(a=='w') nx=xx-1;
else if(a=='a') ny=yy-1;
else if(a=='s') nx=xx+1;
else if(a=='d')ny=yy+1;
if(Maze[nx][ny]!=ROUTE||nx<0||ny<0||nx>=L||ny>=L) continue;
else{
swap(Maze[xx][yy],Maze[nx][ny]);
xx=nx;
yy=ny;
}
}
全部代码:
#include<stdio.h>
#include<Windows.h>
#include<time.h>
#include<math.h>
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
#define WALL 0
#define ROUTE 1
#define REN 2
static int Rank = 0;
int xx=2,yy=1,zx,zy,L=10;
char a;
int **Maze = (int**)malloc(L * sizeof(int *));
void CreateMaze(int **maze, int x, int y);
void print();
int main() {
srand((unsigned)time(NULL));
while(1){
L++;
for (int i = 0; i < L; i++) {
Maze[i] = (int*)calloc(L, sizeof(int));
}
for (int i = 0; i < L; i++){
Maze[i][0] = ROUTE;
Maze[0][i] = ROUTE;
Maze[i][L - 1] = ROUTE;
Maze[L - 1][i] = ROUTE;
}
CreateMaze(Maze, 2, 2);
Maze[xx][yy] = REN;
do{
zx=rand()%(L-2),zy=rand()%(L-2);
if(zx==0) zx+=2;
if(zy==0) zy+=2;
if(zx>=L-1) zx-=2;
if(zy>=L-1) zy-=2;
}
while(Maze[zx][zy]!=ROUTE);
while(scanf("%c",&a)&&(xx!=zx||yy!=zy)){
system("cls");
print();
int nx=xx,ny=yy;
if(a=='w') nx=xx-1;
else if(a=='a') ny=yy-1;
else if(a=='s') nx=xx+1;
else if(a=='d')ny=yy+1;
if(Maze[nx][ny]!=ROUTE||nx<0||ny<0||nx>=L||ny>=L) continue;
else{
swap(Maze[xx][yy],Maze[nx][ny]);
xx=nx;
yy=ny;
}
}
}
}
void CreateMaze(int **maze, int x, int y) {
maze[x][y] = ROUTE;
int direction[4][2] = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
for (int i = 0; i < 4; i++) {
int r = rand() % 4;
int temp = direction[0][0];
direction[0][0] = direction[r][0];
direction[r][0] = temp;
temp = direction[0][1];
direction[0][1] = direction[r][1];
direction[r][1] = temp;
}
for (int i = 0; i < 4; i++) {
int dx = x;
int dy = y;
int range = 1 + (Rank == 0 ? 0 : rand() % Rank);
while (range>0) {
dx += direction[i][0];
dy += direction[i][1];
if (maze[dx][dy] == ROUTE) {
break;
}
int count = 0;
for (int j = dx - 1; j < dx + 2; j++) {
for (int k = dy - 1; k < dy + 2; k++) {
if (abs(j - dx) + abs(k - dy) == 1 && maze[j][k] == ROUTE) {
count++;
}
}
}
if (count > 1) {
break;
}
--range;
maze[dx][dy] = ROUTE;
}
if (range <= 0) {
CreateMaze(maze, dx, dy);
}
}
}
void print(){
for (int i = 0; i < L; i++) {
for (int j = 0; j < L; j++) {
if(i == zx && j == zy) cout<<"终";
else if(Maze[i][j] == REN){
printf("人");
}
else if (Maze[i][j] == ROUTE) {
printf(" ");
}
else {
printf("墙");
}
}
printf("\n");
}
}
如果你愿意,你可以隐藏光标。
更多推荐
已为社区贡献1条内容
所有评论(0)