Android的蓝牙剪刀石头布 [英] Android bluetooth rock paper scissors

查看:112
本文介绍了Android的蓝牙剪刀石头布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个井字应用程序(在一个问题这里提到)。现在,我在用一个剪刀石头布的一部分。现在我怎么做它的是有相应的摇滚,纸,剪刀和一颗钮扣,那一定是pressed当每个玩家做出选择3个按键。内部时任何岩石/纸/剪刀按钮是pressed一个消息被发送,它是由一个处理程序接收并存储到另一台设备上的字符串。当玩家presses第四按钮时,游戏不逻辑和显示结果。

现在我想摆脱第四按钮。这时候,我preSS摇滚/纸/剪刀,它应该可以等待其他玩家做出他的选择,或者他已经立刻做出了他,显示效果。我怎样才能做到这一点?反正是有等待消息收到关于处理器(比忙等待其他)?
这里是code为我的处理程序:

 静态处理程序receiveChoice =新的处理程序(){
    @覆盖
    公共无效的handleMessage(消息MSG){
        INT numOfBytesReceived = msg.arg1;
        字节[]缓冲=(字节[])msg.obj;
        字符串strReceived =新的String(缓冲);
        strReceived = strReceived.substring(0,numOfBytesReceived);
        opponentChoice = strReceived;
    }
};


解决方案

您想了解的 等待/通知 。你可以有一到发送端的睡眠,直到它从听到另一种答案。你要确保你的逻辑决定是否睡觉是同步的(用同步挥发性或通过原子变量)与逻辑处理传入的事件。

I have created a tic tac toe app (mentioned in a question here). Now I use a rock paper scissors part in this. Right now how I have done it is have 3 buttons corresponding to rock, paper, scissors and one more button that must be pressed when each player has made their choice. Internally when any of the rock/paper/scissor button is pressed a message gets sent, it is received by a handler and stored into a string on the other device. When the player presses the 4th button, the game does the logic and displays the result.

Now I want to get rid of the 4th button. That is when I press rock/paper/scissor, it should either wait for the other player to make his choice or if he has already made his, display result immediately. How can I achieve this? Is there anyway to wait for a message to receive on the handler (other than busy waiting)? Here is the code for my handler:

static Handler receiveChoice = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        int numOfBytesReceived = msg.arg1;
        byte[] buffer = (byte[]) msg.obj;
        String strReceived = new String(buffer);
        strReceived = strReceived.substring(0, numOfBytesReceived);
        opponentChoice = strReceived;
    }
};

解决方案

You want to learn about wait/notify. You can have the first-to-send side sleep until it hears an answer from the other one. You'll want to make sure that your logic that decides whether to sleep is synchronized (with synchronized, volatile, or with an atomic variable) with the logic processing incoming events.

这篇关于Android的蓝牙剪刀石头布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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