ChatNoir
ChatNoir

Reputation: 545

Ruby - how to refer to all elements within an array

I have my array:

array = Array.new(10) { Array.new(10 , 0)}

Which when printed out contains a 10x10 grid of 0s.

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".

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"

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.

Thank you! :)

Upvotes: 0

Views: 72

Answers (2)

roinir
roinir

Reputation: 300

Change the stopping condition to: array.flatten.any?{|v| v=="."} (i.e while this is true carry on with whatever)

Upvotes: 3

La-comadreja
La-comadreja

Reputation: 5755

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

Upvotes: 0

Related Questions