2014-02-19 31 views
0

這是我的第一篇文章,很抱歉,如果我不在正確的位置或類似的東西。我只想對我創建的程序提供一些反饋。我已經學了兩個月的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; 
} 
+8

這可能比在http://codereview.stackexchange.com/ SO更好。 –

+2

這是可以理解的,但如果你使用函數做所有事情,那麼它會更清晰,可以理解。你在main函數中寫了所有的東西,爲不同的場景創建不同的函數並在main()中調用它們。 – Emu

+1

我可以告訴你兩件事情,至少:沒有'使用名稱空間標準;'而沒有'系統(「暫停」);'。這很令人耳目一新! –

回答

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)避免幻數和b)DRY(不要重複你自己),但這些都是一般的編程經驗,而不是專門針對C++或遊戲的開發。

0

我會建議你使用更多的OOPS概念,如果你想學C++和遊戲編程。
總的來說,你的代碼風格是順序的,如果其他的基礎。 [C風格]

下一步你將可視化不同的類和它們的用於R-P-S遊戲關係。

使設計易於擴展,例如,如果你想添加另一個移動到岩石剪刀 - 水..