battlesnake 0.1.4 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -0
- data/Gemfile.lock +1 -1
- data/lib/battlesnake/board.rb +52 -1
- data/lib/battlesnake/turn.rb +42 -0
- data/lib/battlesnake/version.rb +1 -1
- data/lib/battlesnake.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7b5ed7b3a7515fa2b0a45e0e942a50e037196405f064aebc4971f91b5b75c54
|
4
|
+
data.tar.gz: 6f10ca74dec6bf341df4ec5792f4fc391e2989f52845d12ba272cd1020ee8372
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d4930d284fad09d1ac16c587c561ede5bd955b376c0a41f77dd1aa51010f002470e4f88ecba06993790213ea2bbbd8bae7c333952cc92f271ac83579f83d67a2
|
7
|
+
data.tar.gz: 0b235c618a3b2444e88e792a1c661aef23c1d470fd2de6c9353e48d0b80ec689986cc3a950408ebbc16cb014b772f10129c54b1d252e2e72bb1b49bf1eaa3a99
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,33 @@
|
|
1
1
|
# Battlesnake
|
2
2
|
|
3
|
+
## 0.1.7 (2023-02-07)
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
|
7
|
+
- Board
|
8
|
+
#flood_fills(location, options = {}) accepts an option "max" which will stop counting available spaces when reached, for performance.
|
9
|
+
|
10
|
+
## 0.1.6 (2023-01-07)
|
11
|
+
|
12
|
+
### Added
|
13
|
+
|
14
|
+
- Class for Turn, which encapsulates a single game turn request.
|
15
|
+
- #others returns all snakes except your own.
|
16
|
+
|
17
|
+
## 0.1.5 (2023-01-04)
|
18
|
+
|
19
|
+
### Added
|
20
|
+
|
21
|
+
- Board
|
22
|
+
- #available_neighbors(location) returns neighboring locations available for moving.
|
23
|
+
- #flood_fills(location) returns hash of reachable locations by direction.
|
24
|
+
|
25
|
+
### Changed
|
26
|
+
|
27
|
+
- Board
|
28
|
+
- #find_path(from, to, max_distance: nil) skips recursion if manhattan distance is greater than
|
29
|
+
max_distance.
|
30
|
+
|
3
31
|
## 0.1.4 (2023-01-03)
|
4
32
|
|
5
33
|
### Added
|
data/Gemfile.lock
CHANGED
data/lib/battlesnake/board.rb
CHANGED
@@ -102,6 +102,37 @@ 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
|
+
# @param [Location] location from which moving is desired.
|
119
|
+
# @options [Hash]
|
120
|
+
# max: max number of spaces to count before stopping search
|
121
|
+
#
|
122
|
+
# @return [Hash] hash of reachable locations by direction
|
123
|
+
def flood_fills(location, options = {})
|
124
|
+
fills = Location::DIRECTIONS.map{ |direction| [direction, []]}.to_h
|
125
|
+
|
126
|
+
available_directions(location).each do |direction|
|
127
|
+
@flood_fill_checked = []
|
128
|
+
@flood_fill_matches = []
|
129
|
+
|
130
|
+
fills[direction] = flood_fill(location.move(direction), options)
|
131
|
+
end
|
132
|
+
|
133
|
+
fills
|
134
|
+
end
|
135
|
+
|
105
136
|
##
|
106
137
|
# List of valid, consecutive paths from one location to the next. Paths may not:
|
107
138
|
#
|
@@ -117,8 +148,11 @@ module Battlesnake
|
|
117
148
|
#
|
118
149
|
# @return [Array<Path>] a list of paths, which themselves are lists of consecutive, valid locations.
|
119
150
|
def find_path(from, to, max_distance: nil)
|
151
|
+
distance = from.distance(to)
|
152
|
+
return nil if max_distance && max_distance < distance
|
153
|
+
|
120
154
|
@paths = []
|
121
|
-
@ideal_path_size =
|
155
|
+
@ideal_path_size = distance + 1
|
122
156
|
@shortest_path_size = max_distance || @ideal_path_size
|
123
157
|
@ideal_path_size_found = false
|
124
158
|
|
@@ -127,6 +161,23 @@ module Battlesnake
|
|
127
161
|
@paths.select{ |path| path.size == @shortest_path_size }.first
|
128
162
|
end
|
129
163
|
|
164
|
+
private
|
165
|
+
|
166
|
+
def flood_fill(location, options = {})
|
167
|
+
@flood_fill_checked << location.coords
|
168
|
+
|
169
|
+
unless occupied?(location)
|
170
|
+
@flood_fill_matches << location
|
171
|
+
|
172
|
+
available_neighbors(location).each do |neighbor|
|
173
|
+
return @flood_fill_matches if options[:max] && @flood_fill_matches.size >= options[:max]
|
174
|
+
flood_fill(neighbor, options) unless @flood_fill_checked.include?(neighbor.coords)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
@flood_fill_matches
|
179
|
+
end
|
180
|
+
|
130
181
|
def recursive_paths(from, to, path)
|
131
182
|
head = path.last
|
132
183
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Battlesnake
|
2
|
+
##
|
3
|
+
# Represents a single iteration (turn) of a Battlesnake game.
|
4
|
+
class Turn < Base
|
5
|
+
# @return [Hash] board as a data structure usable by other objects.
|
6
|
+
attr_reader :as_json
|
7
|
+
|
8
|
+
# @return [Hash] game attributes as a data structure.
|
9
|
+
attr_reader :game
|
10
|
+
|
11
|
+
# @return [Board] board attributes as a Board object.
|
12
|
+
attr_reader :board
|
13
|
+
|
14
|
+
# @return [Snake] your own snake attributes as a Snake object.
|
15
|
+
attr_reader :you
|
16
|
+
|
17
|
+
##
|
18
|
+
# Returns a new instance of Turn.
|
19
|
+
#
|
20
|
+
# @param json_or_hash [String,Hash] can be a hash of attributes, or a JSON string which
|
21
|
+
# represents such a structure.
|
22
|
+
#
|
23
|
+
# @return [Turn]
|
24
|
+
def initialize(json_or_hash)
|
25
|
+
data = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash
|
26
|
+
|
27
|
+
@as_json = data
|
28
|
+
@game = data['game']
|
29
|
+
@board = Board.new(data['board'])
|
30
|
+
@you = Snake.new(data['you'])
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Returns all snakes, minus your snake.
|
35
|
+
#
|
36
|
+
# @return [Array<Snake>]
|
37
|
+
def others
|
38
|
+
return @others if defined?(@others)
|
39
|
+
@others = board.snakes.reject{ |snake| snake.id == you.id }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/battlesnake/version.rb
CHANGED
data/lib/battlesnake.rb
CHANGED
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
|
+
version: 0.1.7
|
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-
|
11
|
+
date: 2023-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/battlesnake/game.rb
|
127
127
|
- lib/battlesnake/location.rb
|
128
128
|
- lib/battlesnake/snake.rb
|
129
|
+
- lib/battlesnake/turn.rb
|
129
130
|
- lib/battlesnake/version.rb
|
130
131
|
homepage: https://github.com/bellmyer/battlesnake
|
131
132
|
licenses: []
|