恶魔轮盘简易版
2025-09-24 10:39:32
发布于:重庆
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
// 物品类型枚举
enum ItemType {
NONE,
SILENCER, // 消音器:降低当前回合的风险
EXTRA_BULLET, // 额外子弹:增加一个子弹
SKIP_TURN, // 跳过回合:让对手额外多一个回合
RELOADER, // 重新装弹器:重新随机所有子弹位置
MEDKIT // 医疗包:恢复1点生命值
};
// 恶魔轮盘游戏类
class DevilRoulette {
private:
const int BASE_CHAMBER_COUNT = 6; // 基础枪膛容量
int currentChamberCount; // 当前关卡枪膛容量
vector<int> bulletPositions; // 子弹位置列表
int currentChamber; // 当前枪膛位置
bool isLevelOver; // 关卡是否结束
bool isGameOver; // 游戏是否结束
bool isPlayerTurn; // 是否是玩家回合
int playerHealth; // 玩家生命值
int devilHealth; // 恶魔生命值
int currentLevel; // 当前关卡
const int MAX_LEVELS = 5; // 最大关卡数
int playerItems[5]; // 玩家物品 [消音器, 额外子弹, 跳过回合, 重新装弹器, 医疗包]
int devilItems[5]; // 恶魔物品
// 随机生成一个物品(随关卡提升概率增加)
ItemType generateRandomItem() {
int randVal = rand() % 100;
int itemChance = 30 + currentLevel * 5; // 基础30%概率,每关+5%
if (randVal < itemChance) {
return static_cast<ItemType>(1 + rand() % 5); // 1-5的随机物品
}
return NONE;
}
// 使用物品
void useItem(ItemType item) {
switch (item) {
case SILENCER:
cout << (isPlayerTurn ? "你" : "恶魔") << "使用了消音器!本回合风险降低!" << endl;
// 消音器效果:本轮如果是子弹位置,有50%概率不触发
if (find(bulletPositions.begin(), bulletPositions.end(), currentChamber) != bulletPositions.end()) {
if (rand() % 2 == 0) {
cout << "消音器生效了!子弹没有触发!" << endl;
// 移除当前位置的子弹
auto it = find(bulletPositions.begin(), bulletPositions.end(), currentChamber);
if (it != bulletPositions.end()) {
bulletPositions.erase(it);
}
// 再添加一个新的随机子弹位置
addBullet();
}
}
break;
case EXTRA_BULLET:
cout << (isPlayerTurn ? "你" : "恶魔") << "使用了额外子弹!增加了一个子弹!" << endl;
addBullet();
break;
case SKIP_TURN:
cout << (isPlayerTurn ? "你" : "恶魔") << "使用了跳过回合卡!对方将多一个回合!" << endl;
// 不切换回合,让对方再玩一次
return;
case RELOADER:
cout << (isPlayerTurn ? "你" : "恶魔") << "使用了重新装弹器!所有子弹位置重新随机!" << endl;
resetBullets();
break;
case MEDKIT:
cout << (isPlayerTurn ? "你" : "恶魔") << "使用了医疗包!恢复了1点生命值!" << endl;
if (isPlayerTurn) {
playerHealth = min(playerHealth + 1, 3); // 最大生命值3
} else {
devilHealth = min(devilHealth + 1, 3 + currentLevel / 2); // 恶魔生命值随关卡提升
}
showHealth();
break;
default:
break;
}
// 减少物品数量
if (isPlayerTurn) {
playerItems[item - 1]--;
} else {
devilItems[item - 1]--;
}
}
// 添加一个子弹
void addBullet() {
if (bulletPositions.size() < currentChamberCount - 1) { // 保留至少一个空位置
int newPos;
do {
newPos = rand() % currentChamberCount;
} while (find(bulletPositions.begin(), bulletPositions.end(), newPos) != bulletPositions.end());
bulletPositions.push_back(newPos);
}
}
// 重置子弹(随关卡提升难度)
void resetBullets() {
bulletPositions.clear();
// 基础1个子弹,每关增加0-1个子弹
int bulletCount = 1 + min(currentLevel / 2, 3); // 最多4个子弹
// 随机增加0-1个子弹,增加不确定性
if (rand() % 2 == 0) bulletCount++;
for (int i = 0; i < bulletCount; i++) {
addBullet();
}
}
// 显示物品
void showItems(bool isPlayer) {
const string itemNames[5] = {"消音器", "额外子弹", "跳过回合卡", "重新装弹器", "医疗包"};
int* items = isPlayer ? playerItems : devilItems;
cout << (isPlayer ? "你的物品:" : "恶魔的物品:");
bool hasItem = false;
for (int i = 0; i < 5; i++) {
if (items[i] > 0) {
cout << itemNames[i] << "(" << items[i] << ") ";
hasItem = true;
}
}
if (!hasItem) {
cout << "无";
}
cout << endl;
}
// 显示生命值
void showHealth() {
cout << "\n--- 生命值 ---" << endl;
cout << "你: ";
for (int i = 0; i < playerHealth; i++) cout << "? ";
cout << endl;
cout << "恶魔: ";
for (int i = 0; i < devilHealth; i++) cout << "? ";
cout << endl;
}
public:
// 构造函数:初始化游戏
DevilRoulette() {
srand(time(0)); // 初始化随机数生成器
resetGame();
}
// 重置整个游戏
void resetGame() {
currentLevel = 1;
playerHealth = 3;
isGameOver = false;
startLevel();
}
// 开始新关卡
void startLevel() {
// 每关增加枪膛容量,但不超过10
currentChamberCount = BASE_CHAMBER_COUNT + min(currentLevel - 1, 4);
resetBullets();
currentChamber = 0;
isLevelOver = false;
// 关卡越高,恶魔先攻概率越大
int playerFirstChance = 50 - (currentLevel - 1) * 10; // 第一关50%,每关减10%
isPlayerTurn = (rand() % 100 < playerFirstChance);
// 初始化物品
for (int i = 0; i < 5; i++) {
playerItems[i] = 0;
devilItems[i] = 0;
}
// 给双方初始物品,随关卡增加
int initialItems = 1 + currentLevel / 2;
for (int i = 0; i < initialItems; i++) {
ItemType item = generateRandomItem();
if (item != NONE) {
playerItems[item - 1]++;
}
item = generateRandomItem();
if (item != NONE) {
devilItems[item - 1]++;
}
}
// 恶魔生命值随关卡提升
devilHealth = 3 + currentLevel / 2;
cout << "\n======= 第 " << currentLevel << " 关 开始 =======" << endl;
cout << "枪膛容量: " << currentChamberCount << "个位置" << endl;
cout << "子弹数量: " << bulletPositions.size() << "发" << endl;
showHealth();
cout << (isPlayerTurn ? "你先开始!" : "恶魔先开始!") << endl;
showItems(true);
}
// 扣动扳机
void pullTrigger() {
if (isLevelOver || isGameOver) return;
cout << "\n--- " << (isPlayerTurn ? "你的回合" : "恶魔的回合") << " ---" << endl;
// 检查是否获得物品
ItemType newItem = generateRandomItem();
if (newItem != NONE) {
const string itemNames[5] = {"消音器", "额外子弹", "跳过回合卡", "重新装弹器", "医疗包"};
cout << "恭喜!获得了一个" << itemNames[newItem - 1] << "!" << endl;
if (isPlayerTurn) {
playerItems[newItem - 1]++;
} else {
devilItems[newItem - 1]++;
}
showItems(isPlayerTurn);
}
// 玩家可以选择使用物品
if (isPlayerTurn) {
showItems(true);
cout << "是否使用物品?(0-不使用, 1-消音器, 2-额外子弹, 3-跳过回合卡, 4-重新装弹器, 5-医疗包): ";
int choice;
cin >> choice;
if (choice >= 1 && choice <= 5) {
if (playerItems[choice - 1] > 0) {
useItem(static_cast<ItemType>(choice));
} else {
cout << "你没有这个物品!" << endl;
}
}
} else {
// 恶魔AI:随关卡提升变得更智能
int useItemChance = 30 + currentLevel * 5; // 基础30%,每关+5%
if (rand() % 100 < useItemChance) {
// 优先使用对玩家不利的物品
vector<int> priority = {2, 3, 1, 5, 4}; // 额外子弹、跳过回合、消音器、医疗包、重新装弹器
for (int i : priority) {
if (devilItems[i - 1] > 0) {
useItem(static_cast<ItemType>(i));
break;
}
}
}
}
// 检查是否命中
bool hit = find(bulletPositions.begin(), bulletPositions.end(), currentChamber) != bulletPositions.end();
if (hit) {
if (isPlayerTurn) {
cout << "砰!你被击中了!失去1点生命值!" << endl;
playerHealth--;
if (playerHealth <= 0) {
cout << "你的生命值耗尽了!" << endl;
isLevelOver = true;
isGameOver = true;
}
} else {
cout << "砰!恶魔被击中了!失去1点生命值!" << endl;
devilHealth--;
if (devilHealth <= 0) {
cout << "恶魔的生命值耗尽了!" << endl;
isLevelOver = true;
if (currentLevel >= MAX_LEVELS) {
// 通关所有关卡
isGameOver = true;
}
}
}
showHealth();
} else {
cout << "咔哒... 幸运的一次!" << endl;
// 转动枪膛到下一个位置
currentChamber = (currentChamber + 1) % currentChamberCount;
}
// 如果关卡未结束,切换回合(使用跳过回合卡时不切换)
if (!isLevelOver) {
isPlayerTurn = !isPlayerTurn;
}
}
// 恶魔回合
void devilTurn() {
if (isLevelOver || isGameOver || isPlayerTurn) return;
// 简单延迟增加紧张感
cout << "恶魔正在思考...";
for (int i = 0; i < 3; i++) {
cout << ".";
cout.flush();
clock_t end = clock() + CLOCKS_PER_SEC / 3;
while (clock() < end);
}
cout << endl;
// 恶魔选择扣动扳机
cout << "恶魔选择扣动扳机..." << endl;
pullTrigger();
}
// 检查关卡是否结束
bool levelOver() {
return isLevelOver;
}
// 检查游戏是否结束
bool gameOver() {
return isGameOver;
}
// 检查玩家是否胜利
bool playerWon() {
return isGameOver && playerHealth > 0;
}
// 进入下一关
void nextLevel() {
if (!isLevelOver || isGameOver) return;
currentLevel++;
if (currentLevel > MAX_LEVELS) {
isGameOver = true;
} else {
startLevel();
}
}
// 检查是否是玩家回合
bool playerTurn() {
return isPlayerTurn;
}
// 获取当前关卡
int getCurrentLevel() {
return currentLevel;
}
};
int main() {
DevilRoulette game;
string input;
cout << "欢迎来到恶魔轮盘挑战!" << endl;
cout << "你需要通过" << game.getCurrentLevel() << "关,击败恶魔获得胜利!" << endl;
cout << "每关中,生命值耗尽的一方失败" << endl;
while (true) {
if (game.gameOver()) {
if (game.playerWon()) {
cout << "\n恭喜你!成功通过所有关卡,击败了恶魔!" << endl;
} else {
cout << "\n很遗憾,你被恶魔击败了!" << endl;
}
cout << "要再玩一次吗?(y/n): ";
cin >> input;
if (input == "y" || input == "Y") {
game.resetGame();
} else {
cout << "谢谢参与,再见!" << endl;
break;
}
} else if (game.levelOver()) {
cout << "\n第 " << game.getCurrentLevel() << " 关结束!" << endl;
cout << "准备进入第 " << game.getCurrentLevel() + 1 << " 关...(按任意键继续)";
cin.ignore();
cin.get();
game.nextLevel();
} else {
if (game.playerTurn()) {
cout << "\n请选择操作: (1)扣动扳机 (2)退出游戏: ";
cin >> input;
if (input == "1") {
game.pullTrigger();
} else if (input == "2") {
cout << "你选择退出游戏,再见!" << endl;
break;
} else {
cout << "无效输入,请重新选择!" << endl;
}
} else {
// 恶魔回合
game.devilTurn();
}
}
}
return 0;
}
这里空空如也
有帮助,赞一个