验证中...
Languages: ActionScript
Latest update 2018-12-09 09:57
ToolLLK.as
Raw Copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
package
{
import flash.display.*;
import flash.events.*;
import flash.filters.GlowFilter;
import flash.ui.Keyboard;
import flash.utils.*;
/**
* ...
* @author Myron
*/
public class ToolLLK extends MovieClip
{
private const DISTANCE:int = 52;
private const COL:int = 4;//行
private const ROW:int = 5;//列
private const LEFT:int = 50;
private const LINECOLOR:uint = 0xffffff;
private var picArr:Array = new Array();
private var tmpArr:Array = new Array();
private var obj1;
private var obj2;
public function ToolLLK()
{
addLintener();
}
private function addLintener()
{
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(MouseEvent.CLICK, clickHandler);
addPic();
}
private function addPic()
{
for (var i:int = 0; i < 20; i++)
{
picArr[i] = new Picture();
addChild(picArr[i]);
picArr[i].gotoAndStop(int(i % 5) + 1);
picArr[i].name = String(i % 5);
//picArr[i].light.visible = false;
//picArr[i].shine.visible = false;
setId();
}
xipai();
}
private function setId()
{
for (var i:int = 0; i < picArr.length; i++)
{
picArr[i].id = i;
}
}
private function clickHandler(e:MouseEvent):void
{
if (!(e.target is Picture))
return ;
if (obj1 == undefined)
{
obj1 = e.target;
//obj1.light.visible = true;
}
else if (obj2 == undefined && e.target.id != obj1.id)
{
if (e.target.name != obj1.name)
{
//obj1.light.visible = false;
obj1 = undefined;
return;
}
obj2 = e.target;
//obj2.light.visible = true;
var id1:int = int(obj1.id);
var id2:int = int(obj2.id);
if (check(id1, id2))
{
removeChild(picArr[obj1.id]);
removeChild(picArr[obj2.id]);
if (id1 > id2)
{
picArr.splice(id1, 1);
picArr.splice(id2, 1);
}
else
{
picArr.splice(id2, 1);
picArr.splice(id1, 1);
}
setId();
if (picArr.length == 0)
{
trace("OVER");
}
}
else
{
//obj1.light.visible = false;
//obj2.light.visible = false;
}
obj1 = undefined;
obj2 = undefined;
}
}
private function keyUpHandler(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.F5)
{
xipai();
//boom();
//aiFind();
//if (checkXipai())
//{
//trace("无解,需要洗牌");
//}
//else
//{
//trace("有解,无需洗牌");
//}
}
if (e.keyCode == Keyboard.F6)
{
boom();
}
if (e.keyCode == Keyboard.F7)
{
aiFind();
}
}
private function xipai()
{
tmpArr.splice(0, tmpArr.length);
for (var i:int = 0; i < picArr.length; i++)
{
xp(i);
tmpArr.push(picArr[i]);
}
function xp(n:int)
{
picArr[n].x = LEFT + (int(Math.random() * ROW) + 1) * DISTANCE;
picArr[n].y = LEFT + (int(Math.random() * COL) + 1) * DISTANCE;
for each (var p in tmpArr)
{
if (picArr[n].x == p.x && picArr[n].y == p.y)
{
xp(n);
break;
}
}
}
}
private function check(i:int, j:int, isXipai:Boolean = false):Boolean
{
var temp:Boolean;
var hitNum:int;
if (picArr[i].x == picArr[j].x || picArr[i].y == picArr[j].y)//一级检测(直线检测)
{
var sprite:Sprite = new Sprite();
sprite.graphics.lineStyle(2, LINECOLOR, 1);
sprite.graphics.moveTo(picArr[i].x, picArr[i].y);
sprite.graphics.lineTo(picArr[j].x, picArr[j].y);
setGlow(sprite);
addChild(sprite);
for each (var p in picArr)
{
if (sprite.hitTestObject(p))
{
hitNum++;
if (hitNum > 2)
{
break;
}
}
}
if (hitNum <= 2)
{
temp = true;
if (isXipai)
{
sprite.graphics.clear();
}
removeSprite(sprite);
}
else
{
sprite.graphics.clear();
removeChild(sprite);
//进入三级检测
temp = thirdCheck(i, j, isXipai);
}
}
else//进入二级检测
{
var sprite1:Sprite = new Sprite();
var sprite2:Sprite = new Sprite();
var sprite3:Sprite = new Sprite();
var sprite4:Sprite = new Sprite();
sprite1.graphics.lineStyle(2, LINECOLOR, 1);
sprite1.graphics.moveTo(picArr[i].x, picArr[i].y);
sprite1.graphics.lineTo(picArr[j].x, picArr[i].y);
setGlow(sprite1);
sprite2.graphics.lineStyle(2, LINECOLOR, 1);
sprite2.graphics.moveTo(picArr[j].x, picArr[i].y);
sprite2.graphics.lineTo(picArr[j].x, picArr[j].y);
setGlow(sprite2);
addChild(sprite1);
addChild(sprite2);
hitNum = 0;
for each (var p1 in picArr)
{
if (sprite1.hitTestObject(p1) || sprite2.hitTestObject(p1))
{
hitNum++;
if (hitNum > 2)
{
break;
}
}
}
if (hitNum <= 2)
{
temp = true;
if (isXipai)
{
sprite1.graphics.clear();
sprite2.graphics.clear();
}
removeSprite(sprite1);
removeSprite(sprite2);
return temp;
}
else
{
sprite1.graphics.clear();
sprite2.graphics.clear();
removeChild(sprite1);
removeChild(sprite2);
}
sprite3.graphics.lineStyle(2, LINECOLOR, 1);
sprite3.graphics.moveTo(picArr[i].x, picArr[i].y);
sprite3.graphics.lineTo(picArr[i].x, picArr[j].y);
setGlow(sprite3);
sprite4.graphics.lineStyle(2, LINECOLOR, 1);
sprite4.graphics.moveTo(picArr[i].x, picArr[j].y);
sprite4.graphics.lineTo(picArr[j].x, picArr[j].y);
setGlow(sprite4);
addChild(sprite3);
addChild(sprite4);
hitNum = 0;
for each (var p2 in picArr)
{
if (sprite3.hitTestObject(p2) || sprite4.hitTestObject(p2))
{
hitNum++;
if (hitNum > 2)
{
break;
}
}
}
if (hitNum <= 2)
{
temp = true;
if (isXipai)
{
sprite3.graphics.clear();
sprite4.graphics.clear();
}
removeSprite(sprite3);
removeSprite(sprite4);
}
else
{
sprite3.graphics.clear();
sprite4.graphics.clear();
removeChild(sprite3);
removeChild(sprite4);
//进入三级检测
temp = thirdCheck(i, j);
}
}
return temp;
}
private function thirdCheck(i:int, j:int, isXipai:Boolean = false):Boolean
{
var temp:Boolean;
var temp3:Boolean;
var temp4:Boolean;
var temp5:Boolean;
var temp6:Boolean;
var hitNum:int;
var sprite5:Sprite = new Sprite();
var sprite6:Sprite = new Sprite();
var sprite7:Sprite = new Sprite();
var sprite8:Sprite = new Sprite();
var sprite9:Sprite = new Sprite();
var sprite10:Sprite = new Sprite();
for (var ii:int = 0; ii < COL + 2; ii++)
{
sprite5.graphics.lineStyle(2, LINECOLOR, 1);
sprite5.graphics.moveTo(picArr[i].x, LEFT + DISTANCE * ii);
sprite5.graphics.lineTo(picArr[j].x, LEFT + DISTANCE * ii);
setGlow(sprite5);
addChild(sprite5);
hitNum = 0;
for each (var p1 in picArr)
{
if (sprite5.hitTestObject(p1))
{
hitNum++;
break;
}
}
if (hitNum == 0)
{
sprite6.graphics.lineStyle(2, LINECOLOR, 1);
sprite6.graphics.moveTo(picArr[i].x, LEFT + DISTANCE * ii);
sprite6.graphics.lineTo(picArr[i].x, picArr[i].y);
setGlow(sprite6);
addChild(sprite6);
hitNum = 0;
for each (var p2 in picArr)
{
if (sprite6.hitTestObject(p2))
{
hitNum++;
if (hitNum > 1)
{
break;
}
}
}
if (hitNum <= 1)
{
temp3 = true;
}
else
{
temp3 = false;
}
sprite7.graphics.lineStyle(2, LINECOLOR, 1);
sprite7.graphics.moveTo(picArr[j].x, LEFT + DISTANCE * ii);
sprite7.graphics.lineTo(picArr[j].x, picArr[j].y);
setGlow(sprite7);
addChild(sprite7);
hitNum = 0;
for each (var p3 in picArr)
{
if (sprite7.hitTestObject(p3))
{
hitNum++;
if (hitNum > 1)
{
break;
}
}
}
if (hitNum <= 1)
{
temp4 = true;
}
else
{
temp4 = false;
}
if (temp3 && temp4)
{
temp = true;
if (isXipai)
{
sprite5.graphics.clear();
sprite6.graphics.clear();
sprite7.graphics.clear();
}
removeSprite(sprite5);
removeSprite(sprite6);
removeSprite(sprite7);
break;
}
else
{
sprite5.graphics.clear();
sprite6.graphics.clear();
sprite7.graphics.clear();
removeChild(sprite5);
removeChild(sprite6);
removeChild(sprite7);
}
}
else
{
sprite5.graphics.clear();
removeChild(sprite5);
}
}
if (temp)
{
return temp;
}
for (var jj:int = 0; jj < ROW + 2; jj++)
{
sprite8.graphics.lineStyle(2, LINECOLOR, 1);
sprite8.graphics.moveTo(LEFT + DISTANCE * jj, picArr[i].y);
sprite8.graphics.lineTo(LEFT + DISTANCE * jj, picArr[j].y);
setGlow(sprite8);
addChild(sprite8);
hitNum = 0;
for each (var p4 in picArr)
{
if (sprite8.hitTestObject(p4))
{
hitNum++;
break;
}
}
if (hitNum == 0)
{
sprite9.graphics.lineStyle(2, LINECOLOR, 1);
sprite9.graphics.moveTo(LEFT + DISTANCE * jj, picArr[i].y);
sprite9.graphics.lineTo(picArr[i].x, picArr[i].y);
setGlow(sprite9);
addChild(sprite9);
hitNum = 0;
for each (var p5 in picArr)
{
if (sprite9.hitTestObject(p5))
{
hitNum++;
if (hitNum > 1)
{
break;
}
}
}
if (hitNum <= 1)
{
temp5 = true;
}
else
{
temp5 = false;
}
sprite10.graphics.lineStyle(2, LINECOLOR, 1);
sprite10.graphics.moveTo(LEFT + DISTANCE * jj, picArr[j].y);
sprite10.graphics.lineTo(picArr[j].x, picArr[j].y);
setGlow(sprite10);
addChild(sprite10);
hitNum = 0;
for each (var p6 in picArr)
{
if (sprite10.hitTestObject(p6))
{
hitNum++;
if (hitNum > 1)
{
break;
}
}
}
if (hitNum <= 1)
{
temp6 = true;
}
else
{
temp6 = false;
}
if (temp5 && temp6)
{
temp = true;
if (isXipai)
{
sprite8.graphics.clear();
sprite9.graphics.clear();
sprite10.graphics.clear();
}
removeSprite(sprite8);
removeSprite(sprite9);
removeSprite(sprite10);
break;
}
sprite8.graphics.clear();
sprite9.graphics.clear();
sprite10.graphics.clear();
removeChild(sprite8);
removeChild(sprite9);
removeChild(sprite10);
}
else
{
sprite8.graphics.clear();
removeChild(sprite8);
}
}
return temp;
}
private function removeSprite(sprite:Sprite)
{
setTimeout(remove, 600);
function remove()
{
removeChild(sprite);
}
}
private function setGlow(sprite:Sprite)
{
sprite.filters = [new GlowFilter(0x00ffcc, 1, 6, 6, 2, 1, false, false)];
}
//炸弹
private function boom():void
{
var random:int = int(Math.random() * picArr.length);
var random2:int;
for (var i:int = 0; i < picArr.length; i++)
{
if (i != random && picArr[i].name == picArr[random].name)
{
random2 = i;
removeChild(picArr[i]);
removeChild(picArr[random]);
if (random > random2)
{
picArr.splice(random, 1);
picArr.splice(random2, 1);
}
else
{
picArr.splice(random2, 1);
picArr.splice(random, 1);
}
return;
}
}
}
//自动寻找
private function aiFind():void
{
for (var i:int = 0; i < picArr.length; i++)
{
picArr[i].ai = false;
}
for (var j:int = 0; j < picArr.length; j++)
{
if (picArr[j].ai == true) continue;
for (var k:int = 0; k < picArr.length; k++)
{
if (picArr[k].ai == true) continue;
if (picArr[k].name == picArr[j].name && k != j)
{
if (check(j, k))
{
removeChild(picArr[j]);
removeChild(picArr[k]);
if (j > k)
{
picArr.splice(j, 1);
picArr.splice(k, 1);
}
else
{
picArr.splice(k, 1);
picArr.splice(j, 1);
}
return;
}
picArr[k].ai = true;
picArr[j].ai = true;
continue;
}
}
}
}
//检测是否需要洗牌
private function checkXipai():Boolean
{
for (var i:int = 0; i < picArr.length; i++)
{
picArr[i].ai = false;
}
for (var j:int = 0; j < picArr.length; j++)
{
if (picArr[j].ai == true) continue;
for (var k:int = 0; k < picArr.length; k++)
{
if (picArr[k].ai == true) continue;
if (picArr[k].name == picArr[j].name && k != j)
{
if (check(j, k, true))
{
return false;
}
picArr[k].ai = true;
picArr[j].ai = true;
continue;
}
}
}
return true;
}
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search