Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import flash.display.MovieClip;
- import flash.events.Event;
- public class MightGiveItAway extends MovieClip {
- protected const gridSize:int = 3;
- protected const mConstant:int = gridSize * (gridSize * gridSize + 1) / 2;
- protected const gridOrder:Array = [0, 1, 3, 4, 2, 6, 5, 7, 8];
- protected var dx:int = 0;
- protected var dy:int = 0;
- protected var count:int = 0;
- protected var unusedNums:Array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
- protected var attempts:int = 0;
- protected var numMcs:Vector.<NumMc> = new Vector.<NumMc>;
- public function MightGiveItAway():void {
- if (stage){
- init();
- }
- else {
- this.addEventListener(Event.ADDED_TO_STAGE, init);
- }
- }
- protected function init (evt:Event = null):void {
- this.removeEventListener(Event.ADDED_TO_STAGE, init);
- placeNums();
- this.addEventListener(Event.ENTER_FRAME, onLoop);
- }
- protected function placeNums ():void {
- dx = stage.width / (gridSize + 1);
- dy = stage.height / (gridSize + 1);
- for (var i:int = 0; i < gridSize; i++) {
- for (var j:int = 0; j < gridSize; j++) {
- var numMc:NumMc = new NumMc();
- stage.addChild(numMc);
- numMc.x = dx + j * dx;
- numMc.y = dy + i * dy;
- numMc.value = 0;
- numMc.position = numMcs.length;
- numMcs.push(numMc);
- }
- }
- }
- protected function onLoop (evt:Event):void {
- if (count > gridSize * gridSize - 1) {
- resetNums();
- }else {
- var numMc:NumMc = numMcs[gridOrder[count]];
- numMc.value = unusedNums[Math.round(Math.random() * (unusedNums.length - 1))];
- unusedNums.splice(unusedNums.indexOf(numMc.value), 1);
- if ( numIsSuccessful(numMc)) {
- if (count == gridSize * gridSize - 1) {
- trace("Completed in " + attempts + " attempts");
- resetNums();
- }
- count++;
- }else {
- resetNums();
- }
- }
- }
- protected function resetNums ():void {
- count = 0;
- attempts++;
- unusedNums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
- for (var i:int = 0; i < gridSize * gridSize; i++) {
- numMcs[i].value = 0;
- }
- }
- protected function numIsSuccessful (numMc:NumMc):Boolean {
- if (getColumnTotal(numMc) > mConstant) {
- return false;
- }else if (getRowTotal(numMc) > mConstant) {
- return false;
- }
- else if (getDiagonalToal(numMc) > mConstant) {
- return false;
- }
- return true;
- }
- protected function getColumnTotal(numMc:NumMc):int {
- var total:int = 0;
- for (var i:int = gridSize % numMc.position; i < gridSize * gridSize; i += gridSize) {
- total += numMcs[i].value;
- }
- return total;
- }
- protected function getRowTotal(numMc:NumMc):int {
- var total:int = 0;
- if (numMc.position < 3) {
- total = numMcs[0].value + numMcs[1].value + numMcs[2].value;
- }
- else if (numMc.position < 6) {
- total = numMcs[3].value + numMcs[4].value + numMcs[5].value;
- }
- else if (numMc.position < 9) {
- total = numMcs[6].value + numMcs[7].value + numMcs[8].value;
- }
- return total;
- }
- protected function getDiagonalToal(numMc:NumMc):int {
- var total:int = 0;
- if (numMc.position == 0 || numMc.position == 4 || numMc.position == 8) {
- total = numMcs[0].value + numMcs[4].value + numMcs[8].value;
- }
- return total;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement