小编Sel*_*ine的帖子

我应该在 C++ 项目中为各个玩家单独分类吗?

I\xe2\x80\x99m 目前正在用 C++ 开发一个基于文本的游戏,有 2 名玩家。两个玩家都具有相同的函数和相同的变量集,但是将玩家操作分为两个不同的类会更优化吗?

\n

I\xe2\x80\x99m 当前运行它的方式是在函数中使用参数,让玩家区分是哪个玩家\xe2\x80\x99s。然而,里面有很多复制粘贴,而且它的条件看起来有点混乱。它有效,它\xe2\x80\x99s只是很难阅读代码。

\n

这里\xe2\x80\x99是相同函数、相同变量集的示例:

\n
    int territoryPrice = 10000;\n    \n    if (player == 1){\n        if (pOneMoney >= territoryPrice){\n            pOneMoney -= territoryPrice;\n            pOneTerritories++;\n        }\n        else if (pOneMoney < territoryPrice){\n            cout << "You don't have enough money!\\n\\n";\n        }\n    }\n    else if (player == 2) {\n        if (pTwoMoney >= territoryPrice){\n            pTwoMoney -= territoryPrice;\n            pTwoTerritories++;\n        }\n        else if (pTwoMoney < territoryPrice){\n            cout << "You don't have enough money!\\n\\n";\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

检查参数 \xe2\x80\x9cplayer\xe2\x80\x9d 并相应地更改统计信息。

\n

c++ optimization class

-2
推荐指数
1
解决办法
121
查看次数

标签 统计

c++ ×1

class ×1

optimization ×1