我遇到了麻煩,我的第一個條件,這將檢查,以確保添加的新件比它之前/之下的一個小。我的塔河內遊戲工作正常,直到我添加它。以下是我的代碼:河內塔,紅寶石條件
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