主题:moz帮个忙,五子棋判断函数
jyf1987
[专家分:930] 发布于 2007-07-11 20:42:00
需要一个函数来检查五子棋是否练成五子了
传入的变量是坐标x y和黑白棋的mode
全局变量随便你用字符还是数组
返回true or false
回复列表 (共5个回复)
沙发
Matodied [专家分:7560] 发布于 2007-07-11 21:34:00
只要判断这5个子的坐标是不是符合下面3中情况之一:
1、5个坐标的x都一样,y是公差为1或-1的等差数列。
2、5个坐标的y都一样,x是公差为1或-1的等差数列。
3、5个坐标的x和y都是公差为1或-1的等差数列。
符合一个就是TRUE,都不符合就是FALSE。
板凳
moz [专家分:37620] 发布于 2007-07-11 21:45:00
function WU(x,y,m)
z1=1
for i=x-1 to 0 step -1
if p(i,y)=m then z1=z1+1 else exit for
next
for i=x+1 to 14
if p(i,y)=m then z1=z1+1 else exit for
next
if z1>=5 then
WU=-1
exit function
endif
z2=1
for i=y-1 to 0 step -1
if p(x,i)=m then z2=z2+1 else exit for
next
for i=y+1 to 14
if p(x,i)=m then z2=z2+1 else exit for
next
if z2>=5 then
WU=-1
exit function
endif
z3=1
z4=1
T1=-1
T2=-1
T3=-1
T4=-1
for i=1 to 4
if x+i<=14 and y+i<=14 then if p(x+i,y+i)=m and T1 then z3=z3+1 else T1=0
if x-i>=0 and y-i>=0 then if p(x-i,y-i)=m and T2 then z3=z3+1 else T2=0
if x+i<=14 and y-i>=0 then if p(x+i,y-i)=m and T3 then z4=z4+1 else T3=0
if x-i>=0 and y+i<=14 then if p(x-i,y+i)=m and T4 then z4=z4+1 else T4=0
next
if z3>=5 or z4>=5 then WU=-1
end function
3 楼
moz [专家分:37620] 发布于 2007-07-11 21:55:00
其实循环是可以合并的,这样会显得简洁一点,
但基于速度问题以及大多数问题,2楼有可能会快上一点点。
function WU(x,y,m)
for i=1 to 4
if x-i>=0 then if p(x-i,y)=m and T1=0 then z1=z1+1 else T1=-1
if x+i<=14 then if p(x+i,y)=m and T2=0 then z1=z1+1 else T2=-1
if y-i>=0 then if p(x,y-i)=m and T3=0 then z2=z2+1 else T3=-1
if y+i<=14 then if p(x,y+i)=m and T4=0 then z2=z2+1 else T4=-1
if x+i<=14 and y+i<=14 then if p(x+i,y+i)=m and T5=0 then z3=z3+1 else T5=-1
if x-i>=0 and y-i>=0 then if p(x-i,y-i)=m and T6=0 then z3=z3+1 else T6=-1
if x+i<=14 and y-i>=0 then if p(x+i,y-i)=m and T7=0 then z4=z4+1 else T7=-1
if x-i>=0 and y+i<=14 then if p(x-i,y+i)=m and T8=0 then z4=z4+1 else T8=-1
next
if z1>=4 or z2>=4 or z3>=4 or z4>=4 then WU=-1
end function
4 楼
jyf1987 [专家分:930] 发布于 2007-07-13 16:12:00
恩
谢谢moz
我去试下
5 楼
wzc1996 [专家分:1680] 发布于 2007-07-16 09:12:00
[quote]function WU(x,y,m)
z1=1
for i=x-1 to 0 step -1
if p(i,y)=m then z1=z1+1 else exit for
next
for i=x+1 to 14
if p(i,y)=m then z1=z1+1 else exit for
next
if z1>=5 then
WU=-1
exit function
endif
z2=1
for i=y-1 to 0 step -1
if p(x,i)=m then z2=z2+1 else exit for
next
for i=y+1 to 14
if p(x,i)=m then z2=z2+1 else exit for
next
if z2>=5 then
WU=-1
exit function
endif
z3=1
z4=1
T1=-1
T2=-1
T3=-1
T4=-1
for i=1 to 4
if x+i<=14 and y+i<=14 then if p(x+i,y+i)=m and T1 then z3=z3+1 else T1=0
if x-i>=0 and y-i>=0 then if p(x-i,y-i)=m and T2 then z3=z3+1 else T2=0
if x+i<=14 and y-i>=0 then if p(x+i,y-i)=m and T3 then z4=z4+1 else T3=0
if x-i>=0 and y+i<=14 then if p(x-i,y+i)=m and T4 then z4=z4+1 else T4=0
next
if z3>=5 or z4>=5 then WU=-1
end function[/quote]
在if p(i,y)=m then z1=z1+1 else exit for这里有问题 !数组没有定义!
我来回复