用户玩快速游戏时,如何确定谁是第一人? [英] How to decide who is the first player when user plays quick game?

查看:56
本文介绍了用户玩快速游戏时,如何确定谁是第一人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用onRoomConnected(int statusCode, Room room)中的以下代码来确定谁是第一个玩家.但是有时候我在两位选手中都获得第一/第二.如何解决此错误.

I am using following code in onRoomConnected(int statusCode, Room room) for deciding who is the first player. But some times I am getting first/second for both players same. How to resolve this error.

        if (quickGame) {
            myTurn = room.getParticipants().get(0).getParticipantId().equals(myId);
        } else {
            myTurn = room.getCreatorId().equals(myId);
        }
        if (myTurn) {
            Log.e(TAG, "First Player");
            status.setText("Click a button to start");
        } else {
            Log.e(TAG, "Second Player");
            status.setText("Wait for opponent to start");
        }

推荐答案

保证参与者ID的集合对于房间中的每个人都是相同的(但不能跨越不同的匹配项).但是,不能保证其在列表中的顺序.因此,如果您想轻松进行选举(例如确定谁先参加选举等),则必须依靠参与者ID的集合,而不是顺序.您可以通过以下几种方法来完成此任务:

The set of participant IDs is guaranteed to be the same to everybody who is in the room (but not across different matches). However, their order in the list is not guaranteed. So if you want to make an easy election (e.g. establish who goes first, etc), you must rely on the set of participant IDs, but not in the order. Some of the ways you can accomplish this are:

  1. 按字母顺序排列的参与者ID是第一个参加比赛的玩家
  2. 按字母顺序排在最前面的参与者ID负责随机选举一个首先开始的玩家.其它客户端将等待,直到参与者发送包含当选参与者的ID的可靠性的实时消息.
  1. The participant ID that comes alphabetically first is the first player to play
  2. The participant ID that comes alphabetically first is responsible for randomly electing a player to start first. The other clients will wait until that participants sends a reliable real time message containing the ID of the elected participant.

首选方法(2),因为它不包含可能的偏差.

Method (2) is preferred, because it doesn't contain a possible bias.

为什么?尽管我们没有指定参与者ID的结构(它只是一个字符串),但事实是它确实对信息进行编码,因此,如果您将参与者ID用作规则,则可能会以奇怪的方式分配谁首先.例如,您可能会发现一个特定的玩家始终在第一位,但这是因为,碰巧的是,他们的参与者ID的生成方式总是会发生这种情况.因此,使用参与者ID来选择谁是权威来随机决定谁先参加,而不是实际上是谁 先参加是一个更好的主意.

Why? Although we don't specify what's the structure of a participant ID (it's just a string), the truth is that it does encode information, so if you use the participant ID as a rule, you might end up with a weird distribution of who goes first. For example, you might find that a particular player is always going first, but that's because, coincidentally, their participant ID is generated in such a way that this always happens. So it's definitely a better idea to use the participant ID to elect who is the authority to randomly decide who goes first, not who actually goes first.

这篇关于用户玩快速游戏时,如何确定谁是第一人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