loop slide puzzle
/**
* Copyright ile ( http://wonderfl.net/user/ile )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wTH8
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.system.Security;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
[SWF(width="350", height="350")]
public class Main extends Sprite {
private const WIDTH:Number = 35;
private const HEIGHT:Number = 35;
private var yPanel:uint = 10;
private var xPanel:uint = 10;
private var yobiPanel:uint = 5;
private var baseX:Number = 0;
private var baseY:Number = 0;
private var dragOnX:Number;
private var dragOnY:Number;
private var dragTate:uint;
private var dragYoko:uint;
private var loopFlg:int = 0;
private var gameBoard:Array = new Array();
private var copyPanelS1:Array = new Array();
private var copyPanelS2:Array = new Array();
private var roadColor:Array = new Array();
private var usedRoad:Array = new Array();
private var swfLoader:Loader;
private var panelClsArr:Array = new Array();
public function Main() {
loadPanel();
}
private function loadPanel():void{
var url:String = "http://ile.jp/wp-content/uploads/2011/11/panel.swf";
var urlReq:URLRequest;
Security.allowDomain("*");
swfLoader = new Loader();
urlReq = new URLRequest(url);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, setPanels);
swfLoader.load(urlReq);
}
private function setPanels(event:Event):void{
panelClsArr[0] = swfLoader.contentLoaderInfo.applicationDomain.getDefinition("panelKL_mc") as Class;
panelClsArr[1] = swfLoader.contentLoaderInfo.applicationDomain.getDefinition("panelMS_mc") as Class;
panelClsArr[2] = swfLoader.contentLoaderInfo.applicationDomain.getDefinition("panelMU_mc") as Class;
panelClsArr[3] = swfLoader.contentLoaderInfo.applicationDomain.getDefinition("panelYK_mc") as Class;
panelClsArr[4] = swfLoader.contentLoaderInfo.applicationDomain.getDefinition("panelHS_mc") as Class;
panelClsArr[5] = swfLoader.contentLoaderInfo.applicationDomain.getDefinition("panelHU_mc") as Class;
panelClsArr[6] = swfLoader.contentLoaderInfo.applicationDomain.getDefinition("panelTT_mc") as Class;
for(var i:uint = 0; i < yPanel; i++){
gameBoard[i] = new Array();
for (var j:uint = 0; j < xPanel; j++){
loopFlg = 0;
var r:uint = Math.floor(Math.random() * 6) + 1;
var panel:PanelMaker = new PanelMaker(panelClsArr, WIDTH * j+baseX, HEIGHT * i+baseY, r,1, i,j);
addChild(panel);
panel.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
gameBoard[i][j] = panel;
}
trace(gameBoard[i][0].panelType+" "+gameBoard[i][1].panelType+" "+gameBoard[i][2].panelType+" "+gameBoard[i][3].panelType+" "+gameBoard[i][4].panelType+" "+gameBoard[i][5].panelType+" "+gameBoard[i][6].panelType+" "+gameBoard[i][7].panelType+" "+gameBoard[i][8].panelType+" "+gameBoard[i][9].panelType);
}
coloring();
}
private function startDragging(event:MouseEvent):void
{
var panel:PanelMaker = event.currentTarget as PanelMaker;
dragTate = panel.tate;
dragYoko = panel.yoko;
// マウスの座標を取得
dragOnX = stage.mouseX;
dragOnY = stage.mouseY;
stage.addEventListener(MouseEvent.MOUSE_UP, stopMove);
stage.addEventListener(MouseEvent.MOUSE_MOVE, wayGet);
function stopMove(event:MouseEvent):void
{
//panel.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, stopMove);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragLine);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, wayGet);
dragOnX = panel.x;
ichiAwase();
}
function wayGet(e:MouseEvent):void
{
var tate:uint = dragTate;
var yoko:uint = dragYoko;
var yuyo:uint = 7;
var i:uint;
var j:uint;
var r:uint;
var copyPanel1:PanelMaker;
var copyPanel2:PanelMaker;
var rCol:uint;
var loopNum:uint;
//移動方向 1,横/2,縦
if (loopFlg == 1) {
i = tate;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, wayGet);
dragOnX = stage.mouseX;
dragOnY = stage.mouseY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragLine);
//複製して左右に移動
for (j = 0; j < xPanel; j++)
{
//タイプ継承
r = gameBoard[tate][j].panelType;
//フレーム継承
loopNum = usedRoad[tate][j];
rCol = roadColor[loopNum];
//左
copyPanel1 = new PanelMaker(panelClsArr, WIDTH * j + baseX - WIDTH * xPanel, HEIGHT * i + baseY, r,rCol, i, j);
addChild(copyPanel1);
copyPanelS1[j] = copyPanel1;
//右
copyPanel2 = new PanelMaker(panelClsArr, WIDTH * j + baseX + WIDTH * xPanel, HEIGHT * i + baseY, r,rCol, i, j);
addChild(copyPanel2);
copyPanelS2[j] = copyPanel2;
}
}else if (loopFlg == 2) {
i = yoko;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, wayGet);
dragOnX = stage.mouseX;
dragOnY = stage.mouseY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragLine);
//複製して上下に移動
for (j = 0; j < yPanel; j++)
{
//タイプ継承
r = gameBoard[j][yoko].panelType;
//フレーム継承
loopNum = usedRoad[j][yoko];
rCol = roadColor[loopNum];
//左
copyPanel1 = new PanelMaker(panelClsArr, WIDTH * i + baseX,HEIGHT * j + baseY - HEIGHT * yPanel , r,rCol, j, i);
addChild(copyPanel1);
copyPanelS1[j] = copyPanel1;
//右
copyPanel2 = new PanelMaker(panelClsArr, WIDTH * i + baseX,HEIGHT * j + baseY + HEIGHT * yPanel , r,rCol, j, i);
addChild(copyPanel2);
copyPanelS2[j] = copyPanel2;
}
}else {
if (stage.mouseX > dragOnX + yuyo) {
//右移動
loopFlg = 1;
}else if (stage.mouseX < dragOnX - yuyo) {
//左移動
loopFlg = 1;
}else if (stage.mouseY > dragOnY + yuyo) {
//下移動
loopFlg = 2;
}else if (stage.mouseY < dragOnY - yuyo){
//上移動
loopFlg = 2;
}
}
}
}
//位置あわせ
private function ichiAwase():void
{
var tate:uint = dragTate;
var yoko:uint = dragYoko;
var xx:Number = 0;
var nh:uint;
var i:uint;
var slideCnt:Number;
//タイプ保存
var bMemo:Array = new Array();
if (loopFlg == 1) {
if(gameBoard[tate][0].x>=baseX){
xx = gameBoard[tate][0].x - baseX + WIDTH / 2;
}else {
xx = (gameBoard[tate][0].x *-1)+ baseX + WIDTH / 2;
}
nh = Math.floor(xx / WIDTH);
slideCnt = nh % xPanel;
for (i = 0; i < xPanel; i++)
{
bMemo[i] = gameBoard[tate][i].panelType;
}
}else if (loopFlg == 2) {
if(gameBoard[0][yoko].y>=baseY){
xx = gameBoard[0][yoko].y - baseY + HEIGHT / 2;
}else {
xx = (gameBoard[0][yoko].y *-1)+ baseY + HEIGHT / 2;
}
nh = Math.floor(xx / HEIGHT);
slideCnt = nh % yPanel;
for (i = 0; i < yPanel; i++)
{
bMemo[i] = gameBoard[i][yoko].panelType;
}
}
//パネル書き換え
i = 0;
var j:uint
var num:Number;
if(loopFlg==1){
for (j = 0; j < xPanel; j++)
{
num = 0;
if (gameBoard[tate][0].x >= baseX) {
//右移動時
if (j < slideCnt) {
num = xPanel - Number(slideCnt) + Number(j);
gameBoard[tate][j].panelType = bMemo[num];
}else {
gameBoard[tate][j].panelType = bMemo[i];
i++;
}
}else {
//左移動時
if (j<xPanel-slideCnt) {
num = Number(slideCnt) + Number(j);
gameBoard[tate][j].panelType = bMemo[num];
}else {
gameBoard[tate][j].panelType = bMemo[i];
i++;
}
}
}
}else if (loopFlg==2) {
for (j = 0; j < yPanel; j++)
{
num = 0;
if (gameBoard[0][yoko].y >= baseY) {
//下移動時
if (j < slideCnt) {
num = yPanel - Number(slideCnt) + Number(j);
gameBoard[j][yoko].panelType = bMemo[num];
}else {
gameBoard[j][yoko].panelType = bMemo[i];
i++;
}
}else {
//上移動時
if (j<yPanel-slideCnt) {
num = Number(slideCnt) + Number(j);
gameBoard[j][yoko].panelType = bMemo[num];
}else {
gameBoard[j][yoko].panelType = bMemo[i];
i++;
}
}
}
}
//ループ用パネル削除&位置戻し
if(loopFlg==1){
for (j = 0; j < xPanel; j++)
{
gameBoard[tate][j].x = j * WIDTH + baseX;
gameBoard[tate][j].frame = 1;
removeChild(copyPanelS1[j]);
removeChild(copyPanelS2[j]);
loopFlg = 0;
}
}else if (loopFlg==2) {
for (j = 0; j < yPanel; j++)
{
gameBoard[j][yoko].y = j * HEIGHT + baseY;
gameBoard[j][yoko].frame = 1;
removeChild(copyPanelS1[j]);
removeChild(copyPanelS2[j]);
loopFlg = 0;
}
}
coloring();
}
//ループ確認&色づけ
private function coloring():void
{
var j:uint;
var i:uint;
var yi:int;
var xj:int;
var arrow:uint;
var loopNum:uint = 1;
var nextpanelType:uint;
var searchFlg:Boolean = true;
//配列の初期化 一度調べた道は調べない(最後は省く)
for (i = 0; i < yPanel; i++)
{
usedRoad[i] = new Array();
for (j = 0; j < xPanel; j++)
{
//未開のパネル
usedRoad[i][j] = 0;
}
}
//通ったことのない、全てのパネルの、2方向のつながり
for (i = 0; i < yPanel; i++)
{
for (j = 0; j < xPanel; j++)
{
//他のループが通ってないとき
if (usedRoad[i][j] == 0) {
//ベース方向その1
//ベースパネル i=y j=x
nextpanelType = gameBoard[i][j].panelType;
usedRoad[i][j] = loopNum;
roadColor[loopNum] = 2;
yi = i;
xj = j;
//arrow 0N 1A 2> 3V 4<
if (nextpanelType == 1) {
//上 進む方向とパネル(繋がっているかはまだ不明)
arrow = 1;
yi-=1
}else if(nextpanelType == 2) {
//左
arrow = 4;
xj -= 1;
}else if (nextpanelType == 3) {
//左
arrow = 4;
xj -= 1;
}else if (nextpanelType == 4) {
//上
arrow = 1;
yi -= 1;
}else if (nextpanelType == 5) {
//下
arrow = 3;
yi += 1;
}else if(nextpanelType==6) {
//上
arrow = 1;
yi -= 1;
}else {
//空パネル
arrow = 0;
}
ijRange();
if (usedRoad[yi][xj] == 0&&arrow!=0) {
loopSearch();
}
//ベース方向その2 るーぷしなかったら
//ベースパネル i=y j=x
nextpanelType = gameBoard[i][j].panelType;
yi = i;
xj = j;
if (nextpanelType == 1) {
//左
arrow = 4;
xj -= 1;
}else if(nextpanelType == 2) {
//下
arrow = 3;
yi += 1;
}else if (nextpanelType == 3) {
//右
arrow = 2;
xj += 1;
}else if (nextpanelType == 4) {
//右
arrow = 2;
xj += 1;
}else if (nextpanelType == 5) {
//右
arrow = 2;
xj += 1;
}else if(nextpanelType==6) {
//下
arrow = 3;
yi += 1;
}else {
//空パネル
arrow = 0;
}
ijRange();
if (usedRoad[yi][xj] == 0&&arrow!=0) {
loopSearch();
}
loopNum++;
}
}
}
//カラーフレーム変更
for (i = 0; i < yPanel; i++)
{
trace("usedRoadARR"+i+"="+usedRoad[i])
for (j = 0; j < xPanel; j++)
{
loopNum = usedRoad[i][j];
var rCol:uint = roadColor[loopNum];
gameBoard[i][j].frame = rCol;
}
}
function ijRange():void
{
//範囲外にでれば反対側へ
if (yi < 0) {
yi = yPanel - 1;
}else if (yi > yPanel - 1) {
yi = 0;
}
if (xj < 0) {
xj = xPanel - 1;
}else if (xj > xPanel - 1) {
xj = 0;
}
}
function loopSearch():void
{
//とぎれるまでたどる
searchFlg = true;
do {
if (usedRoad[yi][xj] == 0) {
//次のパネルが未使用の時
//次のパネルのタイプ
nextpanelType = gameBoard[yi][xj].panelType;
//来た方向が次のパネルのタイプに繋がるか
if (arrow == 1) {
//上 256
if (nextpanelType == 2) {
//繋がっていたので、ルート番号をふる
usedRoad[yi][xj] = loopNum;
//道に沿って次に進む方向
arrow = 4;
//まだ繋がっているかわからないが次のパネル
xj -= 1;
}else if (nextpanelType == 5) {
usedRoad[yi][xj] = loopNum;
arrow = 2;
xj += 1;
}else if (nextpanelType == 6) {
usedRoad[yi][xj] = loopNum;
arrow = 1;
yi -= 1;
}else {
//繋がらないか空パネル
arrow = 0;
}
}else if (arrow == 2) {
//右 123
if (nextpanelType == 1) {
usedRoad[yi][xj] = loopNum;
arrow = 1;
yi -= 1;
}else if (nextpanelType == 2) {
usedRoad[yi][xj] = loopNum;
arrow = 3;
yi += 1;
}else if (nextpanelType == 3) {
usedRoad[yi][xj] = loopNum;
arrow = 2;
xj += 1;
}else {
arrow = 0;
}
}else if (arrow == 3) {
//下 146
if (nextpanelType == 1) {
usedRoad[yi][xj] = loopNum;
arrow = 4;
xj -= 1;
}else if (nextpanelType==4) {
usedRoad[yi][xj] = loopNum;
arrow = 2;
xj += 1;
}else if (nextpanelType == 6) {
usedRoad[yi][xj] = loopNum;
arrow = 3;
yi += 1;
}else {
arrow = 0;
}
}else if (arrow == 4) {
//左 345
if (nextpanelType == 3) {
usedRoad[yi][xj] = loopNum;
arrow = 4;
xj -= 1;
}else if (nextpanelType == 4) {
usedRoad[yi][xj] = loopNum;
arrow = 1;
yi -= 1;
}else if (nextpanelType == 5) {
usedRoad[yi][xj] = loopNum;
arrow = 3;
yi += 1;
}else {
arrow = 0;
}
}else {
arrow = 0;
}
ijRange();
if (arrow != 0 ) {
//ルート色UP
roadColor[loopNum]++;
}else{
//つながらない
searchFlg = false;
}
}else if(i==yi&&j==xj){
//使用済みだが元のパネルなのでループするかも
//どちらにせよルート検索終了
searchFlg = false;
//元のパネルのタイプ
nextpanelType = gameBoard[yi][xj].panelType;
//来た方向がループに繋がるか
if (arrow == 1) {
//上 256
if (nextpanelType == 2 || nextpanelType == 5 || nextpanelType == 6) {
roadColor[loopNum] = 1;
} else {
arrow = 0;
}
} else if (arrow == 2) {
//右 123
if (nextpanelType == 1 || nextpanelType == 2 || nextpanelType == 3) {
roadColor[loopNum] = 1;
} else {
arrow = 0;
}
} else if (arrow == 3) {
//下 146
if (nextpanelType == 1 || nextpanelType == 4 || nextpanelType == 6) {
roadColor[loopNum] = 1;
} else {
arrow = 0;
}
} else if (arrow == 4) {
//左 345
if (nextpanelType == 3 || nextpanelType == 4 || nextpanelType == 5) {
roadColor[loopNum] = 1;
} else {
arrow = 0;
}
} else {
arrow = 0;
}
}else {
//次のパネルが使用済み
searchFlg = false;
}
}while (searchFlg);
}
}
//ドラッグ中、毎フレーム
private function dragLine(e:MouseEvent):void
{
//[]たて
var tate:uint = dragTate;
//[][]よこ
var yoko:uint = dragYoko;
var moveDot:Number;
var j:uint;
if(loopFlg==1){
moveDot = stage.mouseX - dragOnX;
dragOnX = stage.mouseX;
for (j = 0; j < xPanel; j++)
{
//左ループ
if (copyPanelS1[j].x + moveDot < -WIDTH * xPanel) {
copyPanelS1[j].x += WIDTH * xPanel * 3;
}
if (gameBoard[tate][j].x + moveDot < -WIDTH * xPanel) {
gameBoard[tate][j].x += WIDTH * xPanel * 3;
}
if (copyPanelS2[j].x + moveDot < -WIDTH * xPanel) {
copyPanelS2[j].x += WIDTH * xPanel * 3;
}
//右ループ
if (copyPanelS1[j].x + moveDot > WIDTH * xPanel*2) {
copyPanelS1[j].x -= WIDTH * xPanel * 3;
}
if (gameBoard[tate][j].x + moveDot > WIDTH * xPanel*2) {
gameBoard[tate][j].x -= WIDTH * xPanel * 3;
}
if (copyPanelS2[j].x + moveDot > WIDTH * xPanel*2) {
copyPanelS2[j].x -= WIDTH * xPanel * 3;
}
//移動
gameBoard[tate][j].x += moveDot;
copyPanelS1[j].x += moveDot;
copyPanelS2[j].x += moveDot;
}
}else if (loopFlg == 2) {
moveDot = stage.mouseY - dragOnY;
dragOnY = stage.mouseY;
for (j = 0; j < yPanel; j++)
{
//左ループ
if (copyPanelS1[j].y + moveDot < -HEIGHT * yPanel) {
copyPanelS1[j].y += HEIGHT * yPanel * 3;
}
if (gameBoard[j][yoko].y + moveDot < -HEIGHT * yPanel) {
gameBoard[j][yoko].y += HEIGHT * yPanel * 3;
}
if (copyPanelS2[j].y + moveDot < -HEIGHT * yPanel) {
copyPanelS2[j].y += HEIGHT * yPanel * 3;
}
//右ループ
if (copyPanelS1[j].y + moveDot > HEIGHT * yPanel*2) {
copyPanelS1[j].y -= HEIGHT * yPanel * 3;
}
if (gameBoard[j][yoko].x + moveDot > HEIGHT * yPanel*2) {
gameBoard[j][yoko].x -= HEIGHT * yPanel * 3;
}
if (copyPanelS2[j].y + moveDot > HEIGHT * yPanel*2) {
copyPanelS2[j].y -= HEIGHT * yPanel * 3;
}
//移動
gameBoard[j][yoko].y += moveDot;
copyPanelS1[j].y += moveDot;
copyPanelS2[j].y += moveDot;
}
}
}
}
}
import flash.display.MovieClip;
class PanelMaker extends MovieClip {
private var _panelType:uint;
private var _tate:uint;
private var _yoko:uint;
private var _frame:uint;
private var testCls:Class;
private var panelMcArr:Array = new Array();
public function PanelMaker(panelClsArr:Array, x:Number, y:Number, panelType:uint, frame:uint , tate:int, yoko:int):void
{
this.x = x;
this.y = y;
_panelType = panelType;
_frame = frame;
_tate = tate;
_yoko = yoko;
for(var i:uint=0;i<7;i++) panelMcArr[i]= new panelClsArr[i]();
addChild(panelMcArr[_panelType]);
panelMcArr[_panelType].gotoAndStop(_frame);
}
public function get tate():int
{
return _tate;
}
public function set tate(v:int):void
{
_tate = v;
}
public function get yoko():int
{
return _yoko;
}
public function set yoko(v:int):void
{
_yoko = v;
}
public function get panelType():uint
{
return _panelType;
}
public function set panelType(v:uint):void
{
removeChild(panelMcArr[_panelType]);
_panelType = v;
addChild(panelMcArr[_panelType]);
this.panelMcArr[_panelType].gotoAndStop(1);
}
public function get frame():int
{
return panelMcArr[_panelType].currentFrame;
}
public function set frame(v:int):void
{
panelMcArr[_panelType].gotoAndStop(v);
}
}