0

So I am making a rock paper scissors game and the following code is the referee class that checks all of the logic in the objects move member variable and decides who wins that round (out of 5) and adds 1 to their win counter (p1ctr/p2ctr)

#include "Referee.h"

Referee::Referee() {}

Player* Referee::refGame(Player* p1, Player* p2) {

    //char p1Move;
    //char p2Move;
    int p1Ctr = 0;
    int p2Ctr = 0;

    // refresh players memory - for the players that move in a particular order
    p1->refresher();
    p2->refresher();

    // set the players move variable to their move
    p1->makeMove();
    p2->makeMove();

    // return the players move and assign it to the corresponding players move
    //p1Move = p1->getMove();
    //p2Move = p2->getMove();

    for (int i = 0; i < 5; i++) {

        if (p1->getMove() == 'R' && p2->getMove() == 'P') {
            p2Ctr++;
        } else if (p1->getMove() == 'R' && p2->getMove() == 'S') {
            p1Ctr++;
        } else if (p1->getMove() == 'P' && p2->getMove() == 'R') {
            p1Ctr++;
        } else if (p1->getMove() == 'P' && p2->getMove() == 'S') {
            p2Ctr++;
        } else if (p1->getMove() == 'S' && p2->getMove() == 'R') {
            p2Ctr++;
        } else if (p1->getMove() == 'S' && p2->getMove() == 'P') {
            p1Ctr++;
        } else {
            p1Ctr++;
            p2Ctr++;
        }

        // std::cout << "player 1 count: " << p1Ctr << endl;
        // std::cout << "player 2 count: " << p2Ctr << endl;
    }
    
    if (p1Ctr > p2Ctr) {
        return p1;
    } else if (p1Ctr < p2Ctr) {
        return p2;
    } else {
        return p1;
    }
}

now what i am confused about is when i had the player win counters created like this in the code:

int p1Ctr;
int p2Ctr;

it returned the following when i printed them out to test it (there is 5 rounds):

player 1 count: 1 / player 2 count: 1025943249

player 1 count: 2 / player 2 count: 1025943250

player 1 count: 3 / player 2 count: 1025943251

player 1 count: 4 / player 2 count: 1025943252

player 1 count: 5 / player 2 count: 1025943253

this was resolved by fixing the variables declaration to this:

int p1Ctr = 0;
int p2Ctr = 0;

why is this? i know it is to do with the address of the variable and or something rather, no?

thanks

DoCkane
  • 33
  • 7
  • 1
    It undefined behavior to use a variable before it has been initialized. – drescherjm Mar 21 '22 at 02:32
  • 1
    It should be explained at the very beginning of your textbook or whatever, when variables are first introduced, that not initializing or setting them to a value will result in the variable having an indeterminate value and that reading that value will cause undefined behavior. – user17732522 Mar 21 '22 at 02:33
  • See [this](https://stackoverflow.com/a/1597426/12326283) answer for more info – gmdev Mar 21 '22 at 02:35
  • 2
    Does this answer your question? [What happens to a declared, uninitialized variable in C? Does it have a value?](https://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value) – gmdev Mar 21 '22 at 02:36
  • thanks for the reply guys i knew it was something to do with that i had just forgotten the specifics – DoCkane Mar 21 '22 at 02:52

0 Answers0