Skip to content

Commit 0afbaee

Browse files
Merge pull request #1 from matthewstyler/tm/add-xy-index-method
[] coordinate lookup method with command line support for tile coordinate description
2 parents 9bdece5 + 1eca0b6 commit 0afbaee

File tree

5 files changed

+105
-10
lines changed

5 files changed

+105
-10
lines changed

.rubocop_todo.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ Metrics/AbcSize:
2727
# Offense count: 4
2828
# Configuration parameters: CountComments, CountAsOne.
2929
Metrics/ClassLength:
30-
Max: 209
30+
Exclude:
31+
- 'lib/CLI/command.rb'
32+
Max: 197
3133

3234
# Offense count: 2
3335
# Configuration parameters: AllowedMethods, AllowedPatterns.

README.md

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,26 @@ See Command line Usage for full customization, below are some examples. Alter th
6161

6262
# Generate without rendering
6363

64-
```bash
64+
```irb
6565
irb(main):001:0> map = Map.new
66-
...
67-
irb(main):002:0> map.describe[1][0]
66+
```
67+
68+
Map can then be manipulated via traditional x,y lookup
69+
```irb
70+
map[x, y].to_h
71+
=>
72+
{:x=>0,
73+
:y=>1,
74+
:height=>0.29251394359649563,
75+
:moist=>0.29100678755603004,
76+
:temp=>0.6034041566100443,
77+
:biome=>{:name=>"deep_valley", :flora_range=>1, :colour=>"\e[48;5;47m"},
78+
:items=>[]}
79+
```
80+
or the less intuitative multidimensional lookup (reversed axis):
81+
82+
```irb
83+
map.tiles[y][x].to_h
6884
=>
6985
{:x=>0,
7086
:y=>1,
@@ -75,6 +91,20 @@ irb(main):002:0> map.describe[1][0]
7591
:items=>[]}
7692
```
7793

94+
or from the command line:
95+
96+
```bash
97+
$ ruby-perlin-2D-map-generator describe coordinates=0,1
98+
99+
{:x=>0,
100+
:y=>1,
101+
:height=>0.29251394359649563,
102+
:moist=>0.29100678755603004,
103+
:temp=>0.6034041566100443,
104+
:biome=>{:name=>"deep_valley", :flora_range=>1, :colour=>"\e[48;5;47m"},
105+
:items=>[]}
106+
```
107+
78108
# Full Command line Usage
79109
```bash
80110
$ ruby-perlin-2D-map-generator --help
@@ -88,8 +118,14 @@ hashes with each tiles information.
88118

89119
Arguments:
90120
(DESCRIBE | RENDER) command to run: render prints the map to standard
91-
output using ansi colors, while describe prints each
92-
tiles bionome information in the map.
121+
output using ansi colors. describe prints each tiles
122+
bionome information in the map, can be combined with the
123+
coordinates keyword to print a specific tile.
124+
(permitted: describe, render)
125+
126+
Keywords:
127+
COORDINATES=INT_LIST Used with the describe command, only returns the given
128+
coordinate tile details
93129

94130
Options:
95131
--elevation=float Adjust each generated elevation by this percent (0 -
@@ -133,4 +169,7 @@ Examples:
133169

134170
Render with options
135171
$ ruby-perlin-2D-map-generator render --elevation=-40 --moisture=25 --hs=1
172+
173+
Describe tile [1, 1]
174+
$ ruby-perlin-2D-map-generator describe coordinates=1,1
136175
```

lib/CLI/command.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,25 @@ class Command
2020

2121
example 'Render with options',
2222
' $ ruby-perlin-2D-map-generator render --elevation=-40 --moisture=25 --hs=1'
23+
24+
example 'Describe tile [1, 1]',
25+
' $ ruby-perlin-2D-map-generator describe coordinates=1,1'
2326
end
2427

2528
argument :command do
2629
name '(describe | render)'
2730
arity one
28-
validate ->(v) { v.downcase == 'describe' || v.downcase == 'render' }
29-
desc 'command to run: render prints the map to standard output using ansi colors, ' \
30-
'while describe prints each tiles bionome information in the map.'
31+
permit %w[describe render]
32+
desc 'command to run: render prints the map to standard output using ansi colors. ' \
33+
'describe prints each tiles bionome information in the map, can be combined with ' \
34+
'the coordinates keyword to print a specific tile.'
35+
end
36+
37+
keyword :coordinates do
38+
arity one
39+
convert :int_list
40+
validate ->(v) { v >= 0 }
41+
desc 'Used with the describe command, only returns the given coordinate tile details'
3142
end
3243

3344
option :height_seed do
@@ -255,7 +266,7 @@ def execute_command
255266
))
256267
case params[:command]
257268
when 'render' then map.render
258-
when 'describe' then puts map.describe
269+
when 'describe' then puts(!params[:coordinates].nil? ? map[params[:coordinates][0], params[:coordinates][1]].to_h : map.describe)
259270
end
260271
end
261272

lib/map.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ def render
2121
end
2222
end
2323

24+
# rubocop:disable Naming/MethodParameterName:
25+
def [](x, y)
26+
raise ArgumentError, 'coordinates out of bounds' if y.nil? || y >= tiles.size || x.nil? || x >= tiles[y].size
27+
28+
tiles[y][x]
29+
end
30+
# rubocop:enable Naming/MethodParameterName:
31+
2432
def tiles
2533
@tiles ||= MapTileGenerator.new(map: self).generate
2634
end

test/map_test.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,41 @@ def test_render
5757
assert_equal "00011011\n\n", output
5858
end
5959

60+
def test_index_lookup
61+
mock_one = mock('Tile1')
62+
mock_two = mock('Tile2')
63+
mock_three = mock('Tile3')
64+
mock_four = mock('Tile4')
65+
66+
tiles = [[mock_one, mock_two], [mock_three, mock_four]]
67+
68+
map = Map.new
69+
map.expects(:tiles).at_least_once.returns(tiles)
70+
71+
assert_equal mock_one, map[0, 0]
72+
assert_equal mock_three, map[0, 1]
73+
assert_equal mock_two, map[1, 0]
74+
assert_equal mock_four, map[1, 1]
75+
end
76+
77+
def test_index_out_of_bound_raises_error
78+
tiles = [[mock('Tile1'), mock('Tile2')], [mock('Tile3'), mock('Tile4')]]
79+
map = Map.new
80+
map.expects(:tiles).at_least_once.returns(tiles)
81+
82+
assert_raises ArgumentError, 'coordinates out of bounds' do
83+
map[1, 2]
84+
end
85+
86+
assert_raises ArgumentError, 'coordinates out of bounds' do
87+
map[nil, 2]
88+
end
89+
90+
assert_raises ArgumentError, 'coordinates out of bounds' do
91+
map[1, nil]
92+
end
93+
end
94+
6095
private
6196

6297
def capture_output

0 commit comments

Comments
 (0)