這是我的第一篇文章,很抱歉,如果我不在正確的位置或類似的東西。我只想對我創建的程序提供一些反饋。我已經學了兩個月的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
我可以告訴你兩件事情,至少:沒有'使用名稱空間標準;'而沒有'系統(「暫停」);'。這很令人耳目一新! –