battlesnake 0.1.5 → 0.1.7
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 +4 -4
- data/CHANGELOG.md +15 -1
- data/Gemfile.lock +1 -1
- data/lib/battlesnake/board.rb +9 -4
- 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,12 +1,26 @@
|
|
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
|
+
|
3
17
|
## 0.1.5 (2023-01-04)
|
4
18
|
|
5
19
|
### Added
|
6
20
|
|
7
21
|
- Board
|
8
22
|
- #available_neighbors(location) returns neighboring locations available for moving.
|
9
|
-
- #flood_fills(location) returns hash of reachable locations by direction
|
23
|
+
- #flood_fills(location) returns hash of reachable locations by direction.
|
10
24
|
|
11
25
|
### Changed
|
12
26
|
|
data/Gemfile.lock
CHANGED
data/lib/battlesnake/board.rb
CHANGED
@@ -115,15 +115,19 @@ module Battlesnake
|
|
115
115
|
##
|
116
116
|
# List reachable locations in each orthogonal direction.
|
117
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
|
+
#
|
118
122
|
# @return [Hash] hash of reachable locations by direction
|
119
|
-
def flood_fills(location)
|
123
|
+
def flood_fills(location, options = {})
|
120
124
|
fills = Location::DIRECTIONS.map{ |direction| [direction, []]}.to_h
|
121
125
|
|
122
126
|
available_directions(location).each do |direction|
|
123
127
|
@flood_fill_checked = []
|
124
128
|
@flood_fill_matches = []
|
125
129
|
|
126
|
-
fills[direction] = flood_fill(location.move(direction)
|
130
|
+
fills[direction] = flood_fill(location.move(direction), options)
|
127
131
|
end
|
128
132
|
|
129
133
|
fills
|
@@ -159,14 +163,15 @@ module Battlesnake
|
|
159
163
|
|
160
164
|
private
|
161
165
|
|
162
|
-
def flood_fill(location)
|
166
|
+
def flood_fill(location, options = {})
|
163
167
|
@flood_fill_checked << location.coords
|
164
168
|
|
165
169
|
unless occupied?(location)
|
166
170
|
@flood_fill_matches << location
|
167
171
|
|
168
172
|
available_neighbors(location).each do |neighbor|
|
169
|
-
|
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)
|
170
175
|
end
|
171
176
|
end
|
172
177
|
|
@@ -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: []
|