简体   繁体   English

剪刀石头的C ++代码,请咨询

[英]C++ code for Rock-Paper-Scissors, need advice please

This is my first post here, so sorry if I'm not in the right location or something like that. 这是我在这里的第一篇文章,对不起,如果我不在正确的位置或类似的位置。 I just wanted some feedback on a program I created. 我只是想对我创建的程序提供一些反馈。 I've been learning C++ for two months now (self-taught), and this is the first game I've made on my own (I made the program that has a computer play a game of rock paper scissors against you). 我学习C ++已有两个月了(自学成才),这是我自己开发的第一款游戏(我编写了带有计算机的程序,与您抗衡)。 My dream is to become a video game programmer, so please be gentle, haha. 我的梦想是成为一名视频游戏程序员,所以请保持谦虚,哈哈。 I've compiled and executed the program without a problem, and I tested it for bugs and it ran like I intended. 我已经毫无问题地编译并执行了该程序,并且对其进行了错误测试,并按预期运行。 My questions is, is my code understandable? 我的问题是,我的代码可以理解吗? Do I program like professional game programmers, or is my code sloppy? 我像专业游戏程序员一样编程,还是我的代码草率? If it is sloppy, can you recommend how I fix it? 如果它草率,您能推荐我如何解决? Thanks in advance for any advice you can offer! 在此先感谢您提供的任何建议! My code is below. 我的代码如下。 (Again, sorry if I posted incorrectly or in the wrong location!) (再次,抱歉,如果我张贴错误或在错误的位置!)

#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;
}

This should really belong on code review stack, but let me give you my thoughts. 这确实应该属于代码审查堆栈,但是让我给您我的想法。

First thing I would fix is using "magic numbers" for moves (1,2,3). 我要解决的第一件事是对移动(1,2,3)使用“幻数”。 I bet you cant off hand remember which is which, and that will lead to bugs (There are lot more reasons to avoid magic numbers. I would recommend an enum 我敢打赌,您不能忘了哪个是哪个,那会导致错误(还有很多避免使用幻数的原因。我建议您使用一个枚举

enum Move{
    ROCK = 1,
    PAPER,
    SCISSORS
};

Then you can do 那你可以做

myMove == ROCK

Then I would move the output out of the big if . 然后我会将输出移出大if This way, you can make some intelligent printing rather than give an output for each permutation. 这样,您可以进行一些智能打印,而不必为每个排列提供输出。

std::cout << "\nTie, you both threw "<<text_for[move]<<".\n\n";

Instead of 3 separate lines. 而不是3条单独的线。 This makes it easier to change your outputs. 这样可以更轻松地更改输出。

The basic principles in this answer are to a) Avoid Magic Numbers and b) DRY (don't repeat your self), but these are general programming lessons, not specific to C++ or game development. 此答案的基本原则是:a)避免使用幻数和b)避免DRY(不要重蹈覆辙),但这是一般的编程课程,并不特定于C ++或游戏开发。

I would suggest you to use more OOPS concepts, if you wanna learn C++ and Game programming. 如果您想学习C ++和游戏编程,我建议您使用更多的OOPS概念。
Overall your code style is sequential and if else based. 总体而言,您的代码样式是顺序的,并且是基于其他样式的。 [C Style] [C风格]

The next step for you would be to visualizes different classes and their relationships for RPS game. 下一步是为RPS游戏可视化不同的类及其之间的关系。

Make the design easily extensible, eg if you want to add another move to Rock-Paper-Scissor--Water.. 使设计易于扩展,例如,如果您想对“石头剪刀布-水”添加新的动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM