Created
October 3, 2013 12:15
-
-
Save codepope/6808875 to your computer and use it in GitHub Desktop.
Just a rough first hack at Hangman over MQTT... players should subscribe to hangman/result and publish their single character guesses to hangman/guess. A client for playing is left as an exercise for the reader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.eclipse.pahodemo; | |
import org.eclipse.paho.client.mqttv3.MqttCallback; | |
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; | |
import org.eclipse.paho.client.mqttv3.MqttClient; | |
import org.eclipse.paho.client.mqttv3.MqttConnectOptions; | |
import org.eclipse.paho.client.mqttv3.MqttException; | |
import org.eclipse.paho.client.mqttv3.MqttMessage; | |
import org.eclipse.paho.client.mqttv3.MqttPersistenceException; | |
public class MqttHangman implements MqttCallback { | |
MqttClient client; | |
MqttConnectOptions options; | |
String word; | |
String guess; | |
String resulttopic="hangman/result"; | |
String guesstopic="hangman/guess"; | |
boolean newguess=false; | |
boolean done=false; | |
public MqttHangman() {} | |
public static void main(String[] args) { | |
new MqttHangman().doDemo(); | |
} | |
public void setupHangman() { | |
word="hangman"; | |
guess="_______"; | |
newguess=false; | |
done=false; | |
} | |
public void doDemo() { | |
try { | |
client = new MqttClient("tcp://localhost:1883", "hangman_master"); | |
options = new MqttConnectOptions(); | |
client.setCallback(this); | |
client.connect(options); | |
client.subscribe(guesstopic); | |
while(true) { | |
setupHangman(); | |
publishResult(); | |
while(!done) { | |
try { | |
Thread.sleep(1000); | |
if(newguess) { | |
publishResult(); | |
newguess=false; | |
} | |
} catch (InterruptedException e) {} | |
} | |
publishResult(); | |
} | |
//client.disconnect(); | |
} catch (MqttException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void publishResult() throws MqttException, MqttPersistenceException { | |
MqttMessage message = new MqttMessage(); | |
message.setPayload(guess.getBytes()); | |
client.publish(resulttopic, message); | |
} | |
private void guessLetter(String s) { | |
char c=s.toLowerCase().charAt(0); | |
StringBuilder sb=new StringBuilder(); | |
for(int i=0;i<word.length();i++) { | |
if(word.charAt(i)==c) { | |
sb.append(c); | |
} else { | |
sb.append(guess.charAt(i)); | |
} | |
} | |
guess=sb.toString(); | |
done=guess.indexOf('_')==-1; | |
newguess=true; | |
} | |
@Override | |
public void connectionLost(Throwable cause) {} | |
@Override | |
public void messageArrived(String topic, MqttMessage message) | |
throws Exception { | |
if(topic.equals(guesstopic)) { | |
guessLetter(new String(message.getPayload())); | |
} | |
} | |
@Override | |
public void deliveryComplete(IMqttDeliveryToken token) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment