battlesnake 0.1.5 → 0.1.7

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: 8f8520692a2004710c7e4c597c44248819caf6b7f5f864985a70a6ab3fa85e44
4
- data.tar.gz: 66547ac978ce2a6c48a575d9bc6d6b57630c3e980445882278fef94c08a0b466
3
+ metadata.gz: d7b5ed7b3a7515fa2b0a45e0e942a50e037196405f064aebc4971f91b5b75c54
4
+ data.tar.gz: 6f10ca74dec6bf341df4ec5792f4fc391e2989f52845d12ba272cd1020ee8372
5
5
  SHA512:
6
- metadata.gz: 7c37667f70cfcc7ad968e4563341b00cb46f336f3ffe86a14a7fd8d0112fccea55cb545479d8e7423c7377c1293a45a7406a024ac1380e18eff62a40325a78ca
7
- data.tar.gz: 55ea92d1d7e6e0ae6d163a23f7d517c3144e9adec7c312615294a6f33613d95830dea08b5865249674239f046a08e54e77a1baa5ecc6911daab1ae22ba24c229
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- battlesnake (0.1.5)
4
+ battlesnake (0.1.7)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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)) #.uniq(&:coords)
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
- flood_fill(neighbor) unless @flood_fill_checked.include?(neighbor.coords)
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
@@ -1,4 +1,4 @@
1
1
  module Battlesnake
2
2
  # The current version of the driver.
3
- VERSION = "0.1.5"
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.5
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-05 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: []