Grid Battleship board-如何在某些点插入换行符?

问题描述 投票:0回答:2

我在自学Ruby时正在建造战舰。在董事会课程中,我填充了一个范围(1..4,“ A” ..“ D”)中的所有单元格,因此显示为“ A1”,“ A2”,“ A3”等。字母显示行,数字显示列。

现在,我想做的就是渲染电路板。我以为我可以提取单元格的哈希值,分离键(字母数字坐标),并在每个字母组合结束后插入换行符(A的字母,然后\ n的字母B,\ n的\ n字母,等等...)我可以使它以非常静态的方式工作,但是我不确定如何使它更动态以匹配电路板-允许自定义尺寸的电路板。当我调用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...}

我目前正在渲染电路板,所以我的逻辑是,如果我可以将单元拆分成每个不同的字母(cell.keys [0]),那么我将能够触发@render和从那里操作它。

[请告诉我我的逻辑是否废话:)我需要反馈来学习。另外,如果您知道如何按照我的想法进行工作,可以为我指明正确的方向吗?如果我认为有误,请解释。

谢谢!

我在自学Ruby时正在建造战舰。在董事会课程中,我填充了一个范围(1..4,“ A” ..“ D”)中的所有单元格,因此显示为“ A1”,“ A2”,“ A3”等。字母显示行,并且...

ruby syntax
2个回答
0
投票

我会尝试group_by


0
投票

让我们说这是您的课程:

© www.soinside.com 2019 - 2024. All rights reserved.