#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// 设置随机种子
srand(time(0));
// 定义地牢的尺寸
const int ROWS = 5;
const int COLS = 5;
// 创建地牢数组
char dungeon[ROWS][COLS];
// 初始化地牢
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
dungeon[i][j] = '.';
}
}
// 放置玩家和怪物
int playerX = rand() % ROWS;
int playerY = rand() % COLS;
int monsterX = rand() % ROWS;
int monsterY = rand() % COLS;
dungeon[playerX][playerY] = 'P';
dungeon[monsterX][monsterY] = 'M';
// 游戏循环
bool gameOver = false;
while (!gameOver) {
// 打印当前地牢状态
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
cout << dungeon[i][j] << " ";
}
cout << endl;
}
cout << "移动玩家 (w上, a左, s下, d右): ";
// 获取玩家输入
char move;
cin >> move;
// 更新玩家位置
dungeon[playerX][playerY] = '.';
if (move == 'w' && playerX > 0) {
playerX--;
}
else if (move == 'a' && playerY > 0) {
playerY--;
}
else if (move == 's' && playerX < ROWS - 1) {
playerX++;
}
else if (move == 'd' && playerY < COLS - 1) {
playerY++;
}
dungeon[playerX][playerY] = 'P';
// 判断游戏是否结束
if (playerX == monsterX && playerY == monsterY) {
gameOver = true;
cout << "你被怪物吃掉了!游戏结束!" << endl;
}
}
return 0;
}