地牢模拟器

#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;
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值