battlesnake 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc1006d3ed555bc10587302b1d0bd9323e92f2a194bc2ce6673ac44e4b502f74
4
- data.tar.gz: e5a12dcd3a84f68b6d70735448d115a007909018b27010597753c7dff41aba84
3
+ metadata.gz: 8f8520692a2004710c7e4c597c44248819caf6b7f5f864985a70a6ab3fa85e44
4
+ data.tar.gz: 66547ac978ce2a6c48a575d9bc6d6b57630c3e980445882278fef94c08a0b466
5
5
  SHA512:
6
- metadata.gz: 66184f4a9b2474337f1d25a76a4ffd7cd2b63beeb390a7a3261e2ff51911b7c2dcabd453f7510018b785869ab25736d3e31e35e031111cbd4c8ef39966f1182c
7
- data.tar.gz: 9ebae26dac464f629359f325f871500d0634a5bdf381ec416b8581ff358777ae94267973c5c937921279afc5dc45490ad63bc8e4357f4f2dce61cde08f0537ca
6
+ metadata.gz: 7c37667f70cfcc7ad968e4563341b00cb46f336f3ffe86a14a7fd8d0112fccea55cb545479d8e7423c7377c1293a45a7406a024ac1380e18eff62a40325a78ca
7
+ data.tar.gz: 55ea92d1d7e6e0ae6d163a23f7d517c3144e9adec7c312615294a6f33613d95830dea08b5865249674239f046a08e54e77a1baa5ecc6911daab1ae22ba24c229
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Battlesnake
2
2
 
3
+ ## 0.1.5 (2023-01-04)
4
+
5
+ ### Added
6
+
7
+ - Board
8
+ - #available_neighbors(location) returns neighboring locations available for moving.
9
+ - #flood_fills(location) returns hash of reachable locations by direction
10
+
11
+ ### Changed
12
+
13
+ - Board
14
+ - #find_path(from, to, max_distance: nil) skips recursion if manhattan distance is greater than
15
+ max_distance.
16
+
3
17
  ## 0.1.4 (2023-01-03)
4
18
 
5
19
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- battlesnake (0.1.4)
4
+ battlesnake (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -102,6 +102,33 @@ module Battlesnake
102
102
  end
103
103
  end
104
104
 
105
+ ##
106
+ # List of neighboring locations available for moving from given _Location_.
107
+ #
108
+ # @param [Location] location from which moving is desired.
109
+ #
110
+ # @return [Array<Location>] list of locations
111
+ def available_neighbors(location)
112
+ Location::DIRECTIONS.map{ |direction| location.move(direction) }.select{ |l| available?(l) }
113
+ end
114
+
115
+ ##
116
+ # List reachable locations in each orthogonal direction.
117
+ #
118
+ # @return [Hash] hash of reachable locations by direction
119
+ def flood_fills(location)
120
+ fills = Location::DIRECTIONS.map{ |direction| [direction, []]}.to_h
121
+
122
+ available_directions(location).each do |direction|
123
+ @flood_fill_checked = []
124
+ @flood_fill_matches = []
125
+
126
+ fills[direction] = flood_fill(location.move(direction)) #.uniq(&:coords)
127
+ end
128
+
129
+ fills
130
+ end
131
+
105
132
  ##
106
133
  # List of valid, consecutive paths from one location to the next. Paths may not:
107
134
  #
@@ -117,8 +144,11 @@ module Battlesnake
117
144
  #
118
145
  # @return [Array<Path>] a list of paths, which themselves are lists of consecutive, valid locations.
119
146
  def find_path(from, to, max_distance: nil)
147
+ distance = from.distance(to)
148
+ return nil if max_distance && max_distance < distance
149
+
120
150
  @paths = []
121
- @ideal_path_size = from.distance(to) + 1
151
+ @ideal_path_size = distance + 1
122
152
  @shortest_path_size = max_distance || @ideal_path_size
123
153
  @ideal_path_size_found = false
124
154
 
@@ -127,6 +157,22 @@ module Battlesnake
127
157
  @paths.select{ |path| path.size == @shortest_path_size }.first
128
158
  end
129
159
 
160
+ private
161
+
162
+ def flood_fill(location)
163
+ @flood_fill_checked << location.coords
164
+
165
+ unless occupied?(location)
166
+ @flood_fill_matches << location
167
+
168
+ available_neighbors(location).each do |neighbor|
169
+ flood_fill(neighbor) unless @flood_fill_checked.include?(neighbor.coords)
170
+ end
171
+ end
172
+
173
+ @flood_fill_matches
174
+ end
175
+
130
176
  def recursive_paths(from, to, path)
131
177
  head = path.last
132
178
 
@@ -1,4 +1,4 @@
1
1
  module Battlesnake
2
2
  # The current version of the driver.
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.5"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: battlesnake
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jaime Bellmyer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-04 00:00:00.000000000 Z
11
+ date: 2023-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec