battlesnake 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b89d9fd8df0aef295a5b8b854347b633550daf06ec2327c9f0fbd7b220eaace4
4
- data.tar.gz: 9eb544e850f0f4267519e9b802db9dce508f7a8eeb9d6d9cbc19463a0596f2e1
3
+ metadata.gz: 8f8520692a2004710c7e4c597c44248819caf6b7f5f864985a70a6ab3fa85e44
4
+ data.tar.gz: 66547ac978ce2a6c48a575d9bc6d6b57630c3e980445882278fef94c08a0b466
5
5
  SHA512:
6
- metadata.gz: 48edb7affb7b6ff01e943b5cd4c9c87c8b6284b281f256bddda9412c1b371ab8ccdeb504fd5de469e830f789ea54fe66ae36c768e079f0f0757161bd88cac855
7
- data.tar.gz: 86dbedc0c782be217c6220de2de438606fb8b006d6f0661b5da08c9f68dc967eaf116bc1901ae8451c55c3bb303d20287ab220762115cff4d62f8dfb90948e87
6
+ metadata.gz: 7c37667f70cfcc7ad968e4563341b00cb46f336f3ffe86a14a7fd8d0112fccea55cb545479d8e7423c7377c1293a45a7406a024ac1380e18eff62a40325a78ca
7
+ data.tar.gz: 55ea92d1d7e6e0ae6d163a23f7d517c3144e9adec7c312615294a6f33613d95830dea08b5865249674239f046a08e54e77a1baa5ecc6911daab1ae22ba24c229
data/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
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
+
17
+ ## 0.1.4 (2023-01-03)
18
+
19
+ ### Added
20
+
21
+ - Board
22
+ - #food?(location) returns true if location is food.
23
+
24
+ ### Changed
25
+
26
+ - Board
27
+ - #occupied_locations no longer includes food.
28
+
3
29
  ## 0.1.3 (2022-11-07)
4
30
 
5
31
  ### Added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- battlesnake (0.1.3)
4
+ battlesnake (0.1.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -41,12 +41,13 @@ module Battlesnake
41
41
  end
42
42
 
43
43
  ##
44
- # List of all occupied locations on the board; snakes, food, hazards, etc
44
+ # List of all occupied locations on the board; snakes, hazards, etc.
45
+ # Does NOT include food, since we don't want to avoid that.
45
46
  #
46
47
  # @return [Array<Location>] list of occupied locations
47
48
  def occupied_locations
48
49
  return @occupied_locations if defined?(@occupied_locations)
49
- @occupied_locations = snakes.map(&:body).flatten + food + hazards
50
+ @occupied_locations = snakes.map(&:body).flatten + hazards
50
51
  end
51
52
 
52
53
  ##
@@ -79,6 +80,16 @@ module Battlesnake
79
80
  on_board?(location) && !occupied?(location)
80
81
  end
81
82
 
83
+ ##
84
+ # Whether the supplied location is food.
85
+ #
86
+ # @param [Location] location being tested for availability.
87
+ #
88
+ # @return [Boolean] true if location is food.
89
+ def food?(location)
90
+ food.include?(location)
91
+ end
92
+
82
93
  ##
83
94
  # List of directions (up, down, left, right) available for moving from given _Location_.
84
95
  #
@@ -91,6 +102,33 @@ module Battlesnake
91
102
  end
92
103
  end
93
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
+
94
132
  ##
95
133
  # List of valid, consecutive paths from one location to the next. Paths may not:
96
134
  #
@@ -106,8 +144,11 @@ module Battlesnake
106
144
  #
107
145
  # @return [Array<Path>] a list of paths, which themselves are lists of consecutive, valid locations.
108
146
  def find_path(from, to, max_distance: nil)
147
+ distance = from.distance(to)
148
+ return nil if max_distance && max_distance < distance
149
+
109
150
  @paths = []
110
- @ideal_path_size = from.distance(to) + 1
151
+ @ideal_path_size = distance + 1
111
152
  @shortest_path_size = max_distance || @ideal_path_size
112
153
  @ideal_path_size_found = false
113
154
 
@@ -116,6 +157,22 @@ module Battlesnake
116
157
  @paths.select{ |path| path.size == @shortest_path_size }.first
117
158
  end
118
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
+
119
176
  def recursive_paths(from, to, path)
120
177
  head = path.last
121
178
 
@@ -1,4 +1,4 @@
1
1
  module Battlesnake
2
2
  # The current version of the driver.
3
- VERSION = "0.1.3"
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.3
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