Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 红宝石战舰_Ruby_Arrays - Fatal编程技术网

Ruby 红宝石战舰

Ruby 红宝石战舰,ruby,arrays,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

我正在测试我的网格,我已经意识到第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

在将彩色磁贴添加到阵列之前,您正在打印行
0

另外,对于循环,您不需要嵌套的
(您只有一个元素)。
尝试:

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 

这里有一些建议可以帮助你找到你的小毛病:添加
x_min=0;x_max=9;y_min=0;y_max=9
然后在y_min中为y重写循环,例如
..y_max
谢谢!很好用!