我遇到了麻烦,我的第一个条件,这将检查,以确保添加的新件比它之前/之下的一个小。我的塔河内游戏工作正常,直到我添加它。以下是我的代码:河内塔,红宝石条件
arrays = [[5,4,3,2,1],[],[]]
win = false
while win != true
puts "Choose a top piece: (1, 2, 3) "
top = gets.to_i
puts "Which stack to place this piece? (1, 2, 3)"
stack = gets.to_i
if (arrays[stack-1] == nil) ||
(arrays[stack-1][arrays[stack-1].count-1] > arrays[top-1][arrays[top-1][arrays[top-1].count]])
arrays[stack-1].push(arrays[top-1].pop)
else
"You need to follow the rules."
end
print arrays
if arrays[1] == [5,4,3,2,1] || arrays[2] == [5,4,3,2,1]
print "You're a winner!"
win = true
end
end
~
下面是我得到的错误。我如何执行我的检查并以简明的方式处理我的nil值数组?
towers_hanoi:13:in `[]': no implicit conversion from nil to integer (TypeError)
from towers_hanoi:13:in `<main>'
谢谢!这非常有帮助。看起来我需要仔细看看Ruby文档。 – Thrynn