Skip to content

Commit

Permalink
command line support for tile coordinate description
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewstyler committed Jul 9, 2023
1 parent a9ab0e8 commit 1eca0b6
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ Metrics/AbcSize:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 209
Exclude:
- 'lib/CLI/command.rb'
Max: 197

# Offense count: 2
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand Down
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ map.tiles[y][x].to_h
:items=>[]}
```

or from the command line:

```bash
$ ruby-perlin-2D-map-generator describe coordinates=0,1

{:x=>0,
:y=>1,
:height=>0.29251394359649563,
:moist=>0.29100678755603004,
:temp=>0.6034041566100443,
:biome=>{:name=>"deep_valley", :flora_range=>1, :colour=>"\e[48;5;47m"},
:items=>[]}
```

# Full Command line Usage
```bash
$ ruby-perlin-2D-map-generator --help
Expand All @@ -104,8 +118,14 @@ hashes with each tiles information.

Arguments:
(DESCRIBE | RENDER) command to run: render prints the map to standard
output using ansi colors, while describe prints each
tiles bionome information in the map.
output using ansi colors. describe prints each tiles
bionome information in the map, can be combined with the
coordinates keyword to print a specific tile.
(permitted: describe, render)

Keywords:
COORDINATES=INT_LIST Used with the describe command, only returns the given
coordinate tile details

Options:
--elevation=float Adjust each generated elevation by this percent (0 -
Expand Down Expand Up @@ -149,4 +169,7 @@ Examples:

Render with options
$ ruby-perlin-2D-map-generator render --elevation=-40 --moisture=25 --hs=1

Describe tile [1, 1]
$ ruby-perlin-2D-map-generator describe coordinates=1,1
```
19 changes: 15 additions & 4 deletions lib/CLI/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ class Command

example 'Render with options',
' $ ruby-perlin-2D-map-generator render --elevation=-40 --moisture=25 --hs=1'

example 'Describe tile [1, 1]',
' $ ruby-perlin-2D-map-generator describe coordinates=1,1'
end

argument :command do
name '(describe | render)'
arity one
validate ->(v) { v.downcase == 'describe' || v.downcase == 'render' }
desc 'command to run: render prints the map to standard output using ansi colors, ' \
'while describe prints each tiles bionome information in the map.'
permit %w[describe render]
desc 'command to run: render prints the map to standard output using ansi colors. ' \
'describe prints each tiles bionome information in the map, can be combined with ' \
'the coordinates keyword to print a specific tile.'
end

keyword :coordinates do
arity one
convert :int_list
validate ->(v) { v >= 0 }
desc 'Used with the describe command, only returns the given coordinate tile details'
end

option :height_seed do
Expand Down Expand Up @@ -255,7 +266,7 @@ def execute_command
))
case params[:command]
when 'render' then map.render
when 'describe' then puts map.describe
when 'describe' then puts(!params[:coordinates].nil? ? map[params[:coordinates][0], params[:coordinates][1]].to_h : map.describe)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def render

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

tiles[y][x]
end
Expand Down
10 changes: 9 additions & 1 deletion test/map_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,19 @@ def test_index_lookup
def test_index_out_of_bound_raises_error
tiles = [[mock('Tile1'), mock('Tile2')], [mock('Tile3'), mock('Tile4')]]
map = Map.new
map.expects(:tiles).returns(tiles)
map.expects(:tiles).at_least_once.returns(tiles)

assert_raises ArgumentError, 'coordinates out of bounds' do
map[1, 2]
end

assert_raises ArgumentError, 'coordinates out of bounds' do
map[nil, 2]
end

assert_raises ArgumentError, 'coordinates out of bounds' do
map[1, nil]
end
end

private
Expand Down

0 comments on commit 1eca0b6

Please sign in to comment.