5D艺术网首页
商城
|
资讯
|
作品
|
博客
|
教程
|
论坛
登录
注册
加为好友
发短消息
来自:
性别:秘密
最后登录:2007-04-12
http://Billows.5d.cn/
首页
|
新闻
|
话题
|
博客
|
相册
|
艺术作品
|
社交关系
|
留言板
|
社交圈
2006/08/29 | 用AS3实现连连看Demo版
类别(My Works)
|
评论
(0)
|
阅读(1044)
|
发表于 11:19
记得第一个工作面试时就是考连连看算法(当然没做出来了),后来工作了也一直没时间研究它,现在正好趁学习AS 3.0就研究了一下,发现并没有想象中那么难,这次用AS 3实现了核心算法,总算了却了这个心结,呵呵,不过这个blog不能打包下载,要不然就把源文件传上来了。代码如下:
package{
import flash.display.*;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.utils.getDefinitionByName;
import flash.text.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class seeLink extends Sprite{
private var mapArray:Array;
private var widthSize:Number = 12;
private var heightSize:Number = 12;
private var imageArray,imageArray2:Array;
private var oneCornerArray,twoCornerArray:Array;
private var allArray:Array;
private var recFrame:RecFrame;
private var preUnit,nextUnit:mapUnit;
private var timeBar:TimeBar;
private var clickCount:Number = 0;
private var flag:Boolean = false;
private var flag_mc:MovieClip;
public function seeLink(){
mapArray = new Array();
allArray = new Array();
oneCornerArray = new Array();
twoCornerArray = new Array();
setImageArray();
attachText();
attachTimeBar();
attachMap();
attachFlag("start");
}
private function attachText():void{
var timeTextField:TextField = new TextField();
timeTextField.x = 7;
timeTextField.y = 15;
timeTextField.text = "时间";
this.addChild(timeTextField);
}
public function attachTimeBar():void{
timeBar = new TimeBar();
timeBar.x = 40;
timeBar.y = 20;
this.addEventListener(Event.ENTER_FRAME,myOnEnterFrame);
this.addChild(timeBar);
}
private function myOnEnterFrame(e:Event):void{
timeSub();
}
private function attachFlag(type:String):void{
flag_mc = new Flag();
//trace(type);
if(type=="win"){
flag_mc.gotoAndStop(1);
}else if(type=="lose"){
flag_mc.gotoAndStop(2);
}else if(type=="start"){
flag_mc.gotoAndStop(3);
}
flag_mc.x = stage.stageWidth/2-flag_mc.width/2;
flag_mc.y = stage.stageHeight/2-flag_mc.height/2;
this.addChild(flag_mc);
var myTimer:Timer = new Timer(3000, 1);
myTimer.addEventListener("timer", timerHandler);
myTimer.start();
}
private function timerHandler(event:TimerEvent):void{
trace("removeFlag");
this.removeChild(flag_mc);
}
private function checkIsWin():Boolean{
for(var i:Number = 0;i<mapArray.length;i++){
if(mapArray[i]._typeNum!=-1){
return false;
}
}
return true;
}
private function removeEnterFrame():void{
this.removeEventListener(Event.ENTER_FRAME,myOnEnterFrame);
}
private function timeSub():void{
//trace("timeBar._width="+timeBar._width);
timeBar.width -= 0.08;
if(timeBar.width<0.5){
//trace("remove");
timeBar.width = 0;
attachFlag("lose");
removeEnterFrame();
}else{
var isWin:Boolean = checkIsWin();
if(isWin){
attachFlag("win");
removeEnterFrame();
}
}
}
public function setLatterArray(_Arr:Array):Array {
var len:Number = _Arr.length;
var j:Number;
for (j=0; j<len; j++) {
var rand:Number = Math.floor(Math.random()*len);
var temp:Number = _Arr[j];
_Arr[j] = _Arr[rand];
_Arr[rand] = temp;
}
return _Arr;
}
private function setImageArray(){
var beforeArray=new Array();
for(var i:Number = 0;i<(widthSize*heightSize-4*(heightSize-1))/2;i++){
var randomNum = Math.floor(Math.random()*28);
beforeArray.push(randomNum);
}
//trace("before beforeArray="+beforeArray);
var tempArray:Array = beforeArray.slice(0);
//trace("before tempArray="+tempArray);
var laterArray:Array = setLatterArray(tempArray);
//trace("later laterArray="+laterArray);
//trace("imageArray2="+imageArray2);
allArray = beforeArray.concat(laterArray);
//trace("allArray="+allArray);
}
private function attachMap(){
var arrayNum:Number = 0;
for(var i:Number = 0;i<heightSize;i++){
for(var j:Number = 0;j<widthSize;j++){
//加载底
var edge:Edge= new Edge();
edge.x = 20+j*edge.width;
edge.y = 80+i*edge.height;
//edge.name = i*10+j;
this.addChild(edge);
var _num = i*heightSize+j;
//加上周围透明mc,用来显示路径
if(i==0||i==heightSize-1||j==0||j==widthSize-1){
var cls= getDefinitionByName("cover");
var temp = new cls();
edge.addChild(temp)
mapArray[_num]= new mapUnit(this,edge,i,j,-1);
}else{
//底上加载各种图片
var typeNum = allArray[arrayNum];
arrayNum++;
var image_cls= getDefinitionByName("type"+typeNum);
var image = new image_cls();
image.name = typeNum;
edge.addChild(image)
mapArray[_num]= new mapUnit(this,edge,i,j,typeNum);
}
}
}
}
private function removeRecFrame(){
/*trace("recFrame="+recFrame);
if(recFrame!=null){
this.removeChild(recFrame);
}*/
recFrame.visible = false;
}
private function attachRecFrame(unit:mapUnit){
//removeRecFrame();
if(recFrame==null){
recFrame = new RecFrame();
this.addChild(recFrame);
}
recFrame.visible = true;
recFrame.x = unit.graphic.x;
recFrame.y = unit.graphic.y;
}
private function addClickCount():void{
clickCount++;
}
public function addRecFrame(unit:mapUnit){
addClickCount();
attachRecFrame(unit);
//trace("clickCount="+clickCount);
if(clickCount==1){
preUnit = unit;
}else{
if(preUnit==unit){
//trace("flag="+flag);
flag = !flag;
//trace("== flag="+flag);
if(flag){
removeRecFrame();
}else{
attachRecFrame(unit);
}
}else{
//trace("preUnit._typeNum="+preUnit._typeNum);
//trace("unit._typeNum="+unit._typeNum);
if(preUnit._typeNum==unit._typeNum){
checkIsLink(preUnit,unit)
}
preUnit = unit;
flag = false;
}
}
}
private function checkIsLink(preUnit,nextUnit:mapUnit){
var isBeeLink:Number = isBeeLineLink(preUnit,nextUnit);
trace("isBeeLink="+isBeeLink);
//isBeeLink return parameter:
//0.没有符合条件;
//1.符合条件,联结成功;
//2.符合条件,没有联结成功.
//一级检测成功
if(isBeeLink==1){
removeHandle(preUnit,nextUnit);
}
//一级检测不成功
if(isBeeLink==2||isBeeLink==0){
//离开一级检验,进入二级检测.
secondCheck(preUnit,nextUnit);
}
}
private function secondCheck(preUnit,nextUnit:mapUnit):void{
var _isOneCornerLink:Boolean = isOneCornerLink(preUnit,nextUnit);
trace("_isOneCornerLink="+_isOneCornerLink);
//二级检测成功
if(_isOneCornerLink){
removeHandle(preUnit,nextUnit);
}else{
//二级检测不成功,离开二级检验,进入三级检验.
thirdCheck(preUnit,nextUnit);
}
}
private function thirdCheck(preUnit,nextUnit:mapUnit):void{
var _isTwoCornerLink:Boolean =isTwoCornerLink(preUnit,nextUnit);
trace("_isTwoCornerLink="+_isTwoCornerLink);
if(_isTwoCornerLink){
removeHandle(preUnit,nextUnit);
}
removeArray();
}
private function isTwoCornerLink(preUnit,nextUnit:mapUnit):Boolean{
//trace("preUnit.xNo="+preUnit.xNo);
//trace("preUnit.yNo="+preUnit.yNo);
//trace("nextUnit.xNo="+nextUnit.xNo);
//trace("nextUnit.yNo="+nextUnit.yNo);
//trace("in isTwoCornerLink");
pushToOneCornerArray(preUnit);
pushToTwoCornerArray(nextUnit);
//trace("oneCornerArray.length="+oneCornerArray.length);
//trace("twoCornerArray.length="+twoCornerArray.length);
var tempArray:Array = new Array();
for(var i:Number = 0;i<oneCornerArray.length;i++){
for(var j:Number = 0;j<twoCornerArray.length;j++){
var temp1:mapUnit = oneCornerArray[i];
var temp2:mapUnit = twoCornerArray[j];
//trace("temp1.xNo="+temp1.xNo);
//trace("temp1.yNo="+temp1.yNo);
//trace("temp2.xNo="+temp2.xNo);
//trace("temp2.yNo="+temp2.yNo);
if(temp1.xNo==temp2.xNo){
if(temp1.yNo==preUnit.yNo&&temp2.yNo==nextUnit.yNo){
tempArray.push({one:temp1,two:temp2});
}
}
if(temp1.yNo==temp2.yNo){
if(temp1.xNo==preUnit.xNo&&temp2.xNo==nextUnit.xNo){
tempArray.push({one:temp1,two:temp2});
}
}
}
}
//trace("tempArray.length="+tempArray.length);
for(var m:Number = 0;m<tempArray.length;m++){
var oneCorner:mapUnit = tempArray[m].one;
var twoCorner:mapUnit = tempArray[m].two;
//trace("oneCorner.xNo="+oneCorner.xNo);
//trace("oneCorner.yNo="+oneCorner.yNo);
//trace("twoCorner.xNo="+twoCorner.xNo);
//trace("twoCorner.yNo="+twoCorner.yNo);
if(oneCorner.yNo==twoCorner.yNo){
var result1:Number = compare(oneCorner.yNo,oneCorner.xNo,twoCorner.xNo,"x");
}
if(oneCorner.xNo==twoCorner.xNo){
var result2:Number = compare(oneCorner.xNo,oneCorner.yNo,twoCorner.yNo,"y");
}
if(result1==1||result2==1){
return true;
}
}
return false;
}
private function removeHandle(preUnit,nextUnit:mapUnit):void{
removeUnit(preUnit);
removeUnit(nextUnit);
removeRecFrame();
}
private function removeArray():void{
//trace("oneCornerArray.length="+oneCornerArray.length);
//trace("twoCornerArray.length="+twoCornerArray.length);
oneCornerArray.splice(0);
twoCornerArray.splice(0);
}
private function checkOneCornerLink(cornerUnit,preUnit,nextUnit:mapUnit):Boolean{
//如果拐角为空
//trace("cornerUnit._typeNum="+cornerUnit._typeNum);
//trace("cornerUnit.xNo="+cornerUnit.xNo);
//trace("cornerUnit.yNo="+cornerUnit.yNo);
if(cornerUnit._typeNum==-1){
//如果拐角满足条件可以联结,则返回成功,否则,判断另一个拐角
var result1,result2:Number;
if(cornerUnit.xNo==preUnit.xNo){
result1 = compare(cornerUnit.xNo,cornerUnit.yNo,preUnit.yNo,"y");
}
if(cornerUnit.yNo==preUnit.yNo){
result1 = compare(cornerUnit.yNo,cornerUnit.xNo,preUnit.xNo,"x");
}
if(cornerUnit.xNo==nextUnit.xNo){
result2 = compare(cornerUnit.xNo,cornerUnit.yNo,nextUnit.yNo,"y");
}
if(cornerUnit.yNo==nextUnit.yNo){
result2 = compare(cornerUnit.yNo,cornerUnit.xNo,nextUnit.xNo,"x");
}
//result,result,result,result,result,result,result,result,result,result;
//trace("result1="+result1);
//trace("result2="+result2);
if(result1==1 && result2==1){
return true;
}else{
//如果中间有中断,返回不成功
return false;
}
}else{
//如果拐角不为空,则直接返回不成功
return false;
}
}
private function isOneCornerLink(preUnit,nextUnit:mapUnit):Boolean{
//trace("preUnit.xNo="+preUnit.xNo);
//trace("preUnit.yNo="+preUnit.yNo);
//trace("nextUnit.xNo="+nextUnit.xNo);
//trace("nextUnit.yNo="+nextUnit.yNo);
//找到拐点
var oneCornerUnit:mapUnit = getUnit(preUnit.xNo,nextUnit.yNo);
var oneTest:Boolean = checkOneCornerLink(oneCornerUnit,preUnit,nextUnit);
//trace("oneTest="+oneTest);
//如果此拐点连接不成功
if(!oneTest){
//找到另一个拐点
var oneCornerUnit2:mapUnit = getUnit(nextUnit.xNo,preUnit.yNo);
//trace("oneCornerUnit2._typeNum="+oneCornerUnit2._typeNum);
return checkOneCornerLink(oneCornerUnit2,preUnit,nextUnit);
}else{
return oneTest;
}
}
private function removeUnit(unit:mapUnit){
//trace("unit._typeNum="+unit._typeNum);
//trace(unit.graphic.getChildByName(unit._typeNum));
unit.graphic.removeChild(unit.graphic.getChildByName(unit._typeNum));
//delete unit.graphic.onPress;
unit.removeMouseEvent();
unit.setTypeNum(-1);
}
//判断两点之间图片是否全为空,1表示为空,可以连接,2表示不为空,中断连接
private function compare(variable,preNo,nextNo:Number,type:String):Number{
//trace("in compare type="+type);
if(preNo<nextNo){
min = preNo;
max = nextNo;
}else{
min = nextNo;
max= preNo;
}
//trace("min="+min);
//trace("max="+max);
if(max-min==1){
return 1;
}else{
var tempUnit:mapUnit;
for(var i:Number = min+1;i<max;i++){
if(type=="x"){
tempUnit = getUnit(i,variable);
}else if(type=="y"){
tempUnit = getUnit(variable,i);
}
//trace("tempUnit._typeNum="+tempUnit._typeNum);
//trace("tempUnit._xNo="+tempUnit.xNo);
//trace("tempUnit._yNo="+tempUnit.yNo);
if(tempUnit._typeNum!=-1){
return 2;
}
}
return 1;
}
}
private function isBeeLineLink(preUnit,nextUnit:mapUnit):Number{
if(preUnit.xNo==nextUnit.xNo){
return compare(preUnit.xNo,preUnit.yNo,nextUnit.yNo,"y");
}
if(preUnit.yNo==nextUnit.yNo){
return compare(preUnit.yNo,preUnit.xNo,nextUnit.xNo,"x");
}
if(preUnit.yNo!=nextUnit.yNo&&preUnit.xNo!=nextUnit.xNo){
return 0;
}
}
private function getUnit(xNo,yNo:Number):mapUnit{
//trace("getUnit xNo="+xNo);
//trace("getUnit yNo="+yNo);
var num:Number= yNo*heightSize+xNo;
return mapArray[num];
}
private function pushToOneCornerArray(preUnit:mapUnit):void{
//trace("left***************************");
for(var i:Number = preUnit.xNo-1;i>-1;i--){
var unitLeft:mapUnit = getUnit(i,preUnit.yNo);
//图片不为空
//trace("unitLeft._typeNum="+unitLeft._typeNum);
if(unitLeft._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
oneCornerArray.push(unitLeft);
}
}
//trace("right***************************");
//trace("oneCornerArray.length="+oneCornerArray.length);
for(var j:Number = preUnit.xNo+1;j<widthSize;j++){
var unitRight:mapUnit = getUnit(j,preUnit.yNo);
//trace("unitRight._typeNum="+unitRight._typeNum);
//trace("unitRight.xNo="+unitRight.xNo);
//trace("unitRight.yNo="+unitRight.yNo);
//图片不为空
if(unitRight._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
oneCornerArray.push(unitRight);
}
}
//trace("up***************************");
//trace("oneCornerArray.length="+oneCornerArray.length);
for(var m:Number = preUnit.yNo-1;m>-1;m--){
var unitUp:mapUnit = getUnit(preUnit.xNo,m);
//trace("unitUp._typeNum="+unitUp._typeNum);
//图片不为空
if(unitUp._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
oneCornerArray.push(unitUp);
}
}
//trace("down***************************");
//trace("oneCornerArray.length="+oneCornerArray.length);
for(var n:Number = preUnit.yNo+1;n<heightSize;n++){
var unitDown:mapUnit= getUnit(preUnit.xNo,n);
//trace("unitDown._typeNum="+unitDown._typeNum);
//图片不为空
if(unitDown._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
oneCornerArray.push(unitDown);
}
}
//trace("oneCornerArray.length="+oneCornerArray.length);
}
private function pushToTwoCornerArray(nextUnit:mapUnit):void{
//trace("Two corner left***************************");
for(var i:Number = nextUnit.xNo-1;i>-1;i--){
var unitLeft = getUnit(i,nextUnit.yNo);
//图片不为空
if(unitLeft._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
twoCornerArray.push(unitLeft);
}
}
//trace("Two corner right***************************");
for(var j:Number = nextUnit.xNo+1;j<widthSize;j++){
var unitRight = getUnit(j,nextUnit.yNo);
//trace("unitRight._typeNum="+unitRight._typeNum);
//trace("unitRight.xNo="+unitRight.xNo);
//trace("unitRight.yNo="+unitRight.yNo);
//图片不为空
if(unitRight._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
twoCornerArray.push(unitRight);
}
}
//trace("Two corner up***************************");
for(var m:Number = nextUnit.yNo-1;m>-1;m--){
var unitUp = getUnit(nextUnit.xNo,m);
//图片不为空
if(unitUp._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
twoCornerArray.push(unitUp);
}
}
//trace("Two corner down***************************");
for(var n:Number = nextUnit.yNo+1;n<heightSize;n++){
var unitDown = getUnit(nextUnit.xNo,n);
//图片不为空
if(unitDown._typeNum!=-1){
break;
}else{
//图片为空,则把这个位置的unit放入数组作为corner考虑
twoCornerArray.push(unitDown);
}
}
}
}
}
Flash 动画
:
0
评论
Comments
日志分类
首页
[116]
Flash&AS2
[56]
FMS
[3]
Flex&AS3
[5]
Asp
[5]
My Works
[13]
My Life
[34]