java生命游戏_大神们 急求一个生命游戏的java代码 谢谢各路大神!

这个程序检查细胞自动机的模式是否为空、稳定或振荡。它能识别不同速度的空间船和骑士船,并根据规则判断周期。程序还会在检测到模式变化时调整视图,每秒展示一次更新。若模式为空则显示为空,稳定则显示为稳定,振荡则显示其周期。此外,它特别处理了B0但非S8规则的情况。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

不知道啥语言

def show_spaceship_speed(period, deltax, deltay):

# we found a moving oscillator

if period == 1:

g.show("Spaceship detected (speed = c)")

elif (deltax == deltay) or (deltax == 0) or (deltay == 0):

speed = ""

if (deltax == 0) or (deltay == 0):

# orthogonal spaceship

if (deltax > 1) or (deltay > 1):

speed += str(deltax + deltay)

else:

# diagonal spaceship (deltax == deltay)

if deltax > 1:

speed += str(deltax)

g.show("Spaceship detected (speed = " + speed + "c/" +str(period) + ")")

else:

# deltax != deltay and both > 0

speed = str(deltay) + "," + str(deltax)

g.show("Knightship detected (speed = " + speed + "c/" + str(period) + ")")

# --------------------------------------------------------------------

def oscillating():

# return True if the pattern is empty, stable or oscillating

# first get current pattern's bounding box

prect = g.getrect()

pbox = rect(prect)

if pbox.empty:

g.show("The pattern is empty.")

return True

# get current pattern and create hash of "normalized" version -- ie. shift

# its top left corner to 0,0 -- so we can detect spaceships and knightships

## currpatt = pattern( g.getcells(prect) )

## h = hash( tuple( currpatt(-pbox.left, -pbox.top) ) )

# use Golly's hash command (3 times faster than above code)

h = g.hash(prect)

# check if outer-totalistic rule has B0 but not S8

rule = g.getrule().split(":")[0]

hasB0notS8 = rule.startswith("B0") and (rule.find("/") > 1) and not rule.endswith("8")

# determine where to insert h into hashlist

pos = 0

listlen = len(hashlist)

while pos < listlen:

if h > hashlist[pos]:

pos += 1

elif h < hashlist[pos]:

# shorten lists and append info below

del hashlist[pos : listlen]

del genlist[pos : listlen]

del poplist[pos : listlen]

del boxlist[pos : listlen]

break

else:

# h == hashlist[pos] so pattern is probably oscillating, but just in

# case this is a hash collision we also compare pop count and box size

if (int(g.getpop()) == poplist[pos]) and \

(pbox.wd == boxlist[pos].wd) and \

(pbox.ht == boxlist[pos].ht):

period = int(g.getgen()) - genlist[pos]

if hasB0notS8 and (period % 2 > 0) and (pbox == boxlist[pos]):

# ignore this hash value because B0-and-not-S8 rules are

# emulated by using different rules for odd and even gens,

# so it's possible to have identical patterns at gen G and

# gen G+p if p is odd

return False

if period == 1:

if pbox == boxlist[pos]:

g.show("The pattern is stable.")

else:

show_spaceship_speed(1, 0, 0)

elif pbox == boxlist[pos]:

g.show("Oscillator detected (period = " + str(period) + ")")

else:

deltax = abs(boxlist[pos].x - pbox.x)

deltay = abs(boxlist[pos].y - pbox.y)

show_spaceship_speed(period, deltax, deltay)

return True

else:

# look at next matching hash value or insert if no more

pos += 1

# store hash/gen/pop/box info at same position in various lists

hashlist.insert(pos, h)

genlist.insert(pos, int(g.getgen()))

poplist.insert(pos, int(g.getpop()))

boxlist.insert(pos, pbox)

return False

# --------------------------------------------------------------------

def fit_if_not_visible():

# fit pattern in viewport if not empty and not completely visible

r = rect(g.getrect())

if (not r.empty) and (not r.visible()): g.fit()

# --------------------------------------------------------------------

g.show("Checking for oscillation... (hit escape to abort)")

oldsecs = time()

while not oscillating():

g.run(1)

newsecs = time()

if newsecs - oldsecs >= 1.0: # show pattern every second

oldsecs = newsecs

fit_if_not_visible()

g.update()

fit_if_not_visible()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值