as3.0-------------数字连连看的实现
这里用到2个文件,.fla和.ActionScripte。这两个文件一定要放在同一个文件夹下
第一:创建一个名为LMC的ActionScripte文件,代码如下:
package
{
import flash.display.MovieClip
import flash.text.TextField
import flash.display.Sprite
import flash.events.MouseEvent
public class LMC extends MovieClip
{
public function LMC(str:String):void
{
init(str)
}
private function init(str:String):void
{
txt.mouseEnabled = false
txt.selectable = false
txt.text = str
this.addEventListener(MouseEvent.MOUSE_OUT,out)
this.addEventListener(MouseEvent.MOUSE_OVER,over)
}
private function out(evt:MouseEvent):void
{
}
private function over(evt:MouseEvent):void
{
}
}
}
第二:创建一个.fla文件(文件名我这里是lianliankan,随个人习惯),在舞台创建2个文本其中动态文本的实例名为“num”,
第三,新建元件名为LMC的影片剪辑,里面创建三个图层,背景第四帧只要改一下颜色为蓝色就行了,至于代码层都为 stop()
第四,打开库,右击LMC----->链接----->链接属性的类名改为:LMC,其他不变----->确定
第五,回到舞台,创建一个代码图层,如图:
第六,代码如下:
import LMC//导入LMC类
var num_i:uint = 0
var lmc:LMC//缓存用的
var arr:Array=new Array()
var c:Array=new Array()
var left:uint = 0
var top:uint = 0
for (var i:uint=0i<102i++)
{
var a:LMC = new LMC(String(Math.floor(Math.random() * 50)))
if (left+a.width>550)
{
left = 0
top += a.height
}
a.x = left
a.y = top
left += a.width
addChild(a)
arr.push(a)
a.addEventListener(MouseEvent.MOUSE_DOWN,dian)
a.addEventListener(MouseEvent.MOUSE_OVER,over)
a.addEventListener(MouseEvent.MOUSE_OUT,out)
}
function out(evt:MouseEvent):void
{
evt.currentTarget.txt.textColor = 0x0000ff
}
function over(evt:MouseEvent):void
{
evt.currentTarget.txt.textColor = 0xff0000
}
function dian(evt:MouseEvent):void
{
evt.currentTarget.gotoAndPlay(2)
if ((evt.currentTarget as LMC)==lmc)
{
return
}
if (lmc==null)
{
lmc = evt.currentTarget as LMC
}
else
{
if (lmc.txt.text == evt.currentTarget.txt.text)
{
num_i += 2
num.text = String(num_i)
removeChild(lmc)
removeChild((evt.currentTarget as LMC))
lmc = null
}
else
{
evt.currentTarget.gotoAndPlay(1)
lmc.gotoAndPlay(1)
lmc = null
}
}
}
连连看自动寻道算法onClipEvent (load) {
_root.yb._alpha = 0
var zt = new Array()
var jilu1 = new Array()
var jilu2 = new Array()
var jilu3 = new Array()
var jilu4 = new Array()
b = 0
val = 1
for (i=0i<12i++) {
for (j=0j<12j++) {
a = attachMovie("ttt", "ttt"+val, val)
a._x = j*32-50
a._y = i*32-80
a.onPress = pres
a.onRollOut = rele
a.n = val
zt[val] = 1
//初始为第一帧;
val++
}
}
for (i=1i<=12i++) {
tellTarget ("ttt"+i) {
gotoAndStop(9)
}
zt[i] = 9
}
for (i=133i<=144i++) {
tellTarget ("ttt"+i) {
gotoAndStop(9)
}
zt[i] = 9
}
for (i=1i<=133i=i+12) {
tellTarget ("ttt"+i) {
gotoAndStop(9)
}
zt[i] = 9
}
for (i=12i<=144i=i+12) {
tellTarget ("ttt"+i) {
gotoAndStop(9)
}
zt[i] = 9
}
for (i=1i<=137i++) {
if (zt[i] != 9) {
ra = random(10)+1
zt[i] = ra
selectframe(ra)
}
}
//列出矩阵;
function pres() {
t = this.n
if (b != 0) {
_root.yb._alpha = 0
//c2=this._currentframe
first = b
second = this.n
ss=zt[second]
if(bb==ss){
//记录判断点类型
line1_x = getProperty("ttt"+first, _x)
line1_y = getProperty("ttt"+first, _y)
line2_x = getProperty("ttt"+second, _x)
line2_y = getProperty("ttt"+second, _y)
if (line1_x == line2_x) {
line4 = "同一列"
_root.control.panduan_lie()
} else if (line1_y == line2_y) {
_root.control.panduan_hang()
line4 = "同一行"
} else {
_root.control.panduan_cuowei()
line4 = "错开位"
}
}
//判断两个点是否在同一列上;
//c=this._currentframe
b = 0
} else {
_root.yb._alpha = 50
_root.yb._x = this._x+546
_root.yb._y = this._y+5
b = this.n
bb=zt[b]
//this.gotoandstop(2)
c = this._currentframe
}
}
function panduan_lie() {
g = 1000
br = 0
//两点在同一列上的处理函数;
if (math.abs(first-second) == 12) {
_root.control.ok()
} else {
//1
line1 = Math.floor(first/12)
line2 = Math.floor(second/12)
for (i=1i<=12i++) {
jilu1[i] = line1*12+i
jilu2[i] = line2*12+i
}
for (i=1i<=12i++) {
//2
if (jilu1[i] != first) {
if (zt[jilu1[i]] == 9 and zt[jilu2[i]] == 9) {
l = jiance_lie1(jilu1[i], jilu2[i])
if (l>0) {
if (l<g) {
g = l
g1 = jilu1[i]
g2 = jilu2[i]
}
//_root.control.ok()
}
}
} else {
l = jiance_lie2(jilu1[i], jilu2[i])
if (l == 100) {
_root.control.shuxian0(jilu1[i], jilu2[i])
br = 1
break
}
}
}
if (g != 1000 and br == 0) {
guaixian2_lie(g1, g2)
}
//2
}
//1
}
function panduan_hang() {
g = 1000
br = 0
//两点在同一行上的处理函数;
if (math.abs(first-second) == 1) {
_root.control.ok()
} else {
line1 = first%12
line2 = second%12
for (i=1i<=12i++) {
jilu1[i] = line1+(i-1)*12
jilu2[i] = line2+(i-1)*12
}
for (i=1i<=12i++) {
if (jilu1[i] != first) {
if (zt[jilu1[i]] == 9 and zt[jilu2[i]] == 9) {
f = jiance_hen1(jilu1[i], jilu2[i])
if (f>0) {
if (f<g) {
g = f
g1 = jilu1[i]
g2 = jilu2[i]
}
}
}
} else {
f = jiance_hen2(jilu1[i], jilu2[i])
if (f == 100) {
_root.control.henxiang0(jilu1[i], jilu2[i])
br = 1
break
}
}
}
}
if (g != 1000 and br == 0) {
guaixian2_hen(g1, g2)
}
}
function jiance_hen1(x, y) {
//检测两点是否为通路的函数;
p1 = 0
p2 = 0
p3 = 0
co = 0
if (x>y) {
//1
dis = x-y-1
for (z=1z<=disz++) {
q = x-z
if (zt[q] == 9) {
p1++
}
}
} else {
dis = y-x-1
for (z=1z<=disz++) {
q = y-z
if (zt[q] == 9) {
p1++
}
}
}
//1
if (p1 == dis) {
p1 = 100
}
//横向检测;<-
if (first>x) {
dis = (first-x)/12-1
if (dis == 0) {
p2 = 100
} else {
for (z=1z<=disz++) {
q = first-z*12
if (zt[q] == 9) {
p2++
}
}
}
} else {
dis = (x-first)/12-1
if (dis == 0) {
p2 = 100
} else {
for (z=1z<=disz++) {
q = x-z*12
if (zt[q] == 9) {
p2++
}
}
}
}
if (p2 == dis) {
p2 = 100
}
if (second>y) {
dis = (second-y)/12-1
if (dis == 0) {
p3 = 100
} else {
for (z=1z<=disz++) {
q = second-z*12
if (zt[q] == 9) {
p3++
}
}
}
} else {
dis = (y-second)/12-1
if (dis == 0) {
p3 = 100
} else {
for (z=1z<=disz++) {
q = y-z*12
if (zt[q] == 9) {
p3++
}
}
}
}
if (p3 == dis) {
p3 = 100
}
p = p1+p2+p3
if (p == 300) {
co = math.abs(x-y)+math.abs(second-y)/12+math.abs(first-x)/12
}
return (co)
}
function jiance_hen2(x, y) {
p4 = 0
if (x>y) {
dis = x-y-1
for (z=1z<=disz++) {
q = x-z
if (zt[q] == 9) {
p4++
}
}
} else {
dis = y-x-1
for (z=1z<=disz++) {
q = y-z
if (zt[q] == 9) {
p4++
}
}
}
if (p4 == dis) {
p4 = 100
}
return (p4)
}
function jiance_lie1(x, y) {
p1 = 0
p2 = 0
p3 = 0
co = 0
if (x>y) {
dis = (x-y)/12-1
for (z=1z<=disz++) {
q = x-z*12
if (zt[q] == 9) {
p1++
}
}
} else {
dis = (y-x)/12-1
for (z=1z<=disz++) {
q = y-z*12
if (zt[q] == 9) {
p1++
}
}
}
if (p1 == dis) {
p1 = 100
}
//纵向检测《---
if (first>x) {
dis = first-x-1
for (z=1z<=disz++) {
q = first-z
if (zt[q] == 9) {
p2++
}
}
} else {
dis = x-first-1
for (z=1z<=disz++) {
q = x-z
if (zt[q] == 9) {
p2++
}
}
}
if (p2 == dis) {
p2 = 100
}
if (second>y) {
dis = second-y-1
for (z=1z<=disz++) {
q = second-z
if (zt[q] == 9) {
p3++
}
}
} else {
dis = y-second-1
for (z=1z<=disz++) {
q = y-z
if (zt[q] == 9) {
p3++
}
}
}
if (p3 == dis) {
p3 = 100
}
p = p1+p2+p3
if (p == 300) {
co = math.abs(x-y)/12+math.abs(second-y)+math.abs(first-x)
}
return (co)
}
function jiance_lie2(x, y) {
p4 = 0
if (x>y) {
dis = (x-y)/12-1
for (z=1z<=disz++) {
q = x-z*12
if (zt[q] == 9) {
p4++
}
}
} else {
dis = (y-x)/12-1
for (z=1z<=disz++) {
q = y-z*12
if (zt[q] == 9) {
p4++
}
}
}
if (p4 == dis) {
p4 = 100
}
return (p4)
}
function panduan_cuowei() {
g = 1000
gg = 1000
br = 0
j = 0
k = 0
line1 = first%12
line2 = second%12
line3 = math.floor(first/12)
line4 = math.floor(second/12)
for (i=1i<=12i++) {
jilu1[i] = line1+(i-1)*12
jilu2[i] = line2+(i-1)*12
//HANG
jilu3[i] = line3*12+i
jilu4[i] = line4*12+i
//LIE
}
for (i=1i<=12i++) {
if (jilu1[i] == first) {
if (zt[jilu2[i]] == 9) {
w = jiance_cuowei01(jilu2[i])
if (w == 200) {
guaixian1_hen1(jilu2[i])
_root.control.ok()
br = 1
break
}
}
} else if (jilu2[i] == second) {
if (zt[jilu1[i]] == 9) {
w = jiance_cuowei02(jilu1[i])
if (w == 200) {
guaixian1_hen2(jilu1[i])
_root.control.ok()
br = 1
break
}
}
}
if (zt[jilu1[i]] == 9 and zt[jilu2[i]] == 9) {
j = jiance_hen1(jilu1[i], jilu2[i])
}
if (j>0) {
if (j<g) {
g = j
g1 = jilu1[i]
g2 = jilu2[i]
}
//_root.control.ok()
} else {
if (zt[jilu3[i]] == 9 and zt[jilu4[i]] == 9) {
k = jiance_lie1(jilu3[i], jilu4[i])
}
if (k>0) {
if (k<gg) {
gg = k
g3 = jilu3[i]
g4 = jilu4[i]
}
}
}
}
if (br == 0) {
if (g != 1000) {
guaixian2_hen(g1, g2)
br = 1
} else if (gg != 1000) {
guaixian2_lie(g3, g4)
}
}
}
function jiance_cuowei01(x) {
//错位状态1
p1 = 0
p2 = 0
dis = math.abs(x-first)-1
if (dis == 0) {
p1 = 100
} else {
if (x>first) {
for (z=1z<=disz++) {
q = x-z
if (zt[q] == 9) {
p1++
}
}
} else {
for (z=1z<=disz++) {
q = first-z
if (zt[q] == 9) {
p1++
}
}
}
if (p1 == dis) {
p1 = 100
}
}
dis = math.abs((second-x))/12-1
if (dis == 0) {
p2 == 100
} else {
if (second>x) {
for (z=1z<=disz++) {
q = second-z*12
if (zt[q] == 9) {
p2++
}
}
} else {
dis = math.abs(dis)
for (z=1z<=disz++) {
q = x-z*12
if (zt[q] == 9) {
p2++
}
}
}
}
if (p2 == dis) {
p2 = 100
}
return (p1+p2)
}
function jiance_cuowei02(x) {
//错位状态2
p1 = 0
p2 = 0
dis = math.abs(x-second)-1
if (dis == 0) {
p1 = 100
} else {
if (x>second) {
for (z=1z<=disz++) {
q = x-z
if (zt[q] == 9) {
p1++
}
}
} else {
for (z=1z<=disz++) {
q = second-z
if (zt[q] == 9) {
p1++
}
}
}
if (p1 == dis) {
p1 = 100
}
}
dis = math.abs((first-x))/12-1
if (dis == 0) {
p2 == 100
} else {
if (first>x) {
for (z=1z<=disz++) {
q = first-z*12
if (zt[q] == 9) {
p2++
}
}
} else {
dis = math.abs(dis)
for (z=1z<=disz++) {
q = x-z*12
if (zt[q] == 9) {
p2++
}
}
}
}
if (p2 == dis) {
p2 = 100
}
return (p1+p2)
}
function henxiang0(x, y) {
//横线0拐点画线函数;
//tellTarget ("ttt"+y) {
// gotoAndPlay(10)
//}
if (x>y) {
dis = x-y-1
for (z=1z<=disz++) {
q = x-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
} else {
dis = y-x-1
for (z=1z<=disz++) {
q = y-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
}
_root.control.ok()
}
function shuxian0(x, y) {
//竖线0拐点画线函数;
if (x>y) {
dis = (x-y)/12-1
for (z=1z<=disz++) {
q = x-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
} else {
dis = (y-x)/12-1
for (z=1z<=disz++) {
q = y-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
_root.control.ok()
}
function guaixian1_hen1(x) {
if (x>first and x<second) {
tellTarget ("ttt"+x) {
gotoAndPlay(19)
}
} else if (x>first and x>second) {
tellTarget ("ttt"+x) {
gotoAndPlay(25)
}
} else if (x<first and x<second) {
tellTarget ("ttt"+x) {
gotoAndPlay(16)
}
} else {
tellTarget ("ttt"+x) {
gotoAndPlay(22)
}
}
dis = math.abs(x-first)-1
if (dis != 0) {
if (x>first) {
for (z=1z<=disz++) {
q = x-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
} else {
for (z=1z<=disz++) {
q = first-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
}
}
dis = math.abs((second-x))/12-1
if (dis != 0) {
if (second>x) {
for (z=1z<=disz++) {
q = second-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
} else {
dis = math.abs(dis)
for (z=1z<=disz++) {
q = x-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
}
}
function guaixian1_hen2(x) {
if (x>first and x<second) {
tellTarget ("ttt"+x) {
gotoAndPlay(22)
}
} else if (x>first and x>second) {
tellTarget ("ttt"+x) {
gotoAndPlay(25)
}
} else if (x<first and x<second) {
tellTarget ("ttt"+x) {
gotoAndPlay(16)
}
} else {
tellTarget ("ttt"+x) {
gotoAndPlay(19)
}
}
dis = math.abs(x-second)-1
if (dis != 0) {
if (x>second) {
for (z=1z<=disz++) {
q = x-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
} else {
for (z=1z<=disz++) {
q = second-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
}
}
dis = math.abs((first-x))/12-1
if (dis != 0) {
if (first>x) {
for (z=1z<=disz++) {
q = first-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
} else {
dis = math.abs(dis)
for (z=1z<=disz++) {
q = x-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
}
}
function guaixian2_hen(x, y) {
if (x<first and y<second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(16)
}
tellTarget ("ttt"+y) {
gotoAndPlay(19)
}
} else if (x<first and y<second and x>y) {
tellTarget ("ttt"+x) {
gotoAndPlay(19)
}
tellTarget ("ttt"+y) {
gotoAndPlay(16)
}
} else if (x>first and y>second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(22)
}
tellTarget ("ttt"+y) {
gotoAndPlay(25)
}
} else if (x>first and y>second and x>y) {
tellTarget ("ttt"+x) {
gotoAndPlay(25)
}
tellTarget ("ttt"+y) {
gotoAndPlay(22)
}
} else if (x>first and y<second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(22)
}
tellTarget ("ttt"+y) {
gotoAndPlay(19)
}
} else if (x>first and y<second and x>y) {
tellTarget ("ttt"+x) {
gotoAndPlay(25)
}
tellTarget ("ttt"+y) {
gotoAndPlay(16)
}
} else if (x<first and y>second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(16)
}
tellTarget ("ttt"+y) {
gotoAndPlay(25)
}
} else {
tellTarget ("ttt"+x) {
gotoAndPlay(19)
}
tellTarget ("ttt"+y) {
gotoAndPlay(22)
}
}
if (x>y) {
dis = x-y-1
for (z=1z<=disz++) {
q = x-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
} else {
dis = y-x-1
for (z=1z<=disz++) {
q = y-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
}
if (first>x) {
dis = (first-x)/12-1
if (dis != 0) {
for (z=1z<=disz++) {
q = first-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
} else {
dis = (x-first)/12-1
if (dis != 0) {
for (z=1z<=disz++) {
q = x-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
}
if (second>y) {
dis = (second-y)/12-1
if (dis != 0) {
for (z=1z<=disz++) {
q = second-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
} else {
dis = (y-second)/12-1
if (dis != 0) {
for (z=1z<=disz++) {
q = y-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
}
_root.control.ok()
}
function guaixian2_lie(x, y) {
if (x<first and y<second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(16)
}
tellTarget ("ttt"+y) {
gotoAndPlay(22)
}
} else if (x<first and y<second and x>y) {
tellTarget ("ttt"+x) {
gotoAndPlay(22)
}
tellTarget ("ttt"+y) {
gotoAndPlay(16)
}
} else if (x>first and y>second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(19)
}
tellTarget ("ttt"+y) {
gotoAndPlay(25)
}
} else if (x>first and y>second and x>y) {
tellTarget ("ttt"+x) {
gotoAndPlay(25)
}
tellTarget ("ttt"+y) {
gotoAndPlay(19)
}
} else if (x>first and y<second and x>y) {
tellTarget ("ttt"+x) {
gotoAndPlay(25)
}
tellTarget ("ttt"+y) {
gotoAndPlay(16)
}
} else if (x>first and y<second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(19)
}
tellTarget ("ttt"+y) {
gotoAndPlay(22)
}
} else if (x<first and y>second and x<y) {
tellTarget ("ttt"+x) {
gotoAndPlay(16)
}
tellTarget ("ttt"+y) {
gotoAndPlay(25)
}
} else {
tellTarget ("ttt"+x) {
gotoAndPlay(22)
}
tellTarget ("ttt"+y) {
gotoAndPlay(19)
}
}
if (x>y) {
dis = (x-y)/12-1
for (z=1z<=disz++) {
q = x-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
} else {
dis = (y-x)/12-1
for (z=1z<=disz++) {
q = y-z*12
tellTarget ("ttt"+q) {
gotoAndPlay(13)
}
}
}
if (first>x) {
dis = first-x-1
for (z=1z<=disz++) {
q = first-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
} else {
dis = x-first-1
for (z=1z<=disz++) {
q = x-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
}
if (second>y) {
dis = second-y-1
for (z=1z<=disz++) {
q = second-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
} else {
dis = y-second-1
for (z=1z<=disz++) {
q = y-z
tellTarget ("ttt"+q) {
gotoAndPlay(10)
}
}
}
_root.control.ok()
}
function selectframe(number) {
switch (number) {
case 1 :
tellTarget ("ttt"+i) {
gotoAndStop(1)
}
break
case 2 :
tellTarget ("ttt"+i) {
gotoAndStop(2)
}
break
case 3 :
tellTarget ("ttt"+i) {
gotoAndStop(3)
}
break
case 4 :
tellTarget ("ttt"+i) {
gotoAndStop(4)
}
break
case 5 :
tellTarget ("ttt"+i) {
gotoAndStop(5)
}
break
case 6 :
tellTarget ("ttt"+i) {
gotoAndStop(6)
}
break
case 7 :
tellTarget ("ttt"+i) {
gotoAndStop(7)
}
break
case 8 :
tellTarget ("ttt"+i) {
gotoAndStop(8)
}
break
case 9 :
tellTarget ("ttt"+i) {
gotoAndStop(28)
}
break
case 10 :
tellTarget ("ttt"+i) {
gotoAndStop(29)
}
break
}
}
function ok() {
//可消除的处理函数;
tellTarget ("ttt"+first) {
gotoAndStop(9)
}
zt[first] = 9
tellTarget ("ttt"+second) {
gotoAndStop(9)
}
zt[second] = 9
c = zt[second]
}
}
//julu数组为待检测点,ZT数组为各点状态;
//9:消除;10:横线;13:竖线;16:「;19:7;22:L;25:」;
基本思路就是分两种情况考虑寻道算法,
为每一个小牌取个名字,(我用程序生成,并用数组记录下每个牌的状态,是无牌状态还是有牌状态)
一是在同行同列上
只要判断从起点到终点的所有牌的状态是否都为无牌状态,也就是起点牌到终点牌是否是连通的
二是只有一个拐点
先检测它们的所在行列所在的交汇点,判断他们的交绘点是否为无牌状态,如果是则继续判断从该交绘点分别到起点和终点间是否为连通,这时的方法同第一种方法一样.
三是有两个拐点
同样先判断这两个点所在行和列的两个交汇点,先判断两个交汇点间是否为空,若是则继续判断两个交汇点到起点终点是否为连通,这两中判断也可以分解为是第一种状态来进行判断
你慢慢研究吧
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)