How do I decide who goes first in a turn-based game?
I am making a turn-based game, and as most turn-based games, there has to be a first player. I want to be able to resolve which player is going first, and I'm going to be particular about how I want to do it.
I could make a GameMaster
object whose only purpose is to tell one of the PlayerObject
s "You go first". But that's not how one usually plays a game in the real world. The players would not be told by an outsider who goes first, they would agree among themselves.
Second, I want the players to be entirely symmetrical. That is, I don't want to have one player have a FirstPlayer
script, I want them both to have the exact same script. This also goes for anything like a`public bool`. That's because that's still something beyond the players deciding who goes first, like the biggest kid always goes first because he's the biggest, and that feels wrong to me. Therefore, the players should have identical scripts and identical public fields.
What I want is for the PlayerObject
s to comminucate in some way and decide on a beginner in a completely fair manner. However, note that I'm not saying that there should be an even split over the course of many games. For instance, I have no control over what order their Start()
methods are called. So if Unity decides that the one who's on top in the Hierarchy is called first every tuesday and otherwise it's the one on the bottom, that's allright. I just want the players to be as equal to one another as the editor will let me.
Is there a nice way of deciding who goes first, all else being equal?
Answer by oswin_c · Dec 22, 2016 at 04:37 PM
That's fine -- you don't need to do anything with the player script.
Simply have something like, on the player script...
public void TakeTurn()
{
DoTurn();
}
Then, for example, on your GameMaster script, you could randomly rearrange the list of players, then call them in order. I had written up an example algorithm for doing this but unfortunately I have to leave and don't have time to finish it. If you get stuck and would like one, let me know.
On the GameMaster script, you could order the players in whatever order you like with whatever algorithm you like, then simply call...
void NextTurn()
{
....
NextPlayer.TakeTurn();
}
The player script would have nothing whatsoever to do with how they are ordered, only have a public TakeTurn() function, identical to all the others, that the GameMaster script calls when necessary.
This is still not the players themselves agreeing on who goes first though. This is an authoroty figure (i.e. the Game$$anonymous$$aster
) telling them who goes first. It doesn't matter that (s)he rolls a die to decide it. If I can just solve this little conundrum in a nice manner, I won't have need for a Game$$anonymous$$aster
, since the game is simple enough that all the elements can just take care of themselves. I want to try to make this game without having to have a Game$$anonymous$$aster
at all, and in that process, I stumbled upon the puzzle I posted above.
Your answer
![](https://koobas.hobune.stream/wayback/20220612103240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tile-based Enemy AI issue 0 Answers
StreamingAssets: allow players to edit sprites? 0 Answers
Best way to implement the server-side of a online turn based game? 0 Answers
C# Stuck on creating my Turn based battle system 0 Answers
Pull Playfab users list 0 Answers