Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.utils.Dictionary;
- public class Main extends Sprite
- {
- protected var _textfield : TextField;
- protected var _dictionary : Dictionary = new Dictionary();
- public function Main():void
- {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private function init(e:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- var i : int;
- var letters : Array = ["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"];
- for (i = 0; i < 26; ++i)
- _dictionary[letters[i]] = i + 1;
- _textfield = new TextField();
- _textfield.defaultTextFormat = new TextFormat("_sans", 16);
- _textfield.width = 640;
- _textfield.height = 480;
- _textfield.multiline = true;
- addChild(_textfield);
- _textfield.appendText("Beginning Tests...\n");
- decode("105");
- decode("2175");
- decode("2222");
- decode("0000");
- decode("12471");
- decode("161012");
- }
- protected function decode(message : String) : Array
- {
- var result : Array = [];
- _textfield.appendText("Decoding..." + message + "\n");
- decipher(result, message, 0, "");
- _textfield.appendText("\tResult Array(" + result +")\n\n");
- return result;
- }
- protected function decipher(result : Array, message : String, position : int = 0, output : String = "") : void
- {
- for (var letter : String in _dictionary)
- {
- var converted : String = _dictionary[letter].toString();
- if (message.indexOf(converted, position) == position)
- {
- // okay this letter matches but is the next number a 0?
- // because it can't be a leading zero
- var newPosition : int = position + converted.length;
- if (message.indexOf("0", newPosition) != newPosition)
- {
- var newOutput : String = output + letter;
- if (output != "")
- {
- var index : int = result.indexOf(output);
- if (index >= 0) // does it exist? add to it
- result[index] += letter;
- else
- result.push(newOutput); // is this new? push it
- }
- else
- {
- result.push(newOutput);
- }
- // round and round we go!
- decipher(result, message, position + converted.length, newOutput);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment