last day (15 days later) » 

11:13 AM
1
A: Changing Global Variables to Pointers

Andreas WenzelI suggest that you delete the following global variables int P1_CUR_AMT = 200; int P2_CUR_AMT = 200; int P1_CUR_POS = 0; int P2_CUR_POS = 0; int currentPlayer = 1; and define the following struct game_state: struct game_state { int p1_cur_amt; int p2_cur_amt; int p1_cur_pos; ...

 
i tried modifying my code to what you said and it made an error which is "note: declared here 121 | int checkLand( struct game_state *pgs, int Money )" I changed my global variables to struct game_state { int p1_cur_amt; int p2_cur_amt; int p1_cur_pos; int p2_cur_pos; int currentPlayer; }; and add pointers to checkWin() and checkLand
 
@Gastrodia: The functions will now have access to the game state via the pointer parameter pgs, instead of via a global variable. For example, when a function receives the struct game_state *pgs parameter, they can access it like this: pgs->currentPlayer instead of typing currentPlayer to access the global variable.
In a previous comment, you said that I should "never mind" the error report of your first comment, but you seem to have now deleted that comment, but not the error report. Please clarify whether you still have this error or not.
 
these are the errors I've encountered after modifying it. error: duplicate member 'P1_CUR_POS' 13 | int P1_CUR_POS; hatdog.c: In function 'checkLand': hatdog.c:124:8: error: 'currentPlayer' undeclared (first use in this function) 124 | if(currentPlayer == 1) hatdog.c:124:8: note: each undeclared identifier is reported only once for each function it appears in hatdog.c:126:9: error: 'P1_CUR_POS' undeclared (first use in this function) 126 | P1_CUR_POS += RollDice();
If im not mistaken, I will change my global variables to struct game_state right?
 
@Gastrodia: All functions that access the previous global variables P1_CUR_AMT, P2_CUR_AMT, P1_CUR_POS, P2_CUR_POS or currentPlayer will have to be changed accept an additional parameter of type game_state *pgs, and they will instead have to access the variables via this pointer, for example by using pgs->currentPlayer.
 
if(pgs->currentPlayer == 1) { pgs->P1_CUR_POS += RollDice(); if(pgs->P1_CUR_POS == 10){ pgs->P1_CUR_POS = 0; } else if(pgs->P1_CUR_POS > 10){ P1_CUR_POS = P1_CUR_POS - 10; } choice = pgs->P1_CUR_POS; } if(pgs->currentPlayer == 2) { pgs->P2_CUR_POS += RollDice(); if(pgs->P2_CUR_POS == 10){ pgs->P2_CUR_POS = 0; } else if(pgs->P2_CUR_POS > 10) { pgs->P2_CUR_POS = P2_CUR_POS - 10; } choice = pgs->P2_CUR_POS; }
 
11:13 AM
@Gastrodia: That looks good, except that I defined the struct game_state in such a way that I made the names of all members lower case, because that is how it is normally written. So instead of writing pgs->P1_CUR_POS, you should write pgs->p1_cur_pos. If you really want to keep the names upper case, then you can of course change the definition of struct game_state to make the names of its members upper case again.
 
Yes i changed my struct game_state to uppercase. but I still encounter problems such as error: 'pgs' undeclared (first use in this function); did you mean 'gs'? 370 | if(pgs->currentPlayer > 2) pgs->currentPlayer = 1; | gs also if you have discord or any sm platform I can show you all the errors I encounter
 
@Gastrodia Is that error message from inside the function PlayGame? That function does not have a pointer pgs, because it owns the struct game_state gs, so you can access gs directly, without a pointer, like this: gs.currentPlayer. All other functions must use pgs->currentPlayer.
 
Ohh thanks now the only error is from the checkWin() function. it says error: too few arguments to function 'checkWin' 386 | i = checkWin(); | ^~~~~~~~ hatdog.c:325:5: note: declared here 325 | int checkWin( struct game_state *pgs)
 
@Gastrodia: When you call i = checkWin(); from PlayGame, you must pass the address of gs, like this: i = checkWin( &gs );
 
ohhh okay i solve it now but the problem is whenever it ask the player 1 to roll a dice after it rolls a dice it usually ask the player 2. But in this case it is always player 1
I have this code in my Play Game if(gs.currentPlayer > 2) gs.currentPlayer = 1; printf("It is Player %d turn.\n", gs.currentPlayer); printf("Press any key to roll a dice."); getch(); printf("\nPlayer %d landed", gs.currentPlayer); if(gs.currentPlayer == 1) { gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P1_CUR_AMT ); } else { gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P2_CUR_AMT ); } i = checkWin(&gs); gs.currentPlayer++;
 
11:13 AM
@Gastrodia: Did you delete the old global variables P1_CUR_AMT, P2_CUR_AMT, P1_CUR_POS, P2_CUR_POS and currentPlayer? If not, maybe one of your functions is still accessing the old global variables? If you want to be sure that they are no longer accessed, you should delete them.
 
I deleted my global variables and change it to struct game_state
 
@Gastrodia: The lines gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P1_CUR_AMT ); and gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P2_CUR_AMT ); look suspicious. You did not have them like that in your original code, did you? The gs_currentPlayer = should probably be removed in both bases.
 
I have it in my original code before modifying it. It currentPlayer = P1_CUR_AMT(P1_CUR_AMT); and currentPlayer = P2_CUR_AMT(P2_CUR_AMT)
 
@Gastrodia: Are you sure? P2_CUR_AMT(P2_CUR_AMT) does not look correct either, and I am also unable to find that in the original code that you posted.
@Gastrodia: This is what was written in your original code that you posted: P1_CUR_AMT = checkLand(P1_CUR_AMT); and P2_CUR_AMT = checkLand(P2_CUR_AMT);. Now you have writen gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P1_CUR_AMT ); and gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P2_CUR_AMT );. Your original code looks good (except for the use of the global variables), but your new code looks very wrong.
 
isnt this is the one you talking about? do { Lines(); if(currentPlayer > 2) currentPlayer = 1; printf("It is Player %d turn.\n", currentPlayer); printf("Press any key to roll a dice."); getch(); printf("\nPlayer %d landed", currentPlayer); if(currentPlayer == 1) { P1_CUR_AMT = checkLand(P1_CUR_AMT); } else { P2_CUR_AMT = checkLand(P2_CUR_AMT); }i = checkWin(); currentPlayer++;
 
11:13 AM
@Gastrodia: Yes.
 
Ohhh i see now thank you very much. Thank you for clarifying things
 
@Gastrodia: As far as I can tell, the code gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P1_CUR_AMT ); has one error: The gs.currentPlayer = should be deleted. And the code gs.currentPlayer = gs.P1_CUR_AMT = checkLand( &gs, gs.P2_CUR_AMT ); has two errors: The same error as the first one, and also P1_CUR_AMT should be changed to P2_CUR_AMT. At least that is how it was written in the original code.
 

last day (15 days later) »