红宝石战舰

时间:2014-12-17 01:22:14

标签: ruby arrays

我正在测试我的网格,我意识到第0行无法着色。请帮忙!

require 'colorize'

def board
  puts "enter the x co-ordinate"  
  x_input = gets.chomp.to_i  
  puts "enter the y co-ordiante"  
  y_input = gets.chomp.to_i    

  arr = Array.new(10, ".").map{|y| Array.new(10, ".")}

  y_axis = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  x_axis = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]

  print "\t"
  print y_axis.join("\t")
  puts

  arr.each_with_index do |y, i|
    print x_axis[i]
    print "\t"
    print y.join("\t")
    for y_coordinate in y_input..y_input
      for x_coordinate in x_input..x_input
        arr[y_coordinate][x_coordinate]= " ".colorize(:color => :light_blue, :background => :red)
      end
    end
    puts
  end 
  puts
end
board

1 个答案:

答案 0 :(得分:0)

在将彩色图块添加到数组之前,您正在打印行0 此外,您不需要嵌套的for循环(您只有一个元素) 试试:

arr.each_with_index do |y, i|
  print x_axis[i]
  print "\t"
  arr[y_input][x_input]= " ".colorize(:color => :light_blue, :background => :red)
  print y.join("\t")
  puts
end