Advertisement
Guest User

Untitled

a guest
Feb 12th, 2011
567
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.     import flash.display.MovieClip;
  3.     import flash.events.Event;
  4.     public class MightGiveItAway extends MovieClip {
  5.        
  6.         protected const gridSize:int = 3;
  7.         protected const mConstant:int = gridSize * (gridSize * gridSize + 1) / 2;
  8.         protected const gridOrder:Array = [0, 1, 3, 4, 2, 6, 5, 7, 8];
  9.        
  10.         protected var dx:int = 0;
  11.         protected var dy:int = 0;
  12.         protected var count:int = 0;
  13.         protected var unusedNums:Array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  14.        
  15.         protected var attempts:int = 0;
  16.        
  17.         protected var numMcs:Vector.<NumMc> = new Vector.<NumMc>;
  18.        
  19.         public function MightGiveItAway():void {
  20.             if (stage){
  21.                  init();
  22.             }
  23.             else {
  24.                 this.addEventListener(Event.ADDED_TO_STAGE, init);
  25.             }          
  26.         }
  27.         protected function init (evt:Event = null):void {
  28.             this.removeEventListener(Event.ADDED_TO_STAGE, init);
  29.             placeNums();
  30.             this.addEventListener(Event.ENTER_FRAME, onLoop);
  31.         }
  32.         protected function placeNums ():void {
  33.             dx = stage.width / (gridSize + 1);
  34.             dy = stage.height / (gridSize + 1);
  35.            
  36.             for (var i:int = 0; i < gridSize; i++) {
  37.                 for (var j:int = 0; j < gridSize; j++) {
  38.                     var numMc:NumMc = new NumMc();
  39.                     stage.addChild(numMc);
  40.                     numMc.x = dx + j * dx;
  41.                     numMc.y = dy + i * dy;
  42.                     numMc.value = 0;
  43.                     numMc.position = numMcs.length;
  44.                     numMcs.push(numMc);
  45.                 }
  46.             }
  47.         }
  48.         protected function onLoop (evt:Event):void {
  49.             if (count > gridSize * gridSize - 1) {
  50.                 resetNums();
  51.             }else {
  52.                 var numMc:NumMc = numMcs[gridOrder[count]];
  53.  
  54.                 numMc.value = unusedNums[Math.round(Math.random() * (unusedNums.length - 1))];             
  55.                 unusedNums.splice(unusedNums.indexOf(numMc.value), 1);
  56.                
  57.                 if ( numIsSuccessful(numMc)) {
  58.                     if (count == gridSize * gridSize - 1) {
  59.                        
  60.                         trace("Completed in " + attempts + " attempts");                       
  61.                         resetNums();
  62.                     }
  63.                     count++;
  64.                 }else {
  65.                     resetNums();
  66.                 }
  67.             }
  68.            
  69.         }
  70.         protected function resetNums ():void {
  71.             count = 0;
  72.             attempts++;
  73.             unusedNums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  74.             for (var i:int = 0; i < gridSize * gridSize; i++) {
  75.                 numMcs[i].value = 0;
  76.             }
  77.         }
  78.         protected function numIsSuccessful (numMc:NumMc):Boolean {
  79.             if (getColumnTotal(numMc) > mConstant) {
  80.                 return false;
  81.             }else if (getRowTotal(numMc) > mConstant) {
  82.                 return false;
  83.             }
  84.             else if (getDiagonalToal(numMc) > mConstant) {
  85.                 return false;
  86.             }
  87.             return true;
  88.         }
  89.         protected function getColumnTotal(numMc:NumMc):int {
  90.             var total:int = 0;
  91.             for (var i:int = gridSize % numMc.position; i < gridSize * gridSize; i += gridSize) {
  92.                 total += numMcs[i].value;
  93.             }
  94.             return total;
  95.         }
  96.         protected function getRowTotal(numMc:NumMc):int {
  97.             var total:int = 0;
  98.             if (numMc.position < 3) {
  99.                 total = numMcs[0].value + numMcs[1].value + numMcs[2].value;
  100.             }
  101.             else if (numMc.position < 6) {
  102.                 total = numMcs[3].value + numMcs[4].value + numMcs[5].value;
  103.             }
  104.             else if (numMc.position < 9) {
  105.                 total = numMcs[6].value + numMcs[7].value + numMcs[8].value;
  106.             }
  107.             return total;
  108.         }
  109.         protected function getDiagonalToal(numMc:NumMc):int {
  110.             var total:int = 0;
  111.             if (numMc.position == 0 || numMc.position == 4 || numMc.position == 8) {
  112.                 total = numMcs[0].value + numMcs[4].value + numMcs[8].value;
  113.             }
  114.             return total;
  115.         }
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement