-1

I am new to C++ and wrote this code. It is a rock, paper and scissors game. But the problem is that when for example computer and player both got 'rock' the computer says 1 - 0 or 0 - 1. Does anyone know how to fix that?

I also want to add a scoreboard when I make more rounds, but I don't know how to do that in this code. Can anyone help me?

thanks!

#include <stdio.h>
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int random ((rand() % 3) + 1);


string steen;
string papier;

int main(int argc, char **argv)
{
    srand(time(0)); 
    cout << "Welkom bij steen papier schaar: druk op enter om verder te gaan.";
    cin.get();
    cout <<"You are playing against the computer. your choices are 'rock' 'paper' and 'scissors'. good luck!\n\n";
    cout << "Round 1\n";
    cout << "rock paper or scissors: ";
    cin >> steen;  


    if((rand() % 3) + 1 == 1) { cout << "computer: steen";  }
    else if ((rand() % 3) + 1 == 2) { cout << "computer: papier"; }
    else if ((rand() % 3) + 1 == 3) { cout << "computer: schaar";} 

         if (random == 1 and steen == "steen") { cout << "\n1 - 1\n"; } 
    else if (random == 2 and steen == "papier") { cout << "\n1 - 1\n"; } 
    else if (random == 3 and steen == "schaar") { cout << "\n1 - 1\n"; } 
    else if (random == 1 and steen == "papier") { cout << "\n 1 - 0 \n"; } 
    else if (random == 1 and steen == "schaar") { cout << "\n0 - 1\n"; } 
    else if (random == 2 and steen == "steen") { cout << "\n0 - 1\n"; } 
    else if (random == 2 and steen == "schaar") { cout << "\n1 - 0\n"; } 
    else if (random == 3 and steen == "steen") { cout << "\n1 - 0\n"; } 
    else if (random == 3 and steen == "papier") { cout << "\n0 - 1\n"; } 
    else { cout << "syntax error\n\n" ; return 0; }



    return 0;
}
Usus woa
  • 43
  • 6
  • 1
    The value assigned to `random` is unrelated to the "computer: X" move you print. Basically, you throw two dice. You print the move based on what's on one dice, but you decide the winner based on the other. – Igor Tandetnik Jun 06 '20 at 13:52
  • 1
    In fact, you throw four dice; you decide the winner based on the first, and you (try to) print the move based on the other three. It's quite possible that you won't print any computer move at all. – Igor Tandetnik Jun 06 '20 at 13:53
  • You are also calling `rand()` before calling `srand()`, because the variable `random` is being initialized before calling `srand()`. I suggest you don't initialize the variable `random`, but assign it a value after calling `srand()`. – Andreas Wenzel Jun 06 '20 at 13:56
  • Just as a side note: It is normally considered bad programming practice to use global variables, especially if you could use a local variable just as well. – Andreas Wenzel Jun 06 '20 at 14:06
  • Just as a side note: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/12149471) – Andreas Wenzel Jun 06 '20 at 14:09
  • Just as a side note: Your program would be easier for other people to understand if you used English variable names and input/output. – Andreas Wenzel Jun 06 '20 at 14:11

2 Answers2

3

As has already been pointed in the comments section, you are generating four random numbers instead of one. One of these calls to rand() even occurs before srand() is called, because a global variable is initialized with the return value of rand().

I also want to add a scoreboard when I make more rounds, but I don't know how to do that in this code. Can anyone help me?

You need to add two counter variables for the score of the player and computer and also to run your program in a loop, with one iteration per round. I have made a code example for you in which I have also rewritten major parts of your program:

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cctype>

int main( void )
{
    enum
    {
        DRAW = 0,
        WIN = 1,
        LOSE = 2
    };
    constexpr int NUM_SHAPES = 3;
    const char *shapes[NUM_SHAPES] = { "rock", "paper", "scissors" };
    constexpr char shape_relationships[NUM_SHAPES][NUM_SHAPES] =
    {
        //rock draws against rock, loses against paper and wins against scissors
        DRAW, LOSE, WIN,

        //paper wins against rock, draws against paper and loses against scissors
        WIN, DRAW, LOSE,

        //scissors loses against rock, wins against paper and draws against scissors
        LOSE, WIN, DRAW
    };

    int round_num = 1;
    int score_player = 0;
    int score_computer = 0;
    int c;

    std::srand( (unsigned int)std::time( NULL ) );

    std::cout << "You are playing against the computer. your choices are 'rock' 'paper' and 'scissors'. Good luck!\n\n";

    do
    {
        std::string answer_string;
        int answer_num;
        int random;

        std::cout << "Round " << round_num << "\n";

    invalid_input_1:
        std::cout << "rock paper or scissors: ";
        std::cin >> answer_string;

        //search through shapes array to find a match
        answer_num = 0;
        for (;;)
        {
            if ( answer_num == NUM_SHAPES )
            {
                //we searched all permissible answers and did not find a match
                std::cout << "Invalid input!\n";
                goto invalid_input_1;
            }

            //test if we have found a match
            if ( answer_string == shapes[answer_num] ) break;

            answer_num++;
        }
        //when the loop exits here, we have found a match

        //generate random number
        random = std::rand() % NUM_SHAPES;

        //print computer's choice
        std::cout << "Computer: " << shapes[random] << "\n";

        //look up player's choice and computer's choice in table to determine winner
        switch ( shape_relationships[answer_num][random] )
        {
        case DRAW:
            std::cout << "Draw.\n";
            break;
        case WIN:
            std::cout << "Player wins.\n";
            score_player++;
            break;
        default: //which must be case LOSE
            std::cout << "Computer wins.\n";
            score_computer++;
        }

        round_num++;

    invalid_input_2:
        std::cout << "The score is: " << score_player << " - " << score_computer << ". Continue playing (y/n)?";
        std::cin >> answer_string;

        c = std::tolower( static_cast<unsigned char>(answer_string[0]) );

        if ( c  != 'y' && c != 'n' )
        {
            std::cout << "Invalid_input!\n";
            goto invalid_input_2;
        }

    } while ( c == 'y' );

    return 0;
}

In the above code, I am using a table instead of writing an if statement for every possible combination. This has the advantage that your code is more flexible. For example if you want to upgrade to Rock Paper Scissors Lizard Spock, you won't need 25 if statements, but you only must update the size and contents of the table, like this:

constexpr int NUM_SHAPES = 5;
const char *shapes[NUM_SHAPES] = { "rock", "paper", "scissors", "lizard", "spock" };
constexpr char shape_relationships[NUM_SHAPES][NUM_SHAPES] =
{
    //rock
    DRAW, LOSE, WIN, WIN, LOSE,

    //paper
    WIN, DRAW, LOSE, LOSE, WIN,

    //scissors
    LOSE, WIN, DRAW, WIN, LOSE,

    //lizard
    LOSE, WIN, LOSE, DRAW, WIN,

    //spock
    WIN, LOSE, WIN, LOSE, DRAW
};
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
2

Here

 if((rand() % 3) + 1 == 1) { cout << "computer: steen";  }
    else if ((rand() % 3) + 1 == 2) { cout << "computer: papier"; }
    else if ((rand() % 3) + 1 == 3) { cout << "computer: schaar";} 

You roll 3 random numbers when actually it should be one:

int roll = (rand() % 3)+1;

if(roll == 1) { cout << "computer: steen";  }
    else if (roll == 2) { cout << "computer: papier"; }
    else if (roll == 3) { cout << "computer: schaar";} 

And then here:

if (random == 1 and steen == "steen") { //....
else if (random == 2 and steen == "papier") { //...
else if (random == 3 and steen == "schaar") { //...
else if (random == 2 and steen == "steen") { //...

Here you are using yet another random number, but it should be the same as above.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185