大佬帮我改一下代码

#include
#include
#include
#include <Windows.h>
#include
#include

using namespace std;

class Item {
public:
std::string name;
int price;
int quantity;
int effectValue; // 用于表示一些道具的特定效果值,比如伤害加成等

Item(const std::string& n, int p, int q, int ef = 0) : name(n), price(p), quantity(q), effectValue(ef) {}

};

class Player {
private:
std::string playerName;
int health;
std::vector items;
double money;
int handcuffsUsed;

public:
Player(const std::string& name, int h, double m) : playerName(name), health(h), money(m), handcuffsUsed(0) {}

// 添加道具
void addItem(const Item& item) {
    for (auto& it : items) {
        if (it.name == item.name) {
            it.quantity += item.quantity;
            return;
        }
    }
    items.push_back(item);
}

// 使用道具
void useItem(const std::string& itemName) {
    for (auto& it : items) {
        if (it.name == itemName) {
            if (it.quantity > 0) {
                it.quantity--;
                if (itemName == "Potion") {
                    // 恢复生命值的逻辑
                    health += 3;
                    if (health > 6) health = 6;
                    cout << playerName << " used a Potion and regained 3 health." << endl;
                }
                else if (itemName == "Enhanced Knife") {
                    // 增强小刀效果的逻辑
                    cout << playerName << " used an Enhanced Knife. Next attack damage increased by 2." << endl;
                }
                // 其他道具的使用逻辑可以继续添加
            }
            else {
                cout << "No more " << itemName << " left to use." << endl;
            }
            return;
        }
    }
    cout << "Item not found: " << itemName << endl;
}

// 受到伤害
void takeDamage(int damage) {
    health -= damage;
    if (health < 0) health = 0;
    cout << playerName << " takes " << damage << " damage. Remaining health: " << health << endl;
}

// 获取玩家信息
void showInfo() {
    cout << "Player: " << playerName << endl;
    cout << "Health: " << health << endl;
    cout << "Money: $" << money << endl;
    cout << "Items: ";
    for (const auto& it : items) {
        cout << it.name << ": " << it.quantity << " ";
    }
    cout << endl;
}

// 购买道具
void buyItem(const Item& itemToBuy) {
    if (money >= itemToBuy.price) {
        money -= itemToBuy.price;
        addItem(itemToBuy);
        cout << playerName << " bought " << itemToBuy.name << endl;
    }
    else {
        cout << "Not enough money to buy " << itemToBuy.name << endl;
    }
}

};

// 生成随机数
int Rand(int x, int y) {
int A = rand(), B = rand();
return A * 1LL * B % (y - x + 1) + x;
}

class Shop {
private:
std::vector availableItems;

public:
Shop() {
// 初始化商店中的道具
availableItems.push_back(Item(“Potion”, 200, 5));
availableItems.push_back(Item(“Enhanced Knife”, 800, 1, 2));
availableItems.push_back(Item(“Shield”, 1000, 1, 3)); // 盾牌可以减少一定伤害
}

// 显示商店中的道具
void showItems() {
    cout << "Shop Items:" << endl;
    for (size_t i = 0; i < availableItems.size(); i++) {
        cout << i + 1 << ". " << availableItems[i].name << " - Price: $" << availableItems[i].price
            << " - Effect: " << (availableItems[i].effectValue > 0? "Increases damage by " + std::to_string(availableItems[i].effectValue) : "Restores health") << endl;
    }
}

// 获取指定索引的道具
Item getItem(int index) {
    if (index >= 1 && index <= static_cast<int>(availableItems.size())) {
        return availableItems[index - 1];
    }
    return Item("", 0, 0); // 返回一个无效的道具
}

};

