battlesnake 0.1.4 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc1006d3ed555bc10587302b1d0bd9323e92f2a194bc2ce6673ac44e4b502f74
4
- data.tar.gz: e5a12dcd3a84f68b6d70735448d115a007909018b27010597753c7dff41aba84
3
+ metadata.gz: d7b5ed7b3a7515fa2b0a45e0e942a50e037196405f064aebc4971f91b5b75c54
4
+ data.tar.gz: 6f10ca74dec6bf341df4ec5792f4fc391e2989f52845d12ba272cd1020ee8372
5
5
  SHA512:
6
- metadata.gz: 66184f4a9b2474337f1d25a76a4ffd7cd2b63beeb390a7a3261e2ff51911b7c2dcabd453f7510018b785869ab25736d3e31e35e031111cbd4c8ef39966f1182c
7
- data.tar.gz: 9ebae26dac464f629359f325f871500d0634a5bdf381ec416b8581ff358777ae94267973c5c937921279afc5dc45490ad63bc8e4357f4f2dce61cde08f0537ca
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- battlesnake (0.1.4)
4
+ battlesnake (0.1.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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 = from.distance(to) + 1
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
@@ -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.7"
4
4
  end
data/lib/battlesnake.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "battlesnake/version"
2
2
  require 'battlesnake/base'
3
+ require 'battlesnake/turn'
3
4
  require 'battlesnake/board'
4
5
  require 'battlesnake/game'
5
6
  require 'battlesnake/location'
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.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-01-04 00:00:00.000000000 Z
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: []