Guest User

Untitled

a guest
Jun 13th, 2012
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.Sprite;
  4.     import flash.events.Event;
  5.     import flash.text.TextField;
  6.     import flash.text.TextFormat;
  7.     import flash.utils.Dictionary;
  8.    
  9.     public class Main extends Sprite
  10.     {
  11.         protected var _textfield : TextField;
  12.        
  13.         protected var _dictionary : Dictionary = new Dictionary();
  14.        
  15.         public function Main():void
  16.         {
  17.             if (stage) init();
  18.             else addEventListener(Event.ADDED_TO_STAGE, init);
  19.         }
  20.        
  21.         private function init(e:Event = null):void
  22.         {
  23.             removeEventListener(Event.ADDED_TO_STAGE, init);
  24.            
  25.             var i : int;
  26.            
  27.             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"];
  28.            
  29.             for (i = 0; i < 26; ++i)
  30.                 _dictionary[letters[i]] = i + 1;
  31.                
  32.             _textfield = new TextField();
  33.             _textfield.defaultTextFormat = new TextFormat("_sans", 16);
  34.             _textfield.width = 640;
  35.             _textfield.height = 480;
  36.             _textfield.multiline = true;
  37.            
  38.             addChild(_textfield);
  39.            
  40.             _textfield.appendText("Beginning Tests...\n");
  41.            
  42.             decode("105");
  43.             decode("2175");
  44.             decode("2222");
  45.             decode("0000");
  46.             decode("12471");
  47.             decode("161012");
  48.         }
  49.        
  50.         protected function decode(message : String) : Array
  51.         {
  52.             var result : Array = [];
  53.            
  54.             _textfield.appendText("Decoding..." + message + "\n");
  55.            
  56.             decipher(result, message, 0, "");
  57.            
  58.             _textfield.appendText("\tResult Array(" + result +")\n\n");
  59.            
  60.             return result;
  61.         }
  62.        
  63.         protected function decipher(result : Array, message : String, position : int = 0, output : String = "") : void
  64.         {  
  65.             for (var letter : String in _dictionary)
  66.             {
  67.                 var converted : String = _dictionary[letter].toString();
  68.                
  69.                 if (message.indexOf(converted, position) == position)
  70.                 {
  71.                     // okay this letter matches but is the next number a 0?
  72.                     // because it can't be a leading zero
  73.                    
  74.                     var newPosition : int = position + converted.length;
  75.                    
  76.                     if (message.indexOf("0", newPosition) != newPosition)
  77.                     {
  78.                         var newOutput : String = output + letter;
  79.                        
  80.                         if (output != "")
  81.                         {
  82.                             var index : int = result.indexOf(output);
  83.                            
  84.                             if (index >= 0) // does it exist? add to it
  85.                                 result[index] += letter;
  86.                             else
  87.                                 result.push(newOutput); // is this new? push it
  88.                         }
  89.                         else
  90.                         {
  91.                             result.push(newOutput);
  92.                         }
  93.                        
  94.                         // round and round we go!                      
  95.                         decipher(result, message, position + converted.length, newOutput);
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment