Taking turns?

Hey all, I’m a newbie both to this forum and pretty new to Java. Please don’t be too harsh!

For my first try, I want to make a little dice game called Pig. A player rolls the dice and accumulates points until she either holds or “craps out” by rolling a 1, playing against the computer.

I’ve got a lot of it on paper, not coding much yet because I keep getting hung up on how to take turns. I’ve got to know who the active player is obviously, so they can move, and each time the play shifts to another player, the dice score has to be reset, and the totals may change, etc.

I’ve thought of passing the active player to methods as a parameter, or creating an activePlayer object that alternately points to one player, or using some kind of boolean for my Player class like isActive, etc. Looking for code examples has been a lot harder than I thought it would be.

Is there any kind of system that’s been established as the best?

Well, a boolean or int should be used to keep track of turns. If it’s just one player you could just have 2 values for scores, and when the player rolls just set the enemies turn after that.

Also you should look into arrays, as that’ll really help you out.

Nope. You’ll probably find that given the same problem most people approach it in their own slightly different way. Granted some solutions will be better than others, but this will all come down to experience :slight_smile:

I’m working on a little turn based game and I keep the players in an array. Then I can just do players[playerNum].doTurn();
If you are working on a two player game, it wouldn’t be very important, but with more players, and different types, it becomes more useful. You might want to set up a finite state machine if the player has choices and does different things for the ai part. Actually, it would be completely unnecessary for a two player because the computer has to have ai and the human has to do input. The human probably ended his/her turn on some input and then it’s the ai turn.