board_game_grid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c3cc1dd64439cb8caf674f6a26fbaaaaa9dd987
4
+ data.tar.gz: 599590f9c7ec87f172f714710e6260025a54a382
5
+ SHA512:
6
+ metadata.gz: 4d58ee472c595e6cdcc7267b17de7079c05a696e4b41c4fc5319b56b105527b2b356b4adb53c635e776008688761d9e5af078abebf01053525bcb67ce5938905
7
+ data.tar.gz: c6414b12c2b15ba873aa14198e0877f89de0a86cdacb6c0c581571698b88b575fdc1444a9c7d8fd0dbb76d72968debb275f7f2c874cfb8b6f7809c7e6c8f1157
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at mark@mrlhumphreys.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in board_game_grid.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Mark Humphreys
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,70 @@
1
+ # BoardGameGrid
2
+
3
+ Data structure and logic for a board game grid. Provides classes that help interpret and operation on the state of a grid of squares with pieces on them.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'board_game_grid'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install board_game_grid
20
+
21
+ ## Usage
22
+
23
+ The primary class is the square set, which can be initialised from an array of hashes:
24
+
25
+ ```ruby
26
+ square_set = BoardGameGrid::SquareSet.new(squares: [
27
+ {id: 1, x: 2, y: 3, piece: nil},
28
+ {id: 2, x: 4, y: 6, piece: {player_number: 1} }
29
+ ])
30
+ ```
31
+
32
+ or an array of squares:
33
+
34
+ ```ruby
35
+ square_set = BoardGameGrid::SquareSet.new(squares: [
36
+ BoardGameGrid::Square.new({id: 1, x: 2, y: 3, piece: nil}),
37
+ BoardGameGrid::Square.new({id: 2, x: 4, y: 6, piece: {player_number: 1}) }
38
+ ])
39
+ ```
40
+
41
+ A square set has some enumerable functionality and common enumerable methods can be called on it (e.g. `map`, `all?`, `include?`, `size`, `+`, `-`)
42
+
43
+ Most methods that would return an array of squares will return a square set object.
44
+
45
+ Querying methods such as `orthogonal` and `diagonal`, which returns a square set containing squares orthogonal or diagonal to an origin square are available.
46
+
47
+ Other querying methods include the `where` method which takes a hash of attribute/values to filter down the set.
48
+
49
+ ```ruby
50
+ square_set.where(x: 1, y: 2, piece: {player_number: 1})
51
+ ```
52
+
53
+ More details are available in the documentation on rubydoc.info
54
+
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mrlhumphreys/board_game_grid. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
67
+
68
+ ## Code of Conduct
69
+
70
+ Everyone interacting in the BoardGameGrid project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/mrlhumphreys/board_game_grid/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,11 @@
1
+ require 'rake/testtask'
2
+ require "bundler/gem_tasks"
3
+ task :default => :spec
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.test_files = FileList['test/board_game_grid/*_test.rb']
8
+ end
9
+
10
+ desc 'run tests'
11
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "board_game_grid"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "board_game_grid/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "board_game_grid"
8
+ spec.version = BoardGameGrid::VERSION
9
+ spec.authors = ["Mark Humphreys"]
10
+ spec.email = ["mark@mrlhumphreys.com"]
11
+
12
+ spec.summary = %q{Data structure and logic for a board game grid.}
13
+ spec.description = %q{Classes that help interpret and operation on the state of a grid of squares with pieces on them.}
14
+ spec.homepage = "https://github.com/mrlhumphreys/board_game_grid"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.required_ruby_version = '>= 2.2'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.15"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "minitest", "~> 5.0"
29
+ end
@@ -0,0 +1,8 @@
1
+ require "board_game_grid/version"
2
+
3
+ # :nodoc:
4
+ module BoardGameGrid
5
+
6
+ end
7
+
8
+ require 'board_game_grid/square_set'
@@ -0,0 +1,42 @@
1
+ module BoardGameGrid
2
+
3
+ # = Direction
4
+ #
5
+ # The Direction that something is moving on a 2d plane
6
+ class Direction
7
+
8
+ # New objects can be instantiated with
9
+ #
10
+ # @param [Fixnum] dx
11
+ # the dx magnitude.
12
+ #
13
+ # @param [Fixnum] dy
14
+ # the dy magnitude.
15
+ #
16
+ # ==== Example:
17
+ # # Instantiates a new Direction
18
+ # BoardGameGrid::Direction.new(1, 1)
19
+ def initialize(dx, dy)
20
+ x = dx == 0 ? dx : dx/dx.abs
21
+ y = dy == 0 ? dy : dy/dy.abs
22
+
23
+ @x, @y = x, y
24
+ end
25
+
26
+ # @return [Fixnum] the x magnitude.
27
+ attr_reader :x
28
+
29
+ # @return [Fixnum] the y magnitude.
30
+ attr_reader :y
31
+
32
+ # Check if directions are equal by seeing if their magnitudes are equal.
33
+ #
34
+ # @param [Direction] other
35
+ # the other direction to compare to.
36
+ #
37
+ # @return [Boolean]
38
+ def ==(other)
39
+ self.x == other.x && self.y == other.y
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,52 @@
1
+ module BoardGameGrid
2
+
3
+ # = Point
4
+ #
5
+ # A point with an x and y co-ordinates
6
+ class Point
7
+
8
+ # New objects can be instantiated with
9
+ #
10
+ # @param [Fixnum] x
11
+ # the x co-ordinate.
12
+ #
13
+ # @param [Fixnum] y
14
+ # the y co-ordinate.
15
+ #
16
+ # ==== Example:
17
+ # # Instantiates a new Point
18
+ # BoardGameGrid::Point.new({
19
+ # x: 1,
20
+ # y: 1
21
+ # })
22
+ def initialize(x, y)
23
+ @x, @y = x, y
24
+ end
25
+
26
+ # @return [Fixnum] the x co-ordinate.
27
+ attr_reader :x
28
+
29
+ # @return [Fixnum] the y co-ordinate.
30
+ attr_reader :y
31
+
32
+ # Add a point to another point by adding their co-ordinates and returning a new point.
33
+ #
34
+ # @param [Point] other
35
+ # the other point to add.
36
+ #
37
+ # @return [Point]
38
+ def +(other)
39
+ self.class.new(self.x + other.x, self.y + other.y)
40
+ end
41
+
42
+ # Check if popints are equal by seeing if their co-ordinates are equal.
43
+ #
44
+ # @param [Point] other
45
+ # the other point to compare to.
46
+ #
47
+ # @return [TrueClass, FalseClass]
48
+ def ==(other)
49
+ self.x == other.x && self.y == other.y
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,110 @@
1
+ require 'board_game_grid/point'
2
+
3
+ module BoardGameGrid
4
+
5
+ # = Square
6
+ #
7
+ # A Square on a board
8
+ class Square
9
+
10
+ # New objects can be instantiated by passing in a hash with
11
+ #
12
+ # @param [Fixnum] id
13
+ # the unique identifier of the square.
14
+ #
15
+ # @param [Fixnum] x
16
+ # the x co-ordinate of the square.
17
+ #
18
+ # @param [Fixnum] y
19
+ # the y co-ordinate of the square.
20
+ #
21
+ # @option [Hash,NilClass] piece
22
+ # The piece on the square, can be a hash or nil.
23
+ #
24
+ # ==== Example:
25
+ # # Instantiates a new Square
26
+ # JustCheckers::Square.new({
27
+ # id: 1,
28
+ # x: 1,
29
+ # y: 0,
30
+ # piece: { player_number: 1, king: false }
31
+ # })
32
+ def initialize(id: , x: , y: , piece: nil)
33
+ @id = id
34
+ @x = x
35
+ @y = y
36
+ @piece = piece
37
+ end
38
+
39
+ # @return [Fixnum] the unique identifier of the square.
40
+ attr_reader :id
41
+
42
+ # @return [Fixnum] the x co-ordinate of the square.
43
+ attr_reader :x
44
+
45
+ # @return [Fixnum] the y co-ordinate of the square.
46
+ attr_reader :y
47
+
48
+ # @return [Hash,NilClass] The piece on the square if any.
49
+ attr_accessor :piece
50
+
51
+ # checks if the square matches the attributes passed.
52
+ #
53
+ # @param [Symbol] attribute
54
+ # the square's attribute.
55
+ #
56
+ # @param [Object,Hash] value
57
+ # a value to match on. Can be a hash of attribute/value pairs for deep matching
58
+ #
59
+ # ==== Example:
60
+ # # Check if square has a piece owned by player 1
61
+ # square.attribute_match?(:piece, player_number: 1)
62
+ def attribute_match?(attribute, value)
63
+ hash_obj_matcher = lambda do |obj, k, v|
64
+ value = obj.send(k)
65
+ if !value.nil? && v.is_a?(Hash)
66
+ v.all? { |k2,v2| hash_obj_matcher.call(value, k2, v2) }
67
+ else
68
+ value == v
69
+ end
70
+ end
71
+
72
+ hash_obj_matcher.call(self, attribute, value)
73
+ end
74
+
75
+ # Is the square occupied by a piece?
76
+ #
77
+ # @return [Boolean]
78
+ def occupied?
79
+ !!piece
80
+ end
81
+
82
+ # Is the square unoccupied by a piece?
83
+ #
84
+ # @return [Boolean]
85
+ def unoccupied?
86
+ !piece
87
+ end
88
+
89
+ # Is the square the same as another one?
90
+ #
91
+ # @return [Boolean]
92
+ def ==(other)
93
+ self.id == other.id
94
+ end
95
+
96
+ # A point object with the square's co-ordinates.
97
+ #
98
+ # @return [Point]
99
+ def point
100
+ Point.new(x, y)
101
+ end
102
+
103
+ # A serialized version of the square as a hash
104
+ #
105
+ # @return [Hash]
106
+ def as_json
107
+ { id: id, x: x, y: y, piece: piece }
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,311 @@
1
+ require 'forwardable'
2
+ require 'board_game_grid/square'
3
+ require 'board_game_grid/vector'
4
+
5
+ module BoardGameGrid
6
+
7
+ # = Square Set
8
+ #
9
+ # A collection of Squares with useful filtering functions
10
+ class SquareSet
11
+ extend Forwardable
12
+
13
+ # New objects can be instantiated by passing in a hash with squares.
14
+ # They can be square objects or hashes.
15
+ #
16
+ # @param [Array<Square,Hash>] squares
17
+ # An array of squares, each with x and y co-ordinates and a piece.
18
+ #
19
+ # ==== Example:
20
+ # # Instantiates a new Square Set
21
+ # BoardGameGrid::SquareSet.new({
22
+ # squares: [
23
+ # { x: 1, y: 0, piece: { player_number: 1, king: false }}
24
+ # ]
25
+ # })
26
+ def initialize(squares: [])
27
+ @squares = if squares.all? { |element| element.instance_of?(Hash) }
28
+ squares.map { |args| BoardGameGrid::Square.new(args) }
29
+ elsif squares.all? { |element| element.instance_of?(BoardGameGrid::Square) }
30
+ squares
31
+ else
32
+ raise ArgumentError, "all squares must have the same class"
33
+ end
34
+ end
35
+
36
+ # @return [Array<Square>] The squares in the set.
37
+ attr_reader :squares
38
+
39
+ def_delegator :squares, :find
40
+ def_delegator :squares, :first
41
+ def_delegator :squares, :size
42
+ def_delegator :squares, :any?
43
+ def_delegator :squares, :all?
44
+ def_delegator :squares, :none?
45
+ def_delegator :squares, :include?
46
+ def_delegator :squares, :map
47
+ def_delegator :squares, :each
48
+ def_delegator :squares, :empty?
49
+
50
+ # Concat two SquareSets together
51
+ #
52
+ # @param [SquareSet] other
53
+ # the second SquareSet
54
+ #
55
+ # @return [SquareSet]
56
+ # ==== Example:
57
+ # # Concat two SquareSets together
58
+ # square_set + other
59
+ def +(other)
60
+ _squares = squares + other.squares
61
+
62
+ self.class.new(squares: _squares)
63
+ end
64
+
65
+ # Remove squares from one SquareSet from another
66
+ #
67
+ # @param [SquareSet] other
68
+ # the second SquareSet
69
+ #
70
+ # @return [SquareSet]
71
+ # ==== Example:
72
+ # # Remove squares from one SquareSet
73
+ # square_set - other
74
+ def -(other)
75
+ _squares = squares - other.squares
76
+
77
+ self.class.new(squares: _squares)
78
+ end
79
+
80
+ # Push a Square onto a SquareSet
81
+ #
82
+ # @param [Square] square
83
+ # the square being pushed on
84
+ #
85
+ # @return [SquareSet]
86
+ # ==== Example:
87
+ # # Push a Square onto a SquareSet
88
+ # square_set << square
89
+ def <<(square)
90
+ _squares = squares << square
91
+
92
+ self.class.new(squares: _squares)
93
+ end
94
+
95
+ # Find the intersection of Squares between sets
96
+ #
97
+ # @param [SquareSet] other
98
+ # the second SquareSet
99
+ #
100
+ # @return [SquareSet]
101
+ # ==== Example:
102
+ # # Find the intersection of Squares
103
+ # square_set & other
104
+ def &(other)
105
+ select { |square| other.include?(square) }
106
+ end
107
+
108
+ # Filter the squares with a block and behaves like Enumerable#select.
109
+ # It returns a SquareSet with the filtered squares.
110
+ #
111
+ # @return [SquareSet]
112
+ def select(&block)
113
+ _squares = squares.select(&block)
114
+
115
+ self.class.new(squares: _squares)
116
+ end
117
+
118
+ # Filter the squares with a hash of attribute and matching values.
119
+ #
120
+ # @param [Hash] hash
121
+ # attributes to query for.
122
+ #
123
+ # @return [SquareSet]
124
+ #
125
+ # ==== Example:
126
+ # # Find all squares where piece is nil
127
+ # square_set.where(piece: nil)
128
+ def where(hash)
129
+ res = hash.inject(squares) do |memo, (attribute, value)|
130
+ memo.select { |square| square.attribute_match?(attribute, value) }
131
+ end
132
+ self.class.new(squares: res)
133
+ end
134
+
135
+ # Find the square with the matching unique identifier
136
+ #
137
+ # @param [Fixnum] id
138
+ # the unique identifier.
139
+ #
140
+ # @return [Square]
141
+ # ==== Example:
142
+ # # Find the square with id 4
143
+ # square_set.find_by_id(4)
144
+ def find_by_id(id)
145
+ select { |square| square.id == id }.first
146
+ end
147
+
148
+ # Find the square with the matching x and y co-ordinates
149
+ #
150
+ # @param [Fixnum] x
151
+ # the x co-ordinate.
152
+ #
153
+ # @param [Fixnum] y
154
+ # the y co-ordinate.
155
+ #
156
+ # @return [Square]
157
+ # ==== Example:
158
+ # # Find the square at 4,2
159
+ # square_set.find_by_x_and_y(4, 2)
160
+ def find_by_x_and_y(x, y)
161
+ select { |square| square.x == x && square.y == y }.first
162
+ end
163
+
164
+ # Find all squares within distance of square
165
+ #
166
+ # @param [Square] square
167
+ # the originating square
168
+ #
169
+ # @param [Fixnum] distance
170
+ # the specified distance from the square
171
+ #
172
+ # @return [SquareSet]
173
+ # ==== Example:
174
+ # # Get all squares within 2 squares of square_a
175
+ # square_set.in_range(square_a, 2)
176
+ def in_range(origin, distance)
177
+ select { |square| Vector.new(origin, square).magnitude <= distance }
178
+ end
179
+
180
+ # Find all squares at distance of square
181
+ #
182
+ # @param [Square] square
183
+ # the originating square
184
+ #
185
+ # @param [Fixnum] distance
186
+ # the specified distance from the square
187
+ #
188
+ # @return [SquareSet]
189
+ # ==== Example:
190
+ # # Get all squares at 2 squares from square_a
191
+ # square_set.at_range(square_a, 2)
192
+ def at_range(origin, distance)
193
+ select { |square| Vector.new(origin, square).magnitude == distance }
194
+ end
195
+
196
+ # Find all squares orthogonal from square
197
+ #
198
+ # @param [Square] square
199
+ # the originating square
200
+ #
201
+ # @return [SquareSet]
202
+ # ==== Example:
203
+ # # Get all squares orthogonal from square_a
204
+ # square_set.orthogonal(square_a)
205
+ def orthogonal(origin)
206
+ select { |square| Vector.new(origin, square).orthogonal? }
207
+ end
208
+
209
+ # Find all squares diagonal from square
210
+ #
211
+ # @param [Square] square
212
+ # the originating square
213
+ #
214
+ # @return [SquareSet]
215
+ # ==== Example:
216
+ # # Get all squares diagonal from square_a
217
+ # square_set.diagonal(square_a)
218
+ def diagonal(origin)
219
+ select { |square| Vector.new(origin, square).diagonal? }
220
+ end
221
+
222
+ # Find all squares orthogonal or diagonal from square
223
+ #
224
+ # @param [Square] square
225
+ # the originating square
226
+ #
227
+ # @return [SquareSet]
228
+ # ==== Example:
229
+ # # Get all squares orthogonal or diagonal from square_a
230
+ # square_set.orthogonal_or_diagonal(square_a)
231
+ def orthogonal_or_diagonal(origin)
232
+ select { |square| Vector.new(origin, square).orthogonal_or_diagonal? }
233
+ end
234
+
235
+ # Find all squares not orthogonal or diagonal from square
236
+ #
237
+ # @param [Square] square
238
+ # the originating square
239
+ #
240
+ # @return [SquareSet]
241
+ # ==== Example:
242
+ # # Get all squares not orthogonal or diagonal from square_a
243
+ # square_set.not_orthogonal_or_diagonal(square_a)
244
+ def not_orthogonal_or_diagonal(origin)
245
+ select { |square| Vector.new(origin, square).not_orthogonal_or_diagonal? }
246
+ end
247
+
248
+ # Find all squares without pieces on them.
249
+ #
250
+ # @return [SquareSet]
251
+ def unoccupied
252
+ select(&:unoccupied?)
253
+ end
254
+
255
+ # Returns destination from the origin that have a clear path
256
+ #
257
+ # @param [Square] origin
258
+ # the originating square.
259
+ #
260
+ # @param [SquareSet] square_set
261
+ # the board position.
262
+ #
263
+ # @return [SquareSet]
264
+ def unblocked(origin, square_set)
265
+ select { |destination| square_set.between(origin, destination).all?(&:unoccupied?) }
266
+ end
267
+
268
+ # Returns squares between a and b.
269
+ # Only squares that are in the same diagonal will return squares.
270
+ #
271
+ # @param [Square] a
272
+ # a square.
273
+ #
274
+ # @param [Square] b
275
+ # another square.
276
+ #
277
+ # @return [SquareSet]
278
+ #
279
+ # ==== Example:
280
+ # # Get all squares between square_a and square_b
281
+ # square_set.between(square_a, square_b)
282
+ def between(origin, destination)
283
+ vector = Vector.new(origin, destination)
284
+
285
+ if vector.diagonal? || vector.orthogonal?
286
+ point_counter = origin.point
287
+ direction = vector.direction
288
+ _squares = []
289
+
290
+ while point_counter != destination.point
291
+ point_counter = point_counter + direction
292
+ square = find_by_x_and_y(point_counter.x, point_counter.y)
293
+ if square && square.point != destination.point
294
+ _squares.push(square)
295
+ end
296
+ end
297
+ else
298
+ _squares = []
299
+ end
300
+
301
+ self.class.new(squares: _squares)
302
+ end
303
+
304
+ # serializes the squares as a hash
305
+ #
306
+ # @return [Hash]
307
+ def as_json
308
+ squares.map(&:as_json)
309
+ end
310
+ end
311
+ end
@@ -0,0 +1,86 @@
1
+ require 'board_game_grid/direction'
2
+
3
+ module BoardGameGrid
4
+
5
+ # = Vector
6
+ #
7
+ # An element of Vector space
8
+ class Vector
9
+
10
+ # New objects can be instantiated by passing in a two points with x and y co-ordinates
11
+ #
12
+ # @param [Point] origin
13
+ # the start point.
14
+ #
15
+ # @param [Point] destination
16
+ # the end point.
17
+ #
18
+ # ==== Example:
19
+ # # Instantiates a new Vector
20
+ # BoardGameGrid::Vector.new(
21
+ # BoardGameGrid::Point.new(x: 1, y: 1),
22
+ # BoardGameGrid::Point.new(x: 3, y: 3)
23
+ # )
24
+ def initialize(origin, destination)
25
+ @origin, @destination = origin, destination
26
+ end
27
+
28
+ # @return [Object] the origin.
29
+ attr_reader :origin
30
+
31
+ # @return [Object] the destination.
32
+ attr_reader :destination
33
+
34
+ # The direction of the vector as a object
35
+ #
36
+ # @return [Direction]
37
+ def direction
38
+ Direction.new(dx, dy)
39
+ end
40
+
41
+ # The biggest difference between co-ordinates
42
+ #
43
+ # @return [Fixnum]
44
+ def magnitude
45
+ [dx.abs, dy.abs].max
46
+ end
47
+
48
+ # Is the vector orthogonal?
49
+ #
50
+ # @return [Boolean]
51
+ def orthogonal?
52
+ dx == 0 || dy == 0
53
+ end
54
+
55
+ # Is the vector diagonal?
56
+ #
57
+ # @return [Boolean]
58
+ def diagonal?
59
+ dx.abs == dy.abs
60
+ end
61
+
62
+ # Is the vector not orthogonal or diagonal?
63
+ #
64
+ # @return [Boolean]
65
+ def not_orthogonal_or_diagonal?
66
+ !(orthogonal? || diagonal?)
67
+ end
68
+
69
+ # Is the vector orthogonal or diagonal?
70
+ #
71
+ # @return [Boolean]
72
+ def orthogonal_or_diagonal?
73
+ orthogonal? || diagonal?
74
+ end
75
+
76
+ private
77
+
78
+ def dx
79
+ destination.x - origin.x
80
+ end
81
+
82
+ def dy
83
+ destination.y - origin.y
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module BoardGameGrid
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: board_game_grid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mark Humphreys
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Classes that help interpret and operation on the state of a grid of squares
56
+ with pieces on them.
57
+ email:
58
+ - mark@mrlhumphreys.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - board_game_grid.gemspec
72
+ - lib/board_game_grid.rb
73
+ - lib/board_game_grid/direction.rb
74
+ - lib/board_game_grid/point.rb
75
+ - lib/board_game_grid/square.rb
76
+ - lib/board_game_grid/square_set.rb
77
+ - lib/board_game_grid/vector.rb
78
+ - lib/board_game_grid/version.rb
79
+ homepage: https://github.com/mrlhumphreys/board_game_grid
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '2.2'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.11
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Data structure and logic for a board game grid.
103
+ test_files: []