#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <random>
using namespace std;
// 游戏格子类型
enum class SpaceType {
GO, // 起点
PROPERTY, // 地产
JAIL, // 监狱
FREE_PARKING, // 免费停车
GO_TO_JAIL // 去监狱
};
// 地产类
class Property {
public:
string name;
int price;
int rent;
bool isOwned;
int ownerId;
Property(string n, int p, int r)
: name(n), price(p), rent(r), isOwned(false), ownerId(-1) {}
};
// 游戏格子类
class Space {
public:
SpaceType type;
string name;
Property* property; // 如果是地产格子,指向对应的地产
Space(SpaceType t, string n, Property* prop = nullptr)
: type(t), name(n), property(prop) {}
};
// 玩家类
class Player {
public:
string name;
int position;
int money;
bool isInJail;
int jailTurnsLeft;
Player(string n) : name(n), position(0), money(1500),
isInJail(false), jailTurnsLeft(0) {}
// 移动玩家
void move(int steps, int boardSize) {
position = (position + steps) % boardSize;
}
};
// 游戏类
class MonopolyGame {
private:
vector<Player> players;
vector<Space> board;
vector<Property> properties;
int currentPlayerIndex;
bool gameOver;
size_t jailPosition; // 明确的监狱位置
public:
MonopolyGame() : currentPlayerIndex(0), gameOver(false) {
srand(time(0));
initializeGame();
}
// 初始化游戏
void initializeGame() {
// 创建地产
properties = {
Property("滨海大道", 60, 2),
Property("阳光大道", 60, 4),
Property("科技园区", 100, 6),
Property("金融中心", 100, 6),
Property("中央公园", 120, 8),
Property("商业街", 140, 10),
Property("度假区", 140, 10),
Property("工业园区", 160, 12),
Property("购物中心", 180, 14),
Property("娱乐城", 180, 14),
Property("住宅区", 200, 16),
Property("大学城", 220, 18),
Property("医疗中心", 220, 18),
Property("体育场馆", 240, 20),
Property("会展中心", 260, 22),
Property("豪华酒店", 280, 24),
Property("国际机场", 300, 26),
Property("中央车站", 300, 26),
Property("港口", 320, 28),
Property("能源基地", 350, 35),
Property("太空中心", 400, 50)
};
// 创建游戏棋盘
board = {
Space(SpaceType::GO, "起点"),
Space(SpaceType::PROPERTY, "滨海大道", &properties[0]),
Space(SpaceType::FREE_PARKING, "免费停车"),
Space(SpaceType::PROPERTY, "阳光大道", &properties[1]),
Space(SpaceType::JAIL, "监狱"),
Space(SpaceType::PROPERTY, "科技园区", &properties[2]),
Space(SpaceType::PROPERTY, "金融中心", &properties[3]),
Space(SpaceType::FREE_PARKING, "免费停车"),
Space(SpaceType::PROPERTY, "中央公园", &properties[4]),
Space(SpaceType::GO_TO_JAIL, "去监狱"),
Space(SpaceType::PROPERTY, "商业街", &properties[5]),
Space(SpaceType::PROPERTY, "度假区", &properties[6]),
Space(SpaceType::FREE_PARKING, "免费停车"),
Space(SpaceType::PROPERTY, "工业园区", &properties[7]),
Space(SpaceType::PROPERTY, "购物中心", &properties[8]),
Space(SpaceType::JAIL, "监狱"),
Space(SpaceType::PROPERTY, "娱乐城", &properties[9]),
Space(SpaceType::PROPERTY, "住宅区", &properties[10]),
Space(SpaceType::FREE_PARKING, "免费停车"),
Space(SpaceType::PROPERTY, "大学城", &properties[11]),
Space(SpaceType::PROPERTY, "医疗中心", &properties[12]),
Space(SpaceType::GO_TO_JAIL, "去监狱"),
Space(SpaceType::PROPERTY, "体育场馆", &properties[13]),
Space(SpaceType::PROPERTY, "会展中心", &properties[14]),
Space(SpaceType::FREE_PARKING, "免费停车"),
Space(SpaceType::PROPERTY, "豪华酒店", &properties[15]),
Space(SpaceType::PROPERTY, "国际机场", &properties[16]),
Space(SpaceType::JAIL, "监狱"),
Space(SpaceType::PROPERTY, "中央车站", &properties[17]),
Space(SpaceType::PROPERTY, "港口", &properties[18]),
Space(SpaceType::FREE_PARKING, "免费停车"),
Space(SpaceType::PROPERTY, "能源基地", &properties[19]),
Space(SpaceType::PROPERTY, "太空中心", &properties[20])
};
// 明确监狱位置(根据棋盘设计,通常是第一个监狱格子)
jailPosition = 4; // 索引4对应的是第一个监狱格子
// 添加玩家
int playerCount = 0;
while (playerCount < 2 || playerCount > 4) {
cout << "请输入玩家数量 (2-4): ";
cin >> playerCount;
if (playerCount < 2 || playerCount > 4) {
cout << "玩家数量必须在2-4之间!" << endl;
}
}
cin.ignore(); // 忽略换行符
for (int i = 0; i < playerCount; ++i) {
string playerName;
cout << "请输入玩家 " << (i + 1) << " 的名字: ";
getline(cin, playerName);
players.push_back(Player(playerName));
}
cout << "\n===== 大富翁游戏开始! =====" << endl;
}
// 掷骰子
int rollDice() {
return rand() % 6 + 1;
}
// 处理玩家回合
void handlePlayerTurn() {
Player& currentPlayer = players[currentPlayerIndex];
cout << "\n--- " << currentPlayer.name << " 的回合 ---" << endl;
cout << "当前位置: " << board[currentPlayer.position].name << endl;
cout << "当前金钱: " << currentPlayer.money << endl;
// 处理监狱状态
if (currentPlayer.isInJail) {
cout << currentPlayer.name << " 正在监狱中!" << endl;
if (currentPlayer.jailTurnsLeft > 0) {
cout << "剩余监狱回合: " << currentPlayer.jailTurnsLeft << endl;
currentPlayer.jailTurnsLeft--;
// 尝试掷双数出狱
int dice1 = rollDice();
int dice2 = rollDice();
cout << "尝试掷双数出狱: " << dice1 << " + " << dice2 << endl;
if (dice1 == dice2) {
cout << "成功掷出双数!" << currentPlayer.name << " 出狱了!" << endl;
currentPlayer.isInJail = false;
// 移动玩家
int steps = dice1 + dice2;
cout << "移动 " << steps << " 步" << endl;
currentPlayer.move(steps, board.size());
handleSpace(currentPlayer);
} else {
cout << "未能掷出双数,继续留在监狱!" << endl;
if (currentPlayer.jailTurnsLeft == 0) {
cout << "监狱回合结束," << currentPlayer.name << " 出狱了!" << endl;
currentPlayer.isInJail = false;
}
}
} else {
cout << "监狱回合结束," << currentPlayer.name << " 出狱了!" << endl;
currentPlayer.isInJail = false;
}
} else {
// 正常回合
char choice;
cout << "按 'r' 掷骰子: ";
cin >> choice;
cin.ignore(); // 忽略换行符
if (choice == 'r') {
int dice1 = rollDice();
int dice2 = rollDice();
int steps = dice1 + dice2;
cout << "掷出: " << dice1 << " + " << dice2 << " = " << steps << endl;
// 移动玩家
currentPlayer.move(steps, board.size());
cout << currentPlayer.name << " 移动到: " << board[currentPlayer.position].name << endl;
// 处理玩家所在格子
handleSpace(currentPlayer);
} else {
cout << "无效选择,跳过回合!" << endl;
}
}
// 检查玩家是否破产
if (currentPlayer.money < 0) {
cout << currentPlayer.name << " 破产了!" << endl;
// 释放玩家所有地产
for (auto& prop : properties) {
if (prop.isOwned && prop.ownerId == currentPlayerIndex) {
prop.isOwned = false;
prop.ownerId = -1;
}
}
players.erase(players.begin() + currentPlayerIndex);
if (players.size() == 1) {
cout << "\n===== 游戏结束! =====" << endl;
cout << players[0].name << " 获胜!" << endl;
gameOver = true;
return;
}
// 调整当前玩家索引
if (currentPlayerIndex >= players.size()) {
currentPlayerIndex = 0;
}
return;
}
// 切换到下一个玩家
currentPlayerIndex = (currentPlayerIndex + 1) % players.size();
}
// 处理玩家所在格子
void handleSpace(Player& player) {
Space& currentSpace = board[player.position];
switch (currentSpace.type) {
case SpaceType::GO:
cout << "经过起点,获得200元!" << endl;
player.money += 200;
break;
case SpaceType::PROPERTY: {
Property* prop = currentSpace.property;
if (!prop->isOwned) {
// 地产未被购买
cout << "地产 " << prop->name << " 未被购买!" << endl;
cout << "价格: " << prop->price << " 元,租金: " << prop->rent << " 元" << endl;
char buyChoice;
cout << "是否购买? (y/n): ";
cin >> buyChoice;
cin.ignore(); // 忽略换行符
if (buyChoice == 'y' || buyChoice == 'Y') {
if (player.money >= prop->price) {
player.money -= prop->price;
prop->isOwned = true;
prop->ownerId = currentPlayerIndex;
cout << player.name << " 购买了 " << prop->name << "!" << endl;
} else {
cout << "金钱不足,无法购买!" << endl;
}
}
} else if (prop->ownerId != currentPlayerIndex) {
// 地产已被其他玩家购买,支付租金
cout << "这是 " << players[prop->ownerId].name << " 的地产!" << endl;
cout << "支付租金: " << prop->rent << " 元" << endl;
int rentToPay = min(prop->rent, player.money);
player.money -= rentToPay;
players[prop->ownerId].money += rentToPay;
cout << player.name << " 支付给 " << players[prop->ownerId].name << " " << rentToPay << " 元!" << endl;
} else {
// 自己的地产
cout << "这是你的地产!" << endl;
}
break;
}
case SpaceType::JAIL:
cout << "访问监狱,无需支付!" << endl;
break;
case SpaceType::FREE_PARKING:
cout << "休息时间,享受免费停车!" << endl;
break;
case SpaceType::GO_TO_JAIL:
cout << "你被送进监狱!" << endl;
player.isInJail = true;
player.jailTurnsLeft = 3;
// 移动玩家到监狱位置
player.position = jailPosition;
cout << player.name << " 被移动到监狱!" << endl;
break;
}
}
// 显示游戏状态
void displayGameStatus() {
cout << "\n===== 游戏状态 =====" << endl;
for (const auto& player : players) {
cout << player.name << " - 金钱: " << player.money
<< ", 位置: " << board[player.position].name;
if (player.isInJail) {
cout << ", 监狱中,剩余回合: " << player.jailTurnsLeft;
}
cout << endl;
}
cout << "\n--- 地产状态 ---" << endl;
for (const auto& prop : properties) {
cout << prop.name << " - 价格: " << prop.price
<< ", 租金: " << prop.rent;
if (prop.isOwned) {
cout << ", 所有者: " << players[prop.ownerId].name;
} else {
cout << ", 未被购买";
}
cout << endl;
}
}
// 运行游戏
void run() {
while (!gameOver) {
displayGameStatus();
handlePlayerTurn();
// 按任意键继续
cout << "\n按 Enter 键继续...";
cin.ignore();
}
}
};
int main() {
MonopolyGame game;
game.run();
return 0;
}
623

被折叠的 条评论
为什么被折叠?



