连连看作为一个经典的游戏受到很多上班族的喜爱
我老婆就很喜欢玩这个游戏,好,给她做一个
做起来也不难,只要知道实现的原理,不管用什么语言都很容易实现
先看效果
怎么去实现呢?一下是我的实现原理
1. 加载图片将图片
2. 创造一个二维数组,保存成对的随机图片,并显示在屏幕上
3. 连接算法。
算法是关键,我的思路很简单,首先判断是不是在一条直线上
然后再判断经过两个折线是不是能连接,最后判断经过三条折线是不是能够连接
以下是核心代码
private function SeekRoad():Array
{
var road:Array = new Array();
//在一条直线上
if(IsThrough(this.firstClick_.ix,this.firstClick_.iy,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.firstClick_.ix,this.firstClick_.iy));
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
for(var i:uint=0;i<GV.WidthNum+2;i++)
{
if(this.map_[i][this.firstClick_.iy].Type == 0 && IsXThrough(this.firstClick_.ix,this.firstClick_.iy,i,this.firstClick_.iy))
{
road = new Array();
road.push(GetPathPoint(this.firstClick_.ix,this.firstClick_.iy));
road.push(GetPathPoint(i,this.firstClick_.iy));
//两折
if(IsYThrough(i,this.firstClick_.iy,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
//三折
if(this.map_[i][this.secondClick_.iy].Type == 0 && IsYThrough(i,this.firstClick_.iy,i,this.secondClick_.iy))
{
road.push(GetPathPoint(i,this.secondClick_.iy));
if(IsXThrough(i,this.secondClick_.iy,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
}
}
}
for(var j:uint=0;j<GV.HeightNum+2;j++)
{
if(this.map_[this.firstClick_.ix][j].Type == 0 && IsYThrough(this.firstClick_.ix,this.firstClick_.iy,this.firstClick_.ix,j))
{
road = new Array();
road.push(GetPathPoint(this.firstClick_.ix,this.firstClick_.iy));
road.push(GetPathPoint(this.firstClick_.ix,j));
//两折
if(IsXThrough(this.firstClick_.ix,j,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
//三折
if(this.map_[this.secondClick_.ix][j].Type == 0 && IsXThrough(this.firstClick_.ix,j,this.secondClick_.ix,j))
{
road.push(GetPathPoint(this.secondClick_.ix,j));
if(IsYThrough(this.secondClick_.ix,j,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
}
}
}
return new Array();
}
{
var road:Array = new Array();
//在一条直线上
if(IsThrough(this.firstClick_.ix,this.firstClick_.iy,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.firstClick_.ix,this.firstClick_.iy));
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
for(var i:uint=0;i<GV.WidthNum+2;i++)
{
if(this.map_[i][this.firstClick_.iy].Type == 0 && IsXThrough(this.firstClick_.ix,this.firstClick_.iy,i,this.firstClick_.iy))
{
road = new Array();
road.push(GetPathPoint(this.firstClick_.ix,this.firstClick_.iy));
road.push(GetPathPoint(i,this.firstClick_.iy));
//两折
if(IsYThrough(i,this.firstClick_.iy,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
//三折
if(this.map_[i][this.secondClick_.iy].Type == 0 && IsYThrough(i,this.firstClick_.iy,i,this.secondClick_.iy))
{
road.push(GetPathPoint(i,this.secondClick_.iy));
if(IsXThrough(i,this.secondClick_.iy,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
}
}
}
for(var j:uint=0;j<GV.HeightNum+2;j++)
{
if(this.map_[this.firstClick_.ix][j].Type == 0 && IsYThrough(this.firstClick_.ix,this.firstClick_.iy,this.firstClick_.ix,j))
{
road = new Array();
road.push(GetPathPoint(this.firstClick_.ix,this.firstClick_.iy));
road.push(GetPathPoint(this.firstClick_.ix,j));
//两折
if(IsXThrough(this.firstClick_.ix,j,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
//三折
if(this.map_[this.secondClick_.ix][j].Type == 0 && IsXThrough(this.firstClick_.ix,j,this.secondClick_.ix,j))
{
road.push(GetPathPoint(this.secondClick_.ix,j));
if(IsYThrough(this.secondClick_.ix,j,this.secondClick_.ix,this.secondClick_.iy))
{
road.push(GetPathPoint(this.secondClick_.ix,this.secondClick_.iy));
return road;
}
}
}
}
return new Array();
}
代码还未优化,仅供参考