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_Syntax - Fatal编程技术网

Ruby 网格战列舰板-如何在某些点插入换行符?

Ruby 网格战列舰板-如何在某些点插入换行符?,ruby,syntax,Ruby,Syntax,我在建造战舰的同时自学Ruby。 在我的board类中,我填充了范围(1..4,“a”和“D”)内的所有单元格,因此它被显示为“A1”、“A2”、“A3”等。。。字母表示行,数字表示列 现在,我要做的是渲染电路板。我想我可以对单元格进行散列,分离键(字母数字坐标),并在每个字母组合结束后插入一个换行符(a、B、C等等) 我可以使它以一种非常静态的方式工作,但我不确定的是如何使它更动态,以匹配电路板-允许自定义大小的电路板。 当我调用board.cells时,我的当前输出如下所示 {"A1"=&g

我在建造战舰的同时自学Ruby。 在我的board类中,我填充了范围(1..4,“a”和“D”)内的所有单元格,因此它被显示为“A1”、“A2”、“A3”等。。。字母表示行,数字表示列

现在,我要做的是渲染电路板。我想我可以对单元格进行散列,分离键(字母数字坐标),并在每个字母组合结束后插入一个换行符(a、B、C等等) 我可以使它以一种非常静态的方式工作,但我不确定的是如何使它更动态,以匹配电路板-允许自定义大小的电路板。 当我调用board.cells时,我的当前输出如下所示

{"A1"=>#<Cell:0x007f8edf3231e0 @coordinate="A1", @fired=false, @render=**".", @ship=nil**>,
"A2"=>#<Cell:0x007f8edf323118 @coordinate="A2", @fired=false, @render=**".", @ship=nil**>,
"A3"=>#<Cell:0x007f8edf323000 @coordinate="A3", @fired=false, @render=**".", @ship=nil**>,
"A4"=>#<Cell:0x007f8edf322ee8 @coordinate="A4", @fired=false, @render=**".", @ship=nil**>,
"B1"=>#<Cell:0x007f8edf322da8 @coordinate="B1", @fired=false, @render=**".", @ship=nil**>,
"B2"=>#<Cell:0x007f8edf322cb8 @coordinate="B2", @fired=false, @render=**".", @ship=nil**>,
"B3"=>#<Cell:0x007f8edf322ba0 @coordinate="B3", @fired=false, @render=**".", @ship=nil**>,
"B4"=>#<Cell:0x007f8edf322a88 @coordinate="B4", @fired=false, @render=**".", @ship=nil**>,
...C...D...etc...}
{“A1”=>#,
“A2”=>#,
“A3”=>#,
“A4”=>#,
“B1”=>#,
“B2”=>#,
“B3”=>#,
“B4”=>#,
…C…D…等…)
我目前正在绘制电路板,所以我的逻辑是,如果我可以让单元格在每个不同的字母上分割(cell.keys[0]),那么我就可以触发@render并从那里操作它

请告诉我,如果我的逻辑是垃圾:)我需要反馈来学习。另外,如果您知道如何按照我的思维方式\n工作,您能为我指出正确的方向吗?如果我想错了,请解释

谢谢大家!

我想试试分组

%w(a1 a2 a3 b1 b2 b3 c1 c2 c3).sort.group_by{|e| e[0]}
更完整的答案是:

%w(a1 a2 a3 b1 b2 b3 c1 c2 c3).sort.group_by{|e| e[0]}.each{|k, v| v.each{|e| print "#{k}=>#{e} "}; puts}

假设这是你的班级:

class Cell
  attr_accessor :fired, :coordinate, :render, :ship

  def initialize(coord)
    @coordinate = coord
    @fired = false
    @render = '='
  end

  def fired!
    @fired = true
    @render = 'x'
  end

end
然后定义表哈希:

n = 4
rows = (:a..:z).first(n)
cols = (1..n).to_a
cells = rows.product(cols).map(&:join)
#=> ["a1", "a2", "a3", "a4", "b1", "b2", "b3", "b4", "c1", "c2", "c3", "c4", "d1", "d2", "d3", "d4"]
table = cells.map { |coord| [coord, Cell.new(coord)] }.to_h
让我们启动一个单元:

table["c3"].fired!
这是一个可以呈现表的选项(除非通过更改单元格的顺序将哈希搞乱):

或作为字符串:

puts table.values.map { |cell| cell.render }.each_slice(n).map(&:join).join("\n")
# ====
# ====
# ==x=
# ====
puts table.values.map { |cell| cell.render }.each_slice(n).map(&:join).join("\n")
# ====
# ====
# ==x=
# ====