ruby低级问题,来冲下人气
为解决如下问题:有一根300厘米的细木杆,在第30厘米、80厘米、110厘米、160厘米、250厘米这五个位置上各有一只蚂蚁。木杆很细,不能同时通过
两只蚂蚁。开始时,蚂蚁的头朝左还是朝右是任意的,它们只会朝前走或调头,但不会后退。当任意两只蚂蚁碰头时,两只蚂蚁会同
调头朝相反方向走。假设蚂蚁们每秒钟可以走5厘米的距离.
编程计算所以蚂蚁离开细杆用的最小时间和最大时间。
我的ruby代码如下,穷举开始时蚂蚁方向的各种组合。然后一一计算出来离开时间。
ruby-1.8.6 i386-mswin32
文件一ant_class.rb:
class Ant
attr_reader:posi
def initialize(direction,position,speed)
##direction must be 1 or -1
@direct = direction
@posi = position
@spd = speed
@tmintval = 1
end
def getpposition()
return @posi
end
def getdirect()
if @direct == -1
return "<-"
else
return "->"
end
end
def move()
@posi += @spd*@tmintval*@direct
end
def turnback()
@direct *= -1
end
end
文件二 stick_class.rb:
require"ant_class"
class Stick
def initialize(length)
@sticklen = length
@Ants = Array.new()
end
def putants(directs,posits)
for i in 0...posits.size()
@Ants = Ant.new(directs,posits,5)
end
end
##因为蚂蚁的移动速度是5,所以相对和相背移动两种情况中,
##任何两个蚂蚁之间的相对速度都是10,又任何两只蚂蚁的开始距离间隔是10的倍数。
##所以蚂蚁之间的距离只能是10的倍数。我们判断碰撞的方法就简单了
##只需要判断坐标相同就是了。
def anymeet()
for i in 0...@Ants.size()
if (i+1) == @Ants.size()
break
elsif (@Ants).getposition == @Ants.getposition
@Ants.turnback()
@Ants.turnback()
puts "ant"+i+"meet"+"ant"+(i+1)+"at"+ @Ants.getposition
else
print "."
end
end
end
def anyout()
for i in 0...@Ants.size()
if (@Ants).getposition >= @sticklen
@Ants.delete_at(i)
print "ant #{i} out"
end
if @Ants.getposition <= 0
@Ants.delete_at(i)
print "ant #{i} out"
end
end
end
def isallout()
if @Ants.size() == 0
return true
else
return false
end
end
def showdirects()
puts "directions:"
@Ants.each do |ant|
print ant.getdirect()+" "
end
puts ""
end
def showpositions()
puts "positions:"
@Ants.each do |ant|
printant.posi.to_s + " "
end
end
def run()
@time = 0
self.showdirects()
self.showpositions()
while !self.isallout
@time += 1
puts @time
@Ants.each{|ant| ant.move()}
self.anymeet()
self.anyout()
end
puts "========"
puts "time = #{@time}"
end
end
文件三 ant.rb
#####
#####
require "stick_class"
###begin
if __FILE__ == $0
directs = [-1,-1,-1,-1,-1]
positions =
time = 0
st = Stick.new(300)
#I hate this
for i in 0...(2**5)
if 0x10 & i == 0
directs= -1
else
directs = 1
end
if 0x08 & i == 0
directs= -1
else
directs= 1
end
if 0x04 & i == 0
directs = -1
else
directs = 1
end
if 0x02 & i == 0
directs = -1
else
directs = 1
end
if 0x01 & i == 0
directs = -1
else
directs = 1
end
puts "case #{i}"
st.putants(directs,positions)
st.run()
end
end
我的问题:
我使用的IDE是eclipse 在执行程序时报告如下错误:
./stick_class.rb:21:in `anymeet': undefined method `getposition' for #<Ant:0x2ba9058 @posi=25, @direct=-1, @tmintval=1, @spd=5> (NoMethodError)
from ./stick_class.rb:18:in `each'
from ./stick_class.rb:18:in `anymeet'
from ./stick_class.rb:77:in `run'
from D:/workspace/Ant/ant.rb:60
from D:/workspace/Ant/ant.rb:22:in `each'
from D:/workspace/Ant/ant.rb:22
很明显Ant类里面有getposition这个方法。
为什么会出现udefined method这种情况呢,而且在代码别处也有过这种错误,都是莫名其妙的消失了。
初学ruby才几天,求高人解答 ruby其实还是不错的.只是我不会.帮顶一下 太高级
回复 #1 nnnqpnnn 的帖子
class Antdef getpposition()
return @posi
end
getpposition?
getposition
回复 #1 nnnqpnnn 的帖子
:mrgreen: :mrgreen: 看不懂呢~~ 原帖由 耿耿于怀 于 2008-9-4 22:13 发表 http://bbs.chinaunix.net/images/common/back.gif看不懂呢~~
:mrgreen: :mrgreen: 不懂也
页:
[1]