gpt4 book ai didi

剪刀石头布的C++代码,需要建议

转载 作者:行者123 更新时间:2023-11-28 07:05:08 26 4
gpt4 key购买 nike

这是我在这里的第一篇文章,如果我不在正确的位置或类似的地方,我深表歉意。我只是想要一些关于我创建的程序的反馈。我已经学习C++两个月了(自学),这是我自己制作的第一款游戏(我制作了让电脑玩剪刀石头布游戏的程序)。我的梦想是成为一名电子游戏程序员,所以请温柔点,哈哈。我已经毫无问题地编译和执行了该程序,并且我测试了它是否存在错误并且它按我预期的方式运行。我的问题是,我的代码可以理解吗?我是像专业游戏程序员一样编程,还是我的代码草率?如果马虎,你能推荐我如何解决它吗?提前感谢您提供的任何建议!我的代码如下。 (再次抱歉,如果我发错或发错位置了!)

#include <iostream>
#include <cstdlib>
#include <ctime>

int main()
{
std::cout << "\tWelcome to the Rock-Paper-Scissors game!\n\n";

int maxWins;
std::cout << "Please enter the number of wins you wish to play to: ";
std::cin >> maxWins;

std::cout << "\n\n1 - Rock\n";
std::cout << "2 - Paper\n";
std::cout << "3 - Scissors\n\n";

std::cout << "Using the above menu as a reference, please input one of the numbers associated with an action.\n\n";

int myWins = 0;
int computerWins = 0;
srand(static_cast<unsigned int>(time(0))); // seed random number generator I use in while loop

while (myWins != maxWins && computerWins != maxWins)
{
int computerMove = rand() % 3 + 1; // giving the computerMove variable a random number between 1 and 3
int myMove;
std::cout << "Your move: ";
std::cin >> myMove;

if (myMove == computerMove)
{
if (myMove == 1 && computerMove == 1)
{
std::cout << "\nTie, you both threw Rock.\n\n";
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else if (myMove == 2 && computerMove == 2)
{
std::cout << "\nTie, you both threw Paper.\n\n";
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else if (myMove == 3 && computerMove == 3)
{
std::cout << "\nTie, you both threw Scissors.\n\n";
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else
{
std::cout << "\nError #1\n\n"; // catchfall
}
}
else if (myMove == 1 && computerMove == 2)
{
std::cout << "\nComputer's Paper beats your Rock.\n\n";
++computerWins;
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else if (myMove == 1 && computerMove == 3)
{
std::cout << "\nYour Rock beats computer's Scissors.\n\n";
++myWins;
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else if (myMove == 2 && computerMove == 1)
{
std::cout << "\nYour Paper beats computer's Rock.\n\n";
++myWins;
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else if (myMove == 2 && computerMove == 3)
{
std::cout << "\nComputer's Scissors beats your Paper.\n\n";
++computerWins;
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else if (myMove == 3 && computerMove == 1)
{
std::cout << "\nComputer's Rock beats your Scissors.\n\n";
++computerWins;
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else if (myMove == 3 && computerMove == 2)
{
std::cout << "\nYour Scissors beats computer's Paper.\n\n";
++myWins;
std::cout << "Your total wins: " << myWins << "\n";
std::cout << "Computer's total wins: " << computerWins << "\n\n";
}
else
{
std::cout << "\nError #2\n\n"; // catchfall
}
}

if (myWins == maxWins)
{
std::cout << "\n\nCongratulations! You won!!\n\n";
}
else if (computerWins == maxWins)
{
std::cout << "\n\nThe computer beat you. Try again!\n\n";
}
else
{
std::cout << "\n\nError #3\n\n"; // catchfall
}

return 0;
}

最佳答案

这确实应该属于代码审查堆栈,但让我告诉你我的想法。

我要解决的第一件事是对移动 (1,2,3) 使用“魔数(Magic Number)”。我敢打赌你不能立即记住哪个是哪个,这会导致错误(有更多的理由来避免魔数(Magic Number)。我会推荐一个枚举

enum Move{
ROCK = 1,
PAPER,
SCISSORS
};

然后你可以做

myMove == ROCK

然后我会将输出移出大if。这样,您可以进行一些智能打印,而不是为每个排列提供输出。

std::cout << "\nTie, you both threw "<<text_for[move]<<".\n\n";

而不是 3 行。这样可以更轻松地更改输出。

此答案中的基本原则是 a) 避免魔数(Magic Number)和 b) DRY(不要重复自己),但这些是一般编程类(class),并非特定于 C++ 或游戏开发。

关于剪刀石头布的C++代码,需要建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21871918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com