Deciding who is player one and two in a round based game with Google Play Game Services

前端 未结 2 1375
独厮守ぢ
独厮守ぢ 2021-01-17 23:39

I have a round-based Multiplayer game for Android that has been working over XMPP before, and I want to switch to Google Play Game Services. In the old version, there was an

相关标签:
2条回答
  • 2021-01-18 00:13

    This is a standard issue of selecting a leader from a bunch of unorganized nodes. There's some fairly advanced ways to do it, I'm sure. If you want to check out: Automatic selection of a leader in a cluster of nodes and hte Paxos algorithm.

    Now...that being said, since you have a limit of 4 people, theres easier ways to do it. You could just treat their ID's as a number, and who ever has the highest ID is player 1, and descending in order. That would probably be the simplest way.

    You could also have all the players submit a random roll and broad cast it. In the extremely rare case of a duplicate, you can have a reroll. Once everyone has made their rolls, and everyone has the same data, each individual client can determine the player order based on the value of the rolls.

    I wouldn't do anything related to the order in the array however. I don't believe that's guaranteed in any circumstance.

    0 讨论(0)
  • 2021-01-18 00:32

    I have solved the problem by comparing the player ids (which are random for each game):

    String myid = mActiveRoom.getParticipantId(client.getCurrentPlayerId());
    String remoteId = null;
    
    ArrayList<String> ids = mActiveRoom.getParticipantIds();
    for(int i=0; i<ids.size(); i++)
    {
        String test = ids.get(i);
        if( !test.equals(myid) )
        {
            remoteId = test;
            break;
        }
    }
    
    boolean iMakeTheFirstMove = ( myid.compareTo(remoteId) > 0 );
    
    0 讨论(0)
提交回复
热议问题