So for my project I'm working on, we have to simulate a game of Sorry. There are 1-4 players who are each represented by a color. Player 1 is always BLUE, 2 is YELLOW, 3 is GREEN and the 4th player is RED. Each color has it's own unique start/home squares that are determined through reading a separate board file.
The problem that I'm having is with the board file. It's guaranteed to always be formatted correctly, but not always in the same order. The order for the start/home square lines is always <COLOR> <TYPE> <NUMBER>. My original plan was to get the input from the board, and depending on what COLOR was I would assign those values to the appropriate character number.
My code goes something like this:
// Thisis where I start by initializing all the players
for(int i=0; i < MAXPLAYERS ; i++){
cout << i << endl;
initPlayer(board, game, i);}
void initPlayer (ifstream& board, Game& game, int i){
string color;
for(int numTypes =0; numTypes < 2; numTypes++){ // I have to loop this twice per player, since each player has both a start square and a home square
getline(board, color, ' ');
if(color== "BLUE"){
cout << color;
home_endSquare(board, game, 0);}
else if(color == "RED"){
cout << color;
home_endSquare(board, game, 3);}
else if(color == "GREEN"){
cout << color;
home_endSquare(board, game, 2;}
else if(color == "YELLOW"){
cout << color;
home_endSquare(board, game, 1;}
}
void home_endSquare(ifstream &board, Game& game, int numPlayer){
string type;
getline(board, type, ' ');
if(type== "HOME"){
getline(board, type, '\n');
game.players[numPlayer].p_homeSquare= atoi(type.c_str());
cout << " " << game.players[numPlayer].p_homeSquare << endl;}
else if(type== "START"){
getline(board, type, '\n');
game.players[numPlayer].p_homeSquare= atoi(type.c_str());
cout << " " << game.players[numPlayer].p_homeSquare << endl; }
The game.players[numPlayer] part is what is confusing me. It should be taking the value being passed to it (which changes depending on what color is read) and assigning that player the correct values for start/home. The problem is instead it's assigning the first player (#0) with the first values it reads, the second player (#1) with the second, and so on.
If anyone has any suggestions it would be greatly appreciated.
Edit: Example output:
0 <- Player Number
GREEN 2 <- This should be Blue, not Green
GREEN 4
1 <- Player Number
RED 17
RED 19
2 <- Player Number
BLUE 32
BLUE 34
3 <- Player Number
YELLOW 47
YELLOW 49
It's worth noting that the output is always in the same order as the deck input.