はのいのとう
/**
* Copyright hacker_cobulht0 ( http://wonderfl.net/user/hacker_cobulht0 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/iWWv
*/
package {
import flash.display.Sprite;
import flash.events.*;
import flash.text.*;
[SWF(width = "465", height = "465", backgroundColor = 0xffffff, frameRate = "60")]
public class _Tower_of_Hanoi extends Sprite{
private var colorList:Array=[0x1F75FE,0x199EBD,0x1FCECB,0x1CAC78,0xC5E384,0xFFFF99,0xFFB653,0xFF8243,0xFF496C,0xC364C5];
private var tower1:Array=new Array();
private var tower2:Array=new Array();
private var tower3:Array=new Array();
private var tw1:towerA=new towerA();
private var tw2:towerA=new towerA();
private var tw3:towerB=new towerB();
private var bt1:button=new button();
private var bt2:button=new button();
private var bt3:button=new button();
private var flagMouse:Array=new Array();
private var count:Number=0;
private var tf1:TextField = new TextField();
public function _Tower_of_Hanoi(){
if (stage) init();else addEventListener(Event.ADDED_TO_STAGE,init );
}
public function init(ev:Event = null){
removeEventListener( Event.ADDED_TO_STAGE, init ) ;
var bg:dotPattern=new dotPattern(0x7DCCB6,0x9ADDCC);
addChildAt(bg,0);
//----------------------------------------------------------------------------------------------------
addChild(tw1);
addChild(tw2);
addChild(tw3);
tw1.x=80;
tw1.y=70;
tw2.x=230;
tw2.y=70;
tw3.x=390;
tw3.y=70;
addChild(bt1);
addChild(bt2);
addChild(bt3);
bt1.x=00;
bt1.y=0;
bt2.x=160;
bt2.y=0;
bt3.x=320;
bt3.y=0;
for (var q:int=10; q > 0; q--) {
var mc:ring=new ring(colorList[q-1],q*10+25,q);
addChild(mc);
mc.x=tw1.x;
mc.y=230+((q-1)*20);
tower1[q-1]=mc;
mc.myPos=1;
mc.addEventListener(MouseEvent.MOUSE_DOWN,_MD);
}
tower1[0].Permit=true;
stage.addEventListener(MouseEvent.MOUSE_MOVE,_MM);
stage.addEventListener(MouseEvent.MOUSE_UP,_MU);
addChild(tf1);tf1.x=430;tf1.textColor=0x1974D2;
tf1.text=String(count);
}
private function _MD(ev){
if(flagMouse.length==0&&ev.target.Permit==true){flagMouse.push(ev.target);}
}
private function _MM(ev){
if(flagMouse.length>0&&flagMouse[0].Permit==true){
flagMouse[0].x=mouseX;
flagMouse[0].y=mouseY;
}
}
private function _MU(ev){
if(flagMouse.length>0){
if(flagMouse[0].hitTestObject(bt1)){
if(high(flagMouse[0],1)){
flagMouse[0].x=tw1.x;
if(flagMouse[0].myPos!=1){swap(flagMouse[0],1);flagMouse[0].y=yDeploy(tower1);}else{back(flagMouse[0]);}
}else{back(flagMouse[0]);}
}else if(flagMouse[0].hitTestObject(bt2)){
if(high(flagMouse[0],2)){
flagMouse[0].x=tw2.x;
if(flagMouse[0].myPos!=2){swap(flagMouse[0],2);flagMouse[0].y=yDeploy(tower2);}else{back(flagMouse[0]);}
}else{back(flagMouse[0]);}
}else if(flagMouse[0].hitTestObject(bt3)){
if(high(flagMouse[0],3)){
flagMouse[0].x=tw3.x;
if(flagMouse[0].myPos!=3){swap(flagMouse[0],3);flagMouse[0].y=yDeploy(tower3);}else{back(flagMouse[0]);}
}else{back(flagMouse[0]);}
}
lock();
flagMouse=new Array();
tf1.text=String(count);
}
}
//----------------------------------------------------------------------------------------------------
private function high(arg1,arg2):Boolean{
var hg:Boolean=false;
if(arg1.myPos==arg2){hg=true;}
else{
switch (arg2) {
case 1:
if(tower1.length==0){hg=true;}else if(arg1.myNum<tower1[0].myNum){hg=true;}else{hg=false;};
break;
case 2:
if(tower2.length==0){hg=true;}else if(arg1.myNum<tower2[0].myNum){hg=true;}else{hg=false;};
break;
case 3:
if(tower3.length==0){hg=true;}else if(arg1.myNum<tower3[0].myNum){hg=true;}else{hg=false;};
break;
}
}
return hg;
}
private function back(arg1){
switch (arg1.myPos) {
case 1:
flagMouse[0].x=tw1.x;
flagMouse[0].y=430-(tower1.length*20);
break;
case 2:
flagMouse[0].x=tw2.x;
flagMouse[0].y=430-(tower2.length*20);
break;
case 3:
flagMouse[0].x=tw3.x;
flagMouse[0].y=430-(tower3.length*20);
break;
}
flagMouse=new Array();
}
private function yDeploy(arg:Array):Number{
var hg:Number=430;
hg=hg-(arg.length*20);
return hg;
}
private function swap(arg1,arg2){
switch (arg1.myPos) {
case 1:
if(tower1.length==1){tower1=new Array();}else{tower1.splice(0,1);}
break;
case 2:
if(tower2.length==1){tower2=new Array();}else{tower2.splice(0,1);}
break;
case 3:
if(tower3.length==1){tower3=new Array();}else{tower3.splice(0,1);}
break;
}
switch (arg2) {
case 1:
tower1.unshift(arg1);
arg1.myPos=1;
break;
case 2:
tower2.unshift(arg1);
arg1.myPos=2;
break;
case 3:
tower3.unshift(arg1);
arg1.myPos=3;
break;
}
count++;
}
private function lock(){
var q:int=0;
for ( q=0; q < tower1.length; q++) {
if(q==0){tower1[q].Permit=true;}else{tower1[q].Permit=false;}
}
for ( q=0; q < tower2.length; q++) {
if(q==0){tower2[q].Permit=true;}else{tower2[q].Permit=false;}
}
for ( q=0; q < tower3.length; q++) {
if(q==0){tower3[q].Permit=true;}else{tower3[q].Permit=false;}
}
}
private function trace_(){
var q:int=0;
trace("tower1----------")
for ( q=0; q < tower1.length; q++) {
trace(tower1[q].myNum,tower1[q].Permit);
}
trace("tower2----------")
for ( q=0; q < tower2.length; q++) {
trace(tower2[q].myNum,tower2[q].Permit);
}
trace("tower3----------")
for ( q=0; q < tower3.length; q++) {
trace(tower3[q].myNum,tower3[q].Permit);
}trace("\n\n\n\n");
}
//
}
}
import flash.display.*;
class ring extends Sprite{
public var myNum:Number;
public var myPos:Number;
public var Permit:Boolean=false;
public function ring(col:Number,wid:Number,num:Number){this.buttonMode=true;
myNum=num;this.name="ring"+String(num);
this.graphics.beginFill(col);
this.graphics.drawRoundRect(-1*(wid/2),0,wid,20,20);
this.graphics.endFill();
}
}
class towerA extends Sprite{
public function towerA(){
this.graphics.beginFill(0xECEABE);
this.graphics.drawRoundRect(-10,0,20,380,10);
this.graphics.endFill();
}
}
class towerB extends Sprite{
public function towerB(){
this.graphics.beginFill(0xECEABE);
this.graphics.drawRoundRect(-10,0,20,380,10);
this.graphics.endFill();
}
}
class button extends Sprite{
public function button(){
/*this.graphics.lineStyle(2,0x1CAC78);*/
this.graphics.beginFill(0xFFFFFF,0.01);
this.graphics.drawRect(0,0,160,465);
this.graphics.endFill();
}
}
//BackGround
class dotPattern extends Sprite{
public function dotPattern(arg1,arg2){
var bg:Sprite=new Sprite();
bg.graphics.beginFill(arg1);
bg.graphics.drawRect(0,0,465,465);
bg.graphics.endFill();
for(var j:int=0; j < 2304; j++){
var dot:Sprite=new Sprite();
dot.graphics.beginFill(arg2);
dot.graphics.drawCircle(0,0,2.5);
dot.graphics.endFill();
dot.x=j%48*10;
if(j%2==0){dot.y=Math.floor(j / 48)*20+10;}else{dot.y=Math.floor(j / 48)*20;};
bg.addChild(dot);
}
addChild(bg);
}
}