I\xe2\x80\x99m 目前正在用 C++ 开发一个基于文本的游戏,有 2 名玩家。两个玩家都具有相同的函数和相同的变量集,但是将玩家操作分为两个不同的类会更优化吗?
\nI\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}\nRun Code Online (Sandbox Code Playgroud)\n检查参数 \xe2\x80\x9cplayer\xe2\x80\x9d 并相应地更改统计信息。
\n