[英]C++ code for Rock-Paper-Scissors, need advice please
這是我在這里的第一篇文章,對不起,如果我不在正確的位置或類似的位置。 我只是想對我創建的程序提供一些反饋。 我學習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)使用“幻數”。 我敢打賭,您不能忘了哪個是哪個,那會導致錯誤(還有很多避免使用幻數的原因。我建議您使用一個枚舉
enum Move{
ROCK = 1,
PAPER,
SCISSORS
};
那你可以做
myMove == ROCK
然后我會將輸出移出大if
。 這樣,您可以進行一些智能打印,而不必為每個排列提供輸出。
std::cout << "\nTie, you both threw "<<text_for[move]<<".\n\n";
而不是3條單獨的線。 這樣可以更輕松地更改輸出。
此答案的基本原則是:a)避免使用幻數和b)避免DRY(不要重蹈覆轍),但這是一般的編程課程,並不特定於C ++或游戲開發。
如果您想學習C ++和游戲編程,我建議您使用更多的OOPS概念。
總體而言,您的代碼樣式是順序的,並且是基於其他樣式的。 [C風格]
下一步是為RPS游戲可視化不同的類及其之間的關系。
使設計易於擴展,例如,如果您想對“石頭剪刀布-水”添加新的動作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.