效果
main.cpp
#include <iostream>
#include "maze.h"
const int X = 35;
const int Y = 79;
int main(int argc, char** argv) {
system(" title 迷宫 ");
system(" color 08 ");
system(" mode 167 , 41 ");
Maze maze( X , Y );
while(1){
maze.creat();
maze.print_maze();
if ( maze.wander() == 1 )
system("cls");
else
break;
}
return 0;
}
console.h
#ifndef CONSOLE_H_
#define CONSOLE_H_
#include <windows.h>
#include <iostream>
#include <string>
void gotoxy( short x , short y ){
//输出控制台句柄
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD xOy = {x,y};
SetConsoleCursorPosition(handle,xOy);
// 04
SetConsoleTextAttribute(handle, ( (unsigned short)5 % 16 | ( (unsigned short)0 % 16 * 16 ) ) );
static bool state = true;
while( state ){
CONSOLE_CURSOR_INFO cci;
GetConsoleCursorInfo(handle,&cci);//handle与cci关联
cci.dwSize = 100;
// cci.bVisible = true;
SetConsoleCursorInfo(handle, &cci);
state = false;
}
}
void gotoxy( short x , short y , std::string s ){
gotoxy(x,y);
std::cout << s;
}
#endif
maze.h
#ifndef MAZE_H_
#define MAZE_H_
#include <iostream>
#include <stack>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include "console.h"
using std::cout;
using std::stack;
using std::pair;
using std::vector;
class Maze{
private:
int row;
int col;
int exit;
//0--路 1--墙
vector< vector<bool> > unit;
enum key_direction{
Up = 72,
Down = 80,
Left = 75,
Right = 77,
};
void setExit();
int getExit();
void delete_traverse( stack<key_direction>& s ,
int x , int y , key_direction key );
public:
Maze(int r , int c) : row(r) , col(c){}
void creat();
void print_maze();
int wander();
};
//墙的坐标
typedef pair<int,int> wall;
wall makeWall( int x ,int y ){
return std::make_pair(x,y);
}
void Maze::delete_traverse( stack<key_direction>& s ,
int x , int y , key_direction key ){
key_direction temp = s.top();
//抵消
if ( temp + key == 152 ){
gotoxy( x , y , " " );
s.pop();
}
else
s.push(key);
}
int Maze::wander(){
stack<key_direction> s;//辅助栈->消除
s.push((key_direction)999);
bool loop = true;
short ch;
int x = 2 , y = 2; //光标初始值
int i = 0 , j = -1; //unit初始值
while(loop){
switch( getch() ){
case Up:
if( i > 0 && !unit[i-1][j] ){
i--;
delete_traverse(s,x,y,Up);
y -= 1;
}
break;
case Down:
if( i < row - 1 && !unit[i+1][j] ){
i++;
delete_traverse(s,x,y,Down);
y += 1;
}
break;
case Left:
if( j > 0 && !unit[i][j-1] ){
j--;
delete_traverse(s,x,y,Left);
x -= 2;
}
break;
case Right:
if( j < col - 1 && !unit[i][j+1] ||
( j == col - 1 && exit == i ) ){
j++;
delete_traverse(s,x,y,Right);
x += 2;
//终点
if ( j == col )
loop = false;//退出循环
}
break;
}
gotoxy(x,y,"■");
gotoxy(x,y);//复位
}
gotoxy(col-4,row+4,"按1继续.........");
}
void Maze::setExit(){
do{
exit = rand() % row;
}while ( unit[exit][col-1] == 1 );
}
int Maze::getExit(){
return exit;
}
void Maze::creat(){
//将unit全部初始化为墙 bool->1
//unit row行 col列
//seed
srand((unsigned long)time(0));
//initialize unit
unit.resize(row);
for( int i ; i < row ; i++ ){
unit[i].resize(col,1);
}
unit[0][0] = 0;//begin
vector<wall> traverse_wall;
traverse_wall.push_back(makeWall(0,1));
traverse_wall.push_back(makeWall(1,0));
int index;
int x,y;
wall temp;
while( !traverse_wall.empty()){
// index 弹出判断
index = rand() % traverse_wall.size();
temp = traverse_wall[index];
x = temp.first;
y = temp.second;
traverse_wall[index] = traverse_wall.back();
traverse_wall.pop_back();
//
if ( x % 2 == 0 ){
if ( !unit[x][y-1] && unit[x][y+1] )
unit[x][y] = unit[x][y+1] = 0 , y++;
else if ( unit[x][y-1] && !unit[x][y+1] )
unit[x][y] = unit[x][y-1] = 0 , y--;
else
continue;
}
else if( x % 2 == 1 ){
if ( !unit[x-1][y] && unit[x+1][y] )
unit[x][y] = unit[x+1][y] = 0 , x++;
else if ( unit[x-1][y] && !unit[x+1][y] )
unit[x][y] = unit[x-1][y] = 0 , x--;
else
continue;
}
if( x > 0 )
traverse_wall.push_back(makeWall(x-1,y));
if( x < row - 1 )
traverse_wall.push_back(makeWall(x+1,y));
if( y > 0 )
traverse_wall.push_back(makeWall(x,y-1));
if( y < col - 1 )
traverse_wall.push_back(makeWall(x,y+1));
}
}
void Maze::print_maze(){
setExit();
cout << "\n ";//margin-top
//上边界
for( int i = 0 ; i < col + 2; i++ )
cout << "■";
cout << "\n";
for( int i = 0 ; i < row ; i++ ){
cout << " ■";//左边界
for( int j = 0 ; j < col ; j++ )
cout << ( unit[i][j] ? "■" : " " );
cout << "■\n";//右边界
}
//下边界
cout << " ";
for( int i = 0 ; i < col + 2; i++ )
cout << "■";
//+2 第一行留白 第二行边界
//*2 比例尺 +2 ->左边界 +2 ->到右边界上
gotoxy(2*col+4,exit+2,"■");//终点
gotoxy(2,2,"■");//起点
}
#endif
|