I am creating a map for my roguelike game and already I stumbled on a problem. I want to create a two dimensional array of objects. In my previous C++ game I did this:
class tile; //found in another file.
tile theMap[MAP_WIDTH][MAP_HEIGHT];
I can't figure out how I should do this with Ruby.
theMap = Array.new(MAP_HEIGHT) { Array.new(MAP_WIDTH) { Tile.new } }
Use arrays of arrays.
board = [
[ 1, 2, 3 ],
[ 4, 5, 6 ]
]
x = Array.new(3){|i| Array.new(3){|j| i+j}}
Also look into the Matrix class:
require 'matrix'
Matrix.build(3,3){|i, j| i+j}
2D arrays are no sweat
array = [[1,2],[3,4],[5,6]]
=> [[1, 2], [3, 4], [5, 6]]
array[0][0]
=> 1
array.flatten
=> [1, 2, 3, 4, 5, 6]
array.transpose
=> [[1, 3, 5], [2, 4, 6]]
For loading 2D arrays try something like:
rows, cols = 2,3
mat = Array.new(rows) { Array.new(cols) }
# Let's define some class
class Foo
# constructor
def initialize(smthng)
#print_me = smthng
end
def print
puts #print_me
end
# Now let's create 2×2 array with Foo objects
the_map = [
[Foo.new("Dark"), Foo.new("side")],
[Foo.new("of the"), Foo.new("spoon")] ]
# Now to call one of the object's methods just do something like
the_map[0][0].print # will print "Dark"
the_map[1][1].print # will print "spoon"
Related
Is there a way to modify the implementation of map() in the Array class such that it only affects certain indices of the array?
Example:
a = [1, 2, 3, 4, 5]
a.map(2..4) { |x| x*2 }
Now, a = [1, 2, 6, 8, 10] since the map function was only used on indices 2 and 3.
You could (but really shouldn't) do this:
Array.class_eval do
def map(range = nil)
return super() if range.nil?
return self[range].map unless block_given?
self[range].map { |x| yield x }
end
end
[1, 2, 3, 4, 5].map(2..4) { |x| x * 2 }
# => [6, 8, 10]
Ruby already has a MUCH more normal/nice/better way of "select only certain indices", with arr[2..4]:
arr = [1, 2, 3, 4, 5]
arr[2..4].map { |x| x * 2 }
# => [6, 8, 10]
I've avoided mutation above, but if you must have that as well, you can do something similar to the above, just with map! instead.
I have two arrays:
#a = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
#b = [a, b, c]
I need to replace n-th column in a with b like:
swap_column(0)
#=> [a, 2, 3]
[b, 5, 6]
[c, 8, 9]
(This is for using Cramer's rule for solving equations system, if anybody wonders.)
The code I've come up with:
def swap_column(n)
#a.map.with_index { |row, j| row[n] = #b[j] }
end
How do I get rid of assignment here so that map returns the modified matrix while leaving #a intact?
What you wanted is dup. Also, you had the return value of the map.with_index block wrong.
def swap_column(i)
#a.map.with_index{|row, j| row = row.dup; row[i] = #b[j]; row}
end
or
def swap_column(i)
#a.map.with_index{|row, j| row.dup.tap{|row| row[i] = #b[j]}}
end
The answer by sawa is good and the main point is you need to dup your inner arrays for this to work properly. The only reason for this additional post is to point out that often when you are using with_index so that you can directly 1:1 index into another array you can simplify the code by using zip.
def swap_column(n)
#a.zip(#b).map {|r,e| r.dup.tap{|r| r[n] = e}}
end
What zip does is combine your two arrays into a new array where each element is an array made of the two corresponding elements of the initial arrays. In this case it would be an array of an array and an element you want to later use for replacement. We then map over those results and automatically destructure each element into the two pieces. We then dup the array piece and tap it to replace the nth element.
You can use transpose to do the following:
class M
attr :a, :b
def initialize
#a = [[1,2,3],
[4,5,6],
[7,8,9]
]
#b = [:a, :b, :c]
end
def swap_column(n)
t = #a.transpose
t[0] = #b
t.transpose
end
end
m = M.new
=> #<M:0x007ffdc2952e38 #a=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], #b=[:a, :b, :c]>
m.swap_column(0)
=> [[:a, 2, 3], [:b, 5, 6], [:c, 8, 9]]
m # m is unchanged
=> #<M:0x007ffdc2952e38 #a=[[1, 2, 3], [4, 5, 6], [7, 8, 9]], #b=[:a, :b, :c]>
I have very little knowledge about Ruby and cant find a way to create 2d array. Can anyone provide me some snippets or information to get me started?
a = [[1, 2], [3, 4]]
a.each do |sub|
sub.each do |int|
puts int
end
end
# Output:
# 1
# 2
# 3
# 4
or:
a = [[1, 2], [3, 4]]
a.each do |(x, y)|
puts x + y
end
# Output:
# 3
# 7
The easiest way to create a 2d array is by the following:
arr1 = Array.new(3) { Array.new(3)}
The code above creates a 2D array with three rows and three columns.
Cheers.
irb(main):001:0> a = []
=> []
irb(main):002:0> a1 = [1, 2]
=> [1, 2]
irb(main):003:0> a2 = [3, 4]
=> [3, 4]
irb(main):004:0> a.push a1
=> [[1, 2]]
irb(main):005:0> a.push a2
=> [[1, 2], [3, 4]]
irb(main):006:0> a
=> [[1, 2], [3, 4]]
irb(main):007:0> a[0]
=> [1, 2]
irb(main):008:0> a[0][1]
=> 2
Ruby doesn't have the concept of 2-dimensional arrays like C does. Arrays in Ruby are dynamic -- meaning you can resize them a will. They can contain any object or value in each "slot" - including another Array!
In the examples given by #JunaidKirkire and #simonmenke, you have an array which has arrays for its values. You can access the values using the syntax similar to C - but you could also have the case where one slot is an Array and another is just a number, or a String, or a Hash...
You may want to work through a Ruby tutorial to get a better idea of how it works. I like RubyMonk but there are other good ones out there as well.
Creating a 2d array in Ruby
In ruby, every method accepts a block.
two_d_array = Array.new(3) do
Array.new(3) do
0
end
end
The same can be written In oneline (macro style)
two_d_array = Array.new(3) { Array.new(3) { 0 } }
Output
[
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]
]
Iterating a 2d array in Ruby
I can think of three ways.
1: Using range
(0...two_d_array.size).each do |i|
(0...(two_d_array[i].length)).each do |j|
puts two_d_array[i][j]
end
end
2: Using for
for i in (0...two_d_array.size)
for j in (0...two_d_array[i].length)
puts two_d_array[i][j]
end
end
3: Using Each_with_index method
two_d_array.each_with_index do |sub_array, i|
sub_array.each_with_index do |item, j|
puts two_d_array[i][j]
end
end
Is there an elegant way to find and replace any integers superior to 3 (as example) in a multidimensional array? The array may have the dimension 1, 2, 3, or more. Just an example of a such array:
[ [ [ 3, 3, 5 ],
[ 4, 3, 3 ] ],
[ [ 3, 2, 3 ],
[ 0, 3, 8 ] ] ]
I would like to do so without flatten the array.
Following sepp2k idea, here is a possible implementation:
class Object
def deep_map(&block)
if self.respond_to? :each
result = []
self.each do |e|
result << e.deep_map(&block)
end
return result
else
return block.call(self)
end
end
end
Then apply deep_map as you wish on the array:
> [[[3, 3, 5], [4, 3, 3]], [[3, 2, 3], [0, 3, 8]]].deep_map { |e| e > 3 ? 0 : e }
=> [[[3, 3, 0], [0, 3, 3]], [[3, 2, 3], [0, 3, 0]]]
Or, more briefly:
class Object
def deep_map(&block)
respond_to?(:map) ? map{|e| e.deep_map(&block)} : block.call(self)
end
end
Or, polymorphically:
class Object
def deep_map(&block); block.call(self) end
end
class Array
def deep_map(&block); map{|e| e.deep_map(&block)} end
end
You can write a deep_map method, which calls map on the array and then for each element test whether it's a sub-array. If it is, call deep_map recursively with the sub-array, otherwise yield the element.
You can then use that deep_map method to transform the inner elements of your multi-dimensional array without affecting its structure.
So, if I've done this right, f(x) will traverse a multidimensional Array until it finds one containing something that isn't an Array or subclass of Array, at which point it will yield the innermost Array to the block and replace it with the block's return value.
def f x, &block
x.map do |a|
if a.first.class.ancestors.include? Array
f a, &block
else
yield a
end
end
end
p(f [ [ [ 3, 3, 5 ],
[ 4, 3, 3 ] ],
[ [ 3, 2, 3 ],
[ 0, 3, 8 ] ] ] do |e|
e.reject { |x| x > 3 }
end
)
Assume, I have a two dimensional array A, and it's stated that somewhere inside it there's an object my_element. What's the quickest way to find out its coordinates? I am using Ruby 1.8.6.
This is one way. I'm not sure it's the quickest, though.
class Array
def coordinates(element)
each_with_index do |subarray, i|
j = subarray.index(element)
return i, j if j
end
nil
end
end
array = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]
array.coordinates(3) # => [0, 2]
array.coordinates(9) # => [2, 2]
array.coordinates(42) # => nil