八数码游戏就是通过移动空格把数字从给定的状态移动到目标状态,例如:
初始状态为 目标状态为
2 8 3 1 2 3
1 6 4 8 4
7 5 7 6 5
程序中用0表示空格,提供了两套状态。
EightNums.rb
class EightNums
#常量定义
UP = 1
DOWN = -1
LEFT = 2
RIGHT = -2
ROW = 0
COL = 1
#初始化
@@InitStat = [[2,8,3], [1,6,4], [7,0,5]] #初始状态
@@TargetStat = [[1,2,3], [8,0,4], [7,6,5]] #目标状态
@@Stat = [[2,8,3], [1,6,4], [7,0,5]] #初始状态
@@ZeroPos = [2, 1] #0的位置
# @@InitStat = [[2,0,3], [1,4,5], [6,7,8]] #初始状态
# @@TargetStat = [[1,2,3], [4,0,5], [6,7,8]] #目标状态
# @@Stat = [[2,0,3], [1,4,5], [6,7,8]] #初始状态
# @@ZeroPos = [0, 1] #0的位置
def run()
puts "Initial status:"
printStat(@@InitStat)
min = getValue(@@Stat)
puts "Target status:"
printStat(@@TargetStat)
puts "Initial value: #{min}",' '
steps = 0
lastmove = -1 #记录上次移动方向,避免反复
while steps < 10 do
flag = false #是否找到比min小的值
#寻找最小值,确定移动方向
direction = UP
[UP, DOWN, LEFT, RIGHT].each do
|i|
val = tryMoving(i)
if val != -1
if val < min && i != -lastmove then
min = val
flag = true
direction = i
end
end
end
#移动
moveZero(direction)
lastmove = direction
printStat(@@Stat)
puts "Current value: #{getValue(@@Stat)}"
if !flag
min = getValue(@@Stat)
end
steps += 1
break if getValue(@@Stat) == 0
end
end
private
def getValue(status)
value = 0
for row in 0..2 do
for col in 0..2 do
if status[row][col] != @@TargetStat[row][col]
value += 1
end
end
end
return value
end
#移动0
def moveZero(direction)
case direction
when UP #上移
if @@ZeroPos[ROW] != 0
@@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL]] = @@Stat[@@ZeroPos[ROW] - 1][@@ZeroPos[COL]]
@@Stat[@@ZeroPos[ROW] - 1][@@ZeroPos[COL]] = 0
@@ZeroPos[ROW] -= 1
else
return -1
end
when DOWN #下移
if @@ZeroPos[ROW] != 2
@@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL]] = @@Stat[@@ZeroPos[ROW] + 1][@@ZeroPos[COL]]
@@Stat[@@ZeroPos[ROW] + 1][@@ZeroPos[COL]] = 0
@@ZeroPos[ROW] += 1
else
return -1
end
when LEFT #左移
if @@ZeroPos[COL] != 0
@@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL]] = @@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL] - 1]
@@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL] - 1] = 0
@@ZeroPos[COL] -= 1
else
return -1
end
when RIGHT #右移
if @@ZeroPos[COL] != 2
@@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL]] = @@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL] + 1]
@@Stat[@@ZeroPos[ROW]][@@ZeroPos[COL] + 1] = 0
@@ZeroPos[COL] += 1
else
return -1
end
end
return 0
end
#打印状态
def printStat(status)
3.times {|i| print status[i][0], ' ', status[i][1], ' ', status[i][2], "\n"}
end
#尝试移动,移动不成功则返回-1
def tryMoving(direction)
success = moveZero(direction)
if success == 0
value = getValue(@@Stat)
moveZero(-direction)
return value
else
return -1
end
end
end
Num = EightNums.new
Num.run()
分享到:
相关推荐
ruby实现邮件收发
本资源是飞机大战类射击游戏的ruby实现,需要安装rpg maker xp 1.03才可打开本游戏。
twitter-cldr-rb - ICU的Ruby实现
基于Ruby实现的管理游戏信息,以及游戏映射信息的系统
ruby-snmp, SNMP ( 简单网络管理协议)的ruby 实现 用于 ruby的 SNMP库摘要这里库实现 SNMP ( 简单网络管理协议) 。 它在纯 ruby 中实现,因此不依赖于的外部库( 如 ) 。 你可以在 ruby 可以运行的任何地方运行这里库...
ruby实现文件对比资料收集,看完之后写一个文件对比的脚本或者输出报告没有问题,可以看看,
Whois一个智能纯Ruby实现的WHOIS客户端与解析器
Http-2 - HTTP/2协议的纯Ruby实现
基于ruby实现的音乐推荐网站.zip基于ruby实现的音乐推荐网站.zip基于ruby实现的音乐推荐网站.zip
使用Python Lua和Ruby语言进行游戏编程
ruby_使用ruby实现的排序算法_sorting
ruby_使用ruby实现的算法之冒泡排序
ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码ruby代码
ruby_使用ruby实现的算法之加密解密算法
CuckooFilter是Cuckoo Filter的纯Ruby实现
用ROR框架写的,用分页查询,查找,插件等一些功能。
了解游戏脚本编程的基础,初步了解ruby的语法和基本用法。联系python和lua(WOW用到的脚本语言),让你透视脚本语言的奥秘!
一些图形算法的Ruby实现_Ruby_下载.zip