从这段代码中,我想到最后一行:puts computer_number
返回3。
但现在是5。
我该怎么做?
def find_at_risk_square(square, computer_number)
WIN.each do |n|
array = []
n.each do |k|
array << square[k]
end
if array.count("X") == 2
computer_number = array.index(" ") + 1
end
end
end
WIN = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 4, 7],
[2, 5, 8],
[3, 6, 9],
[1, 5, 9],
[3, 5, 7]].freeze
square = {1=>"X",2=>"X",3=>" ",4=>" ",5=>" ",6=>" "}
computer_number = "5"
find_at_risk_square(square, computer_number)
puts computer_number
答案 0 :(得分:1)
您可以更改.each
循环中的值。您无法从类似的方法返回值。
当您在方法中执行computer_number = array.index(" ") + 1
时,您将分配方法本地变量。这不会影响外部世界,无论是否采用.each
方法。