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