#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <conio.h>
#include <windows.h>
using namespace std;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int hhh;
int dx1[] = {0, -1, -1, 0, 1, 1};
int dy1[] = {-1, -1, 0, 1, 0, -1};
int dx2[] = {0, -1, -1, 0, 1, 1};
int dy2[] = {-1, 0, 1, 1, 1, 0};
int board[100][100];
int px, py;
int cx, cy;
int dis[100][100];
int minn = 1e9;
int choice = -1;
int rd = 0;
bool in(int x, int y)
{
return 1 <= x && x <= 11 && 1 <= y && y <= 11;
}
void drawBoard()
{
cout << "\033c" << flush;
for (int i = 1; i <= 11; i++)
{
cout << " ";
if (i % 2 == 0) cout << " ";
for (int j = 1; j <= 11; j++)
{
if (i == cx && j == cy && board[i][j] == 0)
cout << "☆ ";
else if (i == cx && j == cy && board[i][j] == 1){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
cout << "☆ ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
}
else if (board[i][j] == 2) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
cout << " P ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
}
else if (board[i][j] == 1)
cout << "● ";
else
cout << "○ ";
}
cout << endl;
}
}
void init()
{
srand(time(0));
px = 6, py = 6;
cx = 1, cy = 1;
board[px][py] = 2;
rd = 0;
for (int i = 1; i <= 15; i++)
{
int r = rand() % 121;
int row = r / 11 + 1;
int col = r % 11 + 1;
while (board[row][col] != 0)
{
r = rand() % 121;
row = r / 11 + 1;
col = r % 11 + 1;
}
board[row][col] = 1;
}
}
void moveCursor(int dir)
{
int nx = cx + dx[dir];
int ny = cy + dy[dir];
if (in(nx, ny))
{
cx = nx;
cy = ny;
}
}
bool mark(int x, int y)
{
if (board[x][y] == 0)
{
board[x][y] = 1;
return true;
}
return false;
}
void dfs(int x, int y, int step, int col)
{
if (step > minn) return;
if (x == 1 || x == 11 || y == 1 || y == 11)
{
if (dis[x][y] < minn)
{
minn = dis[x][y];
choice = col;
}
return;
}
for (int i = 0; i < 6; i++)
{
int nx, ny;
if (x % 2 == 1)
{
nx = x + dx1[i];
ny = y + dy1[i];
}
else
{
nx = x + dx2[i];
ny = y + dy2[i];
}
if (in(nx, ny) && board[nx][ny] == 0 && step + 1 < dis[nx][ny])
{
dis[nx][ny] = step+1;
if (step == 0) dfs(nx, ny, step+1, i);
else dfs(nx, ny, step+1, col);
}
}
}
int pipiMove()
{
memset(dis, 0x3f, sizeof(dis));
minn = 1e9, choice = -1;
dis[px][py] = 0;
dfs(px, py, 0, -1);
if (choice == -1)
{
return -1;
}
int nx, ny;
if (px % 2 == 1)
{
nx = px + dx1[choice];
ny = py + dy1[choice];
}
else
{
nx = px + dx2[choice];
ny = py + dy2[choice];
}
board[px][py] = 0;
board[nx][ny] = 2;
px = nx;
py = ny;
return choice;
}
void intro()
{
cout << "***********************************************************" << endl;
cout << " 欢迎来到【小P哪里逃】游戏~" << endl;
cout << " 在这个游戏里会有一个 11 * 11 的游戏地图" << endl;
cout << " 按下w↑、a←、s↓、d→ 控制光标☆ 移动,按下 空格键 放置路障" << endl;
cout << " 每次皮皮会向相邻的六个方向逃跑,想办法困住小P!不要让小P逃出去!" << endl;
cout << " 点击 任意键 开始游戏" << endl;
cout << "***********************************************************" << endl;
getch();
}
void game()
{
intro();
while (true)
{
drawBoard();
char ch = getch();
if (ch == 'w') moveCursor(0);
else if (ch == 's') moveCursor(1);
else if (ch == 'a') moveCursor(2);
else if (ch == 'd') moveCursor(3);
else if (ch == '0') exit(0);
else if (ch == ' ')
{
if (!mark(cx, cy))
{
continue;
}
rd++;
drawBoard();
if (pipiMove() != -1)
{
drawBoard();
}
else
{
return;
}
}
}
}
void result()
{
cout << "***********************************************************" << endl;
if (px == 1 || px == 11 || py == 1 || py == 11)
{
cout << "小P : 嘿嘿嘿~我逃出来了!" << endl;
cout << "游戏失败!" << endl;
}
else
{
cout << "小P : 呜呜呜……我被困住了!救命呀?? ~~~" << endl;
cout << "游戏获胜!" << endl;
cout << "步数: " << rd << endl;
}
cout << "***********************************************************" << endl;
}
int main()
{
init();
game();
result();
return 0;
}