这是我的第一篇文章,很抱歉,如果我不在正确的位置或类似的东西。我只想对我创建的程序提供一些反馈。我已经学了两个月的C++(自学),这是我自己做的第一款游戏(我制作的程序有一台电脑玩石头剪刀游戏对你)。我的梦想是成为一名电子游戏程序员,所以请温和,哈哈。我编译并执行该程序时没有问题,我测试了它的错误,并按照我的意图运行。我的问题是,我的代码是否可以理解?我是否像专业游戏程序员一样编程,还是我的代码马虎?如果它是马虎,你能推荐我如何解决它?预先感谢您提供的任何建议!我的代码如下。 (同样,对不起,如果我贴不正确或错误的位置!)Rock-Paper-Scissors的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;
}
这可能比在http://codereview.stackexchange.com/ SO更好。 –
这是可以理解的,但如果你使用函数做所有事情,那么它会更清晰,可以理解。你在main函数中写了所有的东西,为不同的场景创建不同的函数并在main()中调用它们。 – Emu
我可以告诉你两件事情,至少:没有'使用名称空间标准;'而没有'系统(“暂停”);'。这很令人耳目一新! –