Skip to content

Commit 1eca0b6

Browse files
committed
command line support for tile coordinate description
1 parent a9ab0e8 commit 1eca0b6

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
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: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ map.tiles[y][x].to_h
9191
:items=>[]}
9292
```
9393

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+
94108
# Full Command line Usage
95109
```bash
96110
$ ruby-perlin-2D-map-generator --help
@@ -104,8 +118,14 @@ hashes with each tiles information.
104118

105119
Arguments:
106120
(DESCRIBE | RENDER) command to run: render prints the map to standard
107-
output using ansi colors, while describe prints each
108-
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
109129

110130
Options:
111131
--elevation=float Adjust each generated elevation by this percent (0 -
@@ -149,4 +169,7 @@ Examples:
149169

150170
Render with options
151171
$ 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
152175
```

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def render
2323

2424
# rubocop:disable Naming/MethodParameterName:
2525
def [](x, y)
26-
raise ArgumentError, 'coordinates out of bounds' if y >= tiles.size || x >= tiles[y].size
26+
raise ArgumentError, 'coordinates out of bounds' if y.nil? || y >= tiles.size || x.nil? || x >= tiles[y].size
2727

2828
tiles[y][x]
2929
end

test/map_test.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,19 @@ def test_index_lookup
7777
def test_index_out_of_bound_raises_error
7878
tiles = [[mock('Tile1'), mock('Tile2')], [mock('Tile3'), mock('Tile4')]]
7979
map = Map.new
80-
map.expects(:tiles).returns(tiles)
80+
map.expects(:tiles).at_least_once.returns(tiles)
8181

8282
assert_raises ArgumentError, 'coordinates out of bounds' do
8383
map[1, 2]
8484
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
8593
end
8694

8795
private

0 commit comments

Comments
 (0)