// 游戏回合函数
void gameTurn(Player& player1, Player& player2, int& bullets, int& emptyBullets, int& turn, Shop& shop) {
player1.showInfo();
player2.showInfo();

int choice;
cout << player1.playerName << ", it's your turn. Choose an action:" << endl;
cout << "1. Shoot at " << player2.playerName << endl;
cout << "2. Shoot at yourself" << endl;
cout << "3. Use an item" << endl;
cout << "4. Visit Shop" << endl;
cin >> choice;

if (choice == 1) {
    // 向对方开枪的逻辑,与之前类似,略...
}
else if (choice == 2) {
    // 向自己开枪的逻辑,与之前类似,略...
}
else if (choice == 3) {
    cout << "Select an item to use: ";
    string itemToUse;
    cin >> itemToUse;
    player1.useItem(itemToUse);
}
else if (choice == 4) {
    shop.showItems();
    int itemChoice;
    cout << "Select an item to buy (enter item number): ";
    cin >> itemChoice;
    Item itemToBuy = shop.getItem(itemChoice);
    if (itemToBuy.name!= "") {
        player1.buyItem(itemToBuy);
    }
}

// 检查游戏是否结束
if (player1.health <= 0 || player2.health <= 0) {
    // 游戏结束的逻辑,与之前类似,略...
}

// 切换回合
turn++;

}

int main() {
srand(static_cast(time(nullptr)));

// 创建玩家
Player playerA("Player A", 6, 1500.0);
Player playerB("Player B", 6, 1500.0);

// 初始化子弹和空弹数量
int bullets = Rand(1, 4);
int emptyBullets = Rand(1, 4);
int turn = 0;

// 创建商店
Shop shop;

// 游戏循环
while (true) {
    if (turn % 2 == 0) {
        gameTurn(playerA, playerB, bullets, emptyBullets, turn, shop);
    }
    else {
        gameTurn(playerB, playerA, bullets, emptyBullets, turn, shop);
    }
}

return 0;

}

3 个赞

:leafy_green:就多练

4 个赞

我真的你们看过发帖规范吗啊喂,不会我还要把那个陈年老帖发出来置顶吧

4 个赞

搞得像你会一样

2 个赞

Shop 类构造函数
Shop() {
// 初始化商店中的道具
availableItems.push_back(Item(“Potion”, 200, 5));
availableItems.push_back(Item(“Enhanced Knife”, 800, 1, 2));
availableItems.push_back(Item(“Shield”, 1000, 1, 3)); // 盾牌可以减少一定伤害
}

游戏结束逻辑
// 检查游戏是否结束
if (player1.health <= 0) {
cout << player1.playerName << " has been defeated!" << endl;
break; // 退出循环
}
if (player2.health <= 0) {
cout << player2.playerName << " has been defeated!" << endl;
break; // 退出循环
}

完整的 gameTurn 函数(部分)
void gameTurn(Player& player1, Player& player2, int& bullets, int& emptyBullets, int& turn, Shop& shop) {
// …(之前的代码)

// 检查游戏是否结束  
if (player1.health <= 0 || player2.health <= 0) {  
    // 游戏结束的逻辑  
    if (player1.health <= 0) {  
        cout << player1.playerName << " has been defeated!" << endl;  
    } else {  
        cout << player2.playerName << " has been defeated!" << endl;  
    }  
    return; // 退出函数  
}  

// ...(之后的代码)  

}

完整的 main 函数
int main() {
// …(之前的代码)

// 游戏循环,这里假设有一个最大回合数作为退出条件  
const int MAX_TURNS = 10;  
while (turn < MAX_TURNS) {  
    if (turn % 2 == 0) {  
        gameTurn(playerA, playerB, bullets, emptyBullets, turn, shop);  
    } else {  
        gameTurn(playerB, playerA, bullets, emptyBullets, turn, shop);  
    }  
}  

cout << "Game over!" << endl;  

return 0;  

}

5 个赞

是哈,我不会

4 个赞

别人的代码

1 个赞

嗯!

2 个赞

嗯!

2 个赞

当然是我的,网上搜不到

3 个赞

有编译错误吗?

1 个赞

呵!

3 个赞

呵!

1 个赞

sosososososososososososososososososo

2 个赞

蒟蒻是巨弱的意思

1 个赞

有自知之明还是不错的

3 个赞

sosososososoosososoosososoososoos

1 个赞

别吵了

1 个赞

要吗

1 个赞

不用

3 个赞