2014-02-19 114 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游戏关系。

使设计易于扩展,例如,如果你想添加另一个移动到岩石剪刀 - 水..