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.

My code goes something like this:

Try giving a small but complete sample of actual code that illustrates your problem, rather than "something like this" which may actually be different. Include a sample input file as well.

At the moment, we can't guess the cause of your problem as you are describing what you think the code does and what it is doing wrong. Whereas the actual problem is in actual code that behaves differently from your expectations when given an input file that you haven not shown here.

Try giving a small but complete sample of actual code that illustrates your problem, rather than "something like this" which may actually be different. Include a sample input file as well.

At the moment, we can't guess the cause of your problem as you are describing what you think the code does and what it is doing wrong. Whereas the actual problem is in actual code that behaves differently from your expectations when given an input file that you haven not shown here.

Actually that IS my code, exactly as I have it typed up in my p3.cpp. Here's an example of the sample board we're given

GREEN HOME 2
GREEN START 4
RED HOME 17
RED START 19
BLUE HOME 32
BLUE START 34
YELLOW HOME 47
YELLOW START 49

You're the one who provided partial code with a description "goes something like this", and then didn't explain why the output you get does not correspond to the input.

I fail to see how your input;

GREEN HOME 2
GREEN START 4
RED HOME 17
RED START 19
BLUE HOME 32
BLUE START 34
YELLOW HOME 47
YELLOW START 49

should not yield the output you're getting (with your comments from your original post).

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 seems logical to me that your code - incomplete as it is - and an input file with first line "GREEN HOME 2" and second line "GREEN START 4" would yield output of "GREEN 2" followed by "GREEN 4". However you expect the first line of output (for player 0) to be "BLUE 2".

If the rules of the game suggest that, then fine. But you have not shown any code that would make that happen.

you seem to be using an enum or struct for this, is that true?

also it seems like you need some sort of input. most projects like that require file input with fstreams. if you want help on certain parts, you need to pretty much show everything youve got so far so we can follow from start to finish rather than visualize what you aren't showing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.