这是我在这里的第一篇文章,很抱歉,如果我不在合适的位置或类似的地方。我只想对我创建的程序提出一些反馈意见。我已经学习了两个月的C ++(自学成才),这是我自己制作的第一款游戏(我制作的程序中有一台电脑玩石头剪刀对你游戏)。我的梦想是成为一名视频游戏程序员,所以请保持温和,哈哈。我编译并执行了程序没有问题,我测试了它的bug,它像我想的那样运行。我的问题是,我的代码可以理解吗?我是否像专业游戏程序员一样编程,还是我的代码马虎?如果它是草率的,你能推荐我如何解决它?提前感谢您提供的任何建议!我的代码如下。 (再次,抱歉,如果我发布错误或位置错误!)
#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;
}
答案 0 :(得分:1)
这应该属于代码审查堆栈,但让我告诉你我的想法。
我要解决的第一件事是使用“魔术数字”进行移动(1,2,3)。我打赌你不能忘记哪个是哪个,哪个会导致错误(还有很多理由可以避免使用魔法数字。我会推荐一个枚举
enum Move{
ROCK = 1,
PAPER,
SCISSORS
};
然后你可以做
myMove == ROCK
然后我会将输出移出大if
。这样,您可以进行一些智能打印,而不是为每个排列提供输出。
std::cout << "\nTie, you both threw "<<text_for[move]<<".\n\n";
而不是3个单独的行。这样可以更轻松地更改输出。
这个答案的基本原则是:a)避免使用Magic Numbers和b)DRY(不要重复自己),但这些是一般编程课程,不是特定于C ++或游戏开发。
答案 1 :(得分:0)
如果您想学习C ++和游戏编程,我建议您使用更多OOPS概念。
总体而言,您的代码风格是顺序的,如果是其他的话[C风格]
下一步将是为R-P-S游戏可视化不同的类及其关系。
使设计易于扩展,例如,如果你想为Rock-Paper-Scissor - Water添加另一个动作......