Telegraph Roulette
After Prince declared the internet dead (and Kenny G declared the internet not dead), I wondered where we, as humanity, would go next in terms of connectivity. Holomovement? The Mer-ka-bah?
Barring a massive leap forward in consciousness, I thought up the idea of Telegraph Roulette.
It’s like Chat Roulette, but without video, and all messages will be translated into Morse code. Kind of pointless, but a fun coding project.
Before beginning a project, I try to visualize my end goals. In terms of Telegraph Roulette, I want to create:
- Peer-to-peer chatting with random peers (like what Chat Roulette does)
- An interface that will translate chat messages into Morse code
- A visual and audio representation of the Morse code being transmitted consisting of an animated telegraph transmitter and beeping
- Translations of Morse code going out and coming in the user’s Telegraph Roulette session
After that, I try to figure out what technologies I’m going to use.
- Adobe Flex Flash Builder (since it’s one of my favorites)
- Chat Roulette is built upon Adobe Stratus, a peer to peer Flash technology that allows two Flash clients to connect directly to one another. I’m going to follow Chat Roulette’s lead and use Stratus as well, since I can’t seem to find any alternatives (plus they have a nice Stratus code example)
- Something to connect the users to each other. Stratus alone cannot connect users randomly. I’m thinking I’m going to use a technology like AMFPHP (which is also another one of my favorite technologies since it’s so lightweight and uses PHP)
I’ve started building the client interface, which consists of a simple text box that will translate a message into Morse code.
The first step, after creating my interface in MXML, was to figure out the easiest way to translate text into Morse. Users input text into the TextInput
<mx:Label id=”sentText”/>
<mx:Label id=”translation” fontSize=”16″ letterSpacing=”3″/>
<mx:HBox borderStyle=”solid” paddingTop=”10″ paddingBottom=”10″ paddingLeft=”10″ paddingRight=”10″>
<mx:TextInput id=”userInput”/>
<mx:Button id=”sendBtn” label=”Send” click=”sendMsg(event);”/>
</mx:HBox>
</mx:VBox>
Upon clicking the sendBtn the sendMsg function is triggered. sendMsg takes everything from userInput and renders it above the text input box.
-
private function sendMsg(event:MouseEvent):void
-
{
-
sentText.text=userInput.text;
-
translation.text=morseTranslate(userInput.text);
-
}
The first line in the function is pretty simple, but the translation is rendered by calling the morseTranslate() function.
-
private function morseTranslate(inputText:String):String
-
{
-
inputText=inputText.toLocaleLowerCase();
-
var inputArray:Array=inputText.split("");
-
var morseString:String="";
-
for(i=0;i<inputText.length;i++)
-
morseString+=" "+convertToMorse(inputArray[i]);
-
return morseString;
-
}
First, everything is converted to lowercase, since Morse code has no uppercase. Then the text from the userInput text input is split into an array. A new string, called morseString is also created. If morseString=”" then the first character will be null.
After that, inputArray is looped through. The convertToMorse function converts the character at the current index of inputArray to Morse code. Then that is added onto morseString
-
private function convertToMorse(inputCharacter:String):String
-
{
-
if(inputCharacter==".")
-
return "._._._";
-
if(inputCharacter==",")
-
return "__..__";
-
if(inputCharacter=="?")
-
return "..__..";
-
if(inputCharacter=="’")
-
return ".____.";
-
if(inputCharacter=="!")
-
return "_._.__";
-
if(inputCharacter=="/")
-
return "_.._.";
-
if(inputCharacter=="(")
-
return "_.__.";
-
if(inputCharacter==")")
-
return "_.__._";
-
if(inputCharacter=="&")
-
return "._…";
-
if(inputCharacter==":")
-
return "___…";
-
if(inputCharacter==";")
-
return "_._._.";
-
if(inputCharacter=="=")
-
return "_…_";
-
if(inputCharacter=="+")
-
return "._._.";
-
if(inputCharacter=="-")
-
return "_…._";
-
if(inputCharacter=="_")
-
return "..__._";
-
if(inputCharacter==‘"’)
-
return "._.._.";
-
if(inputCharacter=="$")
-
return "…__.._";
-
if(inputCharacter=="@")
-
return ".__._.";
-
if(inputCharacter==" ")
-
return " ";
-
else
-
return morseCode[inputCharacter];
-
}
This function has only one thing special about it. If the string does not match characters such as commas and parenthesis, then it must be a regular character. return morseCode[inputCharacter] matches up the string against an associative array keyed from a-z and containing the Morse code values of the respective character.
-
private var morseCode:Object = {a:"._", b:"_…", c:"_._.", d:"_..", e:".", f:".._.", g:"__.", h:"….", i:"..", j:".___", k:"_._", l:"._..", m:"__", n:"_.", o:"___", p:".__.", q:"__._", r:"._.", s:"…", t:"_", u:".._", v:"…_", w:".__", x:"_.._", y:"_.__", z:"__..", 0:"_____", 1:".____", 2:"..___", 3:"…__", 4:"…._", 5:"…..", 6:"_….", 7:"__…", 8:"___..", 9:"____."};
Here’s my big worry: since I’ve simply used the period and underscore character to denote the dots and dashes of Morse code, I have a feeling that this will come back to haunt me. I don’t know how, and I don’t know when, but it’s just a hunch.
Below is the complete code of Telegraph Roulette so far. I hope that this was of explaining how the code works was helpful, I would love suggestions to improve my development pedagogy. In the next post in the series, I’m going to tackle how to create the animations and sound effects for the translated Morse code and stress the importance of having everything loosely coupled.
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” initialize=”init();”>
<mx:Script>
<![CDATA[
import flash.media.Sound;
import flash.utils.*;
import mx.controls.Text;
private var morseCode:Object = {a:"._", b:"_...", c:"_._.", d:"_..", e:".", f:".._.", g:"__.", h:"....", i:"..", j:".___", k:"_._", l:"._..", m:"__", n:"_.", o:"___", p:".__.", q:"__._", r:"._.", s:"...", t:"_", u:".._", v:"..._", w:".__", x:"_.._", y:"_.__", z:"__..", 0:"_____", 1:".____", 2:"..___", 3:"...__", 4:"...._", 5:".....", 6:"_....", 7:"__...", 8:"___..", 9:"____."};
private var shortBeep:Sound;
private var longBeep:Sound;
private var i:Number;
private var intervalId:uint;
private function init():void
{
//something will go in here, I know it
}
private function sendMsg(event:MouseEvent):void
{
sentText.text=userInput.text;
translation.text=morseTranslate(userInput.text);
}
private function morseTranslate(inputText:String):String
{
inputText=inputText.toLocaleLowerCase();
var inputArray:Array=inputText.split("");
var morseString:String="";
for(i=0;i<inputText.length;i++)
morseString+=" "+convertToMorse(inputArray[i]);
return morseString;
}
private function convertToMorse(inputCharacter:String):String
{
if(inputCharacter==”.”)
return “._._._”;
if(inputCharacter==”,”)
return “__..__”;
if(inputCharacter==”?”)
return “..__..”;
if(inputCharacter==”‘”)
return “.____.”;
if(inputCharacter==”!”)
return “_._.__”;
if(inputCharacter==”/”)
return “_.._.”;
if(inputCharacter==”(“)
return “_.__.”;
if(inputCharacter==”)”)
return “_.__._”;
if(inputCharacter==”&”)
return “._…”;
if(inputCharacter==”:”)
return “___…”;
if(inputCharacter==”;”)
return “_._._.”;
if(inputCharacter==”=”)
return “_…_”;
if(inputCharacter==”+”)
return “._._.”;
if(inputCharacter==”-”)
return “_…._”;
if(inputCharacter==”_”)
return “..__._”;
if(inputCharacter==’”‘)
return “._.._.”;
if(inputCharacter==”$”)
return “…__.._”;
if(inputCharacter==”@”)
return “.__._.”;
if(inputCharacter==” “)
return ” “;
else
return morseCode[inputCharacter];
}
]]>
</mx:Script>
<mx:Panel title=”Telegraph Roulette” height=”75%” width=”75%” paddingTop=”10″ paddingBottom=”10″ paddingLeft=”10″ paddingRight=”10″>
<mx:VBox>
<mx:Label id=”sentText”/>
<mx:Label id=”translation” fontSize=”16″ letterSpacing=”3″/>
<mx:HBox borderStyle=”solid” paddingTop=”10″ paddingBottom=”10″ paddingLeft=”10″ paddingRight=”10″>
<mx:TextInput id=”userInput”/>
<mx:Button id=”sendBtn” label=”Send” click=”sendMsg(event);”/>
</mx:HBox>
</mx:VBox>
</mx:Panel>
</mx:Application>
No comments yet.