简体   繁体   中英

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). 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). 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 . 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. 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.

I would suggest you to use more OOPS concepts, if you wanna learn C++ and Game programming.
Overall your code style is sequential and if else based. [C Style]

The next step for you would be to visualizes different classes and their relationships for RPS game.

Make the design easily extensible, eg if you want to add another move to Rock-Paper-Scissor--Water..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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