I have my array:
我有我的阵列:
array = Array.new(10) { Array.new(10 , 0)}
Which when printed out contains a 10x10 grid of 0s.
打印时包含10x10的0网格。
During the programme some of the elements of the array are replaced with "."
在程序期间,数组的一些元素被替换为“。”。
The user then has to change all the elements which contain "." so that they contain "x".
然后,用户必须更改包含“。”的所有元素。这样它们就包含“x”。
So I'm trying to make a loop which keeps asking the user to input numbers which refer to elements until the array contains no more "."s and puts "Game Over"
所以我试图创建一个循环,不断要求用户输入引用元素的数字,直到数组不再包含“。”s并将“Game Over”放入
Therefore I need to make a loop which keeps going until all elements of the array are not equal to "." So this is what I came up with which doesn't work
因此,我需要创建一个循环,直到数组的所有元素都不等于“。”。所以这就是我提出的不起作用的地方
until array != "." do
Sorry, I have very little knowledge of Ruby so my terminology isn't great.
对不起,我对Ruby知之甚少,所以我的术语不是很好。
Thank you! :)
谢谢! :)
2 个解决方案
#1
3
Change the stopping condition to: array.flatten.any?{|v| v=="."}
(i.e while this is true carry on with whatever)
将停止条件更改为:array.flatten.any?{| v | v ==“。”}(即,这是真的随身携带)
#2
0
Loop through all the elements of each subarray, changing the elements if they are equal to '.'
循环遍历每个子数组的所有元素,如果它们等于'',则更改元素。
array.each do |subarray|
subarray.each do |e|
e = 'x' if e == '.'
end
end
#1
3
Change the stopping condition to: array.flatten.any?{|v| v=="."}
(i.e while this is true carry on with whatever)
将停止条件更改为:array.flatten.any?{| v | v ==“。”}(即,这是真的随身携带)
#2
0
Loop through all the elements of each subarray, changing the elements if they are equal to '.'
循环遍历每个子数组的所有元素,如果它们等于'',则更改元素。
array.each do |subarray|
subarray.each do |e|
e = 'x' if e == '.'
end
end