just_checkers 0.1.1 → 0.1.2

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
  SHA1:
3
- metadata.gz: bd7a80ff97dc8a7dd9864f66f6c2433cb8b0d662
4
- data.tar.gz: 751f933d40e7bbc0bd2c22c4448434f8e968a8d5
3
+ metadata.gz: 8e631bf4123ccf99a1c93a0e06eb3b8a46c79682
4
+ data.tar.gz: 6c872f95204b1afa6912fac02c34141466efedc2
5
5
  SHA512:
6
- metadata.gz: 957dc61a18c7b28d959a142e2a111acc1b8b0554630e4a1252409fbb0aa6f35df5ff9c0a040e397c2cb888fff53099e21a56569d8e278ef84d85db850322c0e4
7
- data.tar.gz: 840fc894b1276b8ef9708296c6841c655ce0e432cfcb7c18acaa8b41681b85b38c2673f79f6376b021036c5e1eb3d1b60a1df4c755d490ab4fac91975b45873c
6
+ metadata.gz: 8e7c12394334a4765f76de167165038745e22f6ed64c82c78818ac1a6abadc583613572ed427906e36076c5dbb21a560229952cecf15bab6442b3e6faf8e0bbb
7
+ data.tar.gz: 8e723f660f4a4c72b922262265de30a470012a0f8ce81b8458c9b6f995e1d78c5b9252a923b6c5ebedc569d8c3b84178944b2c21f94cb31fba83764fb9960471
data/README.md CHANGED
@@ -51,8 +51,6 @@ Also, the game can be serialized into a hash.
51
51
 
52
52
  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.
53
53
 
54
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
55
-
56
54
  ## Contributing
57
55
 
58
56
  Bug reports and pull requests are welcome on GitHub at https://github.com/mrlhumphreys/just_checkers. 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.
@@ -7,8 +7,11 @@ module JustCheckers
7
7
 
8
8
  # New objects can be instantiated with
9
9
  #
10
- # * +x+ - x magnitude
11
- # * +y+ - y magnitude
10
+ # @param [Fixnum] x
11
+ # the x magnitude.
12
+ #
13
+ # @param [Fixnum] y
14
+ # the y magnitude.
12
15
  #
13
16
  # ==== Example:
14
17
  # # Instantiates a new Direction
@@ -20,9 +23,18 @@ module JustCheckers
20
23
  @x, @y = x, y
21
24
  end
22
25
 
23
- attr_reader :x, :y
26
+ # @return [Fixnum] the x magnitude.
27
+ attr_reader :x
28
+
29
+ # @return [Fixnum] the y magnitude.
30
+ attr_reader :y
24
31
 
25
- # check if directions are equal by seeing if their magnitudes are equal.
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]
26
38
  def ==(other)
27
39
  self.x == other.x && self.y == other.y
28
40
  end
@@ -7,10 +7,16 @@ module JustCheckers
7
7
  # Represents a game of Checkers in progress.
8
8
  class GameState
9
9
 
10
- # New objects can be instantiated by passing in a hash with
10
+ # New objects can be instantiated.
11
11
  #
12
- # * +current_player_number+ - Who's turn it is, 1 or 2
13
- # * +squares+ - An array of squares, each with x and y co-ordinates and a piece.
12
+ # @param [Hash] args
13
+ # The data needed for a game
14
+ #
15
+ # @option args [Fixnum] :current_player_number
16
+ # Who's turn it is, 1 or 2
17
+ #
18
+ # @option args [Array<Square>] :squares
19
+ # An array of squares, each with x and y co-ordinates and a piece.
14
20
  #
15
21
  # ==== Example:
16
22
  # # Instantiates a new Game of Checkers
@@ -22,11 +28,22 @@ module JustCheckers
22
28
  # })
23
29
  def initialize(args = {})
24
30
  @current_player_number = args[:current_player_number]
25
- @squares = SquareSet.new(squares: args[:squares])
31
+ @squares = SquareSet.new(squares: args[:squares] || [])
26
32
  @messages = []
27
33
  end
34
+
35
+ # @return [Fixnum] who's turn it is.
36
+ attr_reader :current_player_number
37
+
38
+ # @return [Array<Square>] the board state.
39
+ attr_reader :squares
40
+
41
+ # @return [Array<String>] useful messages if any.
42
+ attr_reader :messages
28
43
 
29
44
  # Instantiates a new GameState object in the starting position
45
+ #
46
+ # @return [GameState]
30
47
  def self.default
31
48
  new({
32
49
  current_player_number: 1,
@@ -74,19 +91,23 @@ module JustCheckers
74
91
  })
75
92
  end
76
93
 
77
- attr_reader :current_player_number, :squares, :messages
78
-
79
- # Returns a hash serialized representation of the game state
94
+ # A hashed serialized representation of the game state
95
+ #
96
+ # @return [Hash]
80
97
  def as_json
81
98
  { current_player_number: current_player_number, squares: squares.as_json, winner: winner }
82
99
  end
83
100
 
84
101
  # Returns the json serialized representation of the game state.
102
+ #
103
+ # @return String
85
104
  def to_json
86
105
  as_json.to_json
87
106
  end
88
107
 
89
- # Returns the player number of the winner. It returns nil if there is no winner
108
+ # The player number of the winner. It returns nil if there is no winner.
109
+ #
110
+ # @return [Fixnum,NilClass]
90
111
  def winner
91
112
  if squares.occupied_by(1).none? { |s| s.possible_jumps(s.piece, squares).any? || s.possible_moves(s.piece, squares).any? }
92
113
  2
@@ -99,16 +120,23 @@ module JustCheckers
99
120
 
100
121
  # Moves a piece owned by the player, from one square, to another
101
122
  #
102
- # * +player_number+ - the player number, 1 or 2
103
- # * +from+ - A hash containing x and y co-ordinates.
104
- # * +to+ - An array of hashes containing x and y co-ordinates.
105
- #
106
123
  # It moves the piece and returns true if the move is valid and it's the player's turn.
107
124
  # It returns false otherwise.
108
125
  #
109
126
  # ==== Example:
110
127
  # # Moves a piece from a square to perform a double jump
111
128
  # game_state.move!(1, {x: 0, y: 1}, [{x: 1, y: 2}, {x: 3, y: 4}])
129
+ #
130
+ # @param [Fixnum] player_number
131
+ # the player number, 1 or 2.
132
+ #
133
+ # @param [Hash] from
134
+ # where the moving piece currently is.
135
+ #
136
+ # @param [Array<Hash>] to
137
+ # each place the piece is going to move to.
138
+ #
139
+ # @return [Boolean]
112
140
  def move!(player_number, from, to)
113
141
  @messages = []
114
142
  from_square = squares.find_by_x_and_y(from[:x].to_i, from[:y].to_i)
@@ -6,9 +6,17 @@ module JustCheckers
6
6
 
7
7
  # New objects can be instantiated by passing in a hash with
8
8
  #
9
- # * +player_number+ - The player who owns the piece, 1 or 2
10
- # * +direction+ - The direction forward on the board, 1 for moving down, -1 for moving up.
11
- # * +king+ - Set to true if the piece has been crowned
9
+ # @param [Hash] args
10
+ # The data needed for a piece
11
+ #
12
+ # @option args [Fixnum] player_number
13
+ # the owner of the piece.
14
+ #
15
+ # @option args [Fixnum] direction
16
+ # the direction forward on the board, 1 for moving down, -1 for moving up.
17
+ #
18
+ # @option args [Boolean] king
19
+ # set to true if the piece has been crowned.
12
20
  #
13
21
  # ==== Example:
14
22
  # # Instantiates a new Piece
@@ -23,15 +31,27 @@ module JustCheckers
23
31
  @king = args[:king]
24
32
  end
25
33
 
26
- attr_reader :player_number, :direction, :king
34
+ # @return [Fixnum] the owner of the piece.
35
+ attr_reader :player_number
36
+
37
+ # @return [Fixnum] the direction forward on the board, 1 for moving down, -1 for moving up.
38
+ attr_reader :direction
39
+
40
+ # @return [Boolean] set to true if the piece has been crowned.
41
+ attr_reader :king
42
+
27
43
  alias_method :king?, :king
28
44
 
29
45
  # promotes the piece by setting the +king+ attribute to true.
46
+ #
47
+ # @return [TrueClass]
30
48
  def promote!
31
49
  @king = true
32
50
  end
33
51
 
34
52
  # returns a serialized piece as a hash
53
+ #
54
+ # @return [Hash]
35
55
  def as_json
36
56
  { player_number: player_number, direction: direction, king: king }
37
57
  end
@@ -7,8 +7,11 @@ module JustCheckers
7
7
 
8
8
  # New objects can be instantiated with
9
9
  #
10
- # * +x+ - x co-ordinate
11
- # * +y+ - y co-ordinate
10
+ # @param [Fixnum] x
11
+ # the x co-ordinate.
12
+ #
13
+ # @param [Fixnum] y
14
+ # the y co-ordinate.
12
15
  #
13
16
  # ==== Example:
14
17
  # # Instantiates a new Point
@@ -20,14 +23,28 @@ module JustCheckers
20
23
  @x, @y = x, y
21
24
  end
22
25
 
23
- attr_reader :x, :y
26
+ # @return [Fixnum] the x co-ordinate.
27
+ attr_reader :x
28
+
29
+ # @return [Fixnum] the y co-ordinate.
30
+ attr_reader :y
24
31
 
25
- # add a point to another point by adding their co-ordinates and returning a new point.
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]
26
38
  def +(other)
27
39
  self.class.new(self.x + other.x, self.y + other.y)
28
40
  end
29
41
 
30
- # check if points are equal by seeing if their co-ordinates are equal.
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]
31
48
  def ==(other)
32
49
  self.x == other.x && self.y == other.y
33
50
  end
@@ -10,9 +10,17 @@ module JustCheckers
10
10
 
11
11
  # New objects can be instantiated by passing in a hash with
12
12
  #
13
- # * +x+ - The x co-ordinate of the square.
14
- # * +y+ - The y co-ordinate of the square.
15
- # * +piece+ - The piece on the square, can be a piece object or hash or nil.
13
+ # @param [Hash] args
14
+ # The data needed for a square
15
+ #
16
+ # @option args [Fixnum] x
17
+ # the x co-ordinate of the square.
18
+ #
19
+ # @option args [Fixnum] y
20
+ # the y co-ordinate of the square.
21
+ #
22
+ # @option args [Piece,Hash,NilClass] piece
23
+ # The piece on the square, can be a piece object or hash or nil.
16
24
  #
17
25
  # ==== Example:
18
26
  # # Instantiates a new Square
@@ -31,13 +39,22 @@ module JustCheckers
31
39
  end
32
40
  end
33
41
 
34
- attr_reader :x, :y
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 [Piece,NilClass] The piece on the square if any.
35
49
  attr_accessor :piece
36
50
 
37
51
  # checks if the square matches the attributes passed.
38
52
  #
39
- # * +attribute+ - a symbol for the squares attribute
40
- # * +value+ - a value to match on. Can be a hash of attribute/value pairs for deep matching
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
41
58
  #
42
59
  # ==== Example:
43
60
  # # Check if square has a piece owned by player 1
@@ -55,29 +72,51 @@ module JustCheckers
55
72
  hash_obj_matcher.call(self, attribute, value)
56
73
  end
57
74
 
58
- # returns true if the square has no piece on it.
75
+ # Is the square unoccupied by a piece?
76
+ #
77
+ # @return [Boolean]
59
78
  def unoccupied?
60
79
  piece.nil?
61
80
  end
62
81
 
63
- # returns a point object with the square's co-ordinates.
82
+ # A point object with the square's co-ordinates.
83
+ #
84
+ # @return [Point]
64
85
  def point
65
86
  Point.new(x, y)
66
87
  end
67
88
 
68
- # returns all squares that a piece on this square could jump to, given the board.
89
+ # All squares that a piece on this square could jump to, given the board.
90
+ #
91
+ # @param [Piece] piece
92
+ # the piece on this square
93
+ #
94
+ # @param [SquareSet] squares
95
+ # the board
96
+ #
97
+ # @return [SquareSet]
69
98
  def possible_jumps(piece, squares)
70
99
  squares.two_squares_away_from(self).in_direction_of(piece, self).unoccupied.select do |s|
71
100
  squares.between(self, s).occupied_by_opponent_of(piece.player_number).any?
72
101
  end
73
102
  end
74
103
 
75
- # returns all squares that a piece on this square could jump to, given the board.
104
+ # All squares that a piece on this square could move to, given the board.
105
+ #
106
+ # @param [Piece] piece
107
+ # the piece on this square
108
+ #
109
+ # @param [SquareSet] squares
110
+ # the board
111
+ #
112
+ # @return [SquareSet]
76
113
  def possible_moves(piece, squares)
77
114
  squares.one_square_away_from(self).in_direction_of(piece, self).unoccupied
78
115
  end
79
116
 
80
- # returns a serialized version of the square as a hash
117
+ # A serialized version of the square as a hash
118
+ #
119
+ # @return [Hash]
81
120
  def as_json
82
121
  { x: x, y: y, piece: piece && piece.as_json }
83
122
  end
@@ -13,7 +13,11 @@ module JustCheckers
13
13
  # New objects can be instantiated by passing in a hash with squares.
14
14
  # They can be square objects or hashes.
15
15
  #
16
- # * +squares+ - An array of squares, each with x and y co-ordinates and a piece.
16
+ # @param [Hash] args
17
+ # The data needed for the squares
18
+ #
19
+ # @option args [Array<Square,Hash>] squares
20
+ # An array of squares, each with x and y co-ordinates and a piece.
17
21
  #
18
22
  # ==== Example:
19
23
  # # Instantiates a new Square Set
@@ -30,6 +34,7 @@ module JustCheckers
30
34
  end
31
35
  end
32
36
 
37
+ # @return [Array<Square>] The squares in the set.
33
38
  attr_reader :squares
34
39
 
35
40
  def_delegator :squares, :first
@@ -40,18 +45,27 @@ module JustCheckers
40
45
  def_delegator :squares, :empty?
41
46
 
42
47
  # Iterate over the squares with a block and behaves like Enumerable#each.
48
+ #
49
+ # @return [Enumerable]
43
50
  def each(&block)
44
51
  squares.each(&block)
45
52
  end
46
53
 
47
54
  # Filter the squares with a block and behaves like Enumerable#select.
48
55
  # It returns a SquareSet with the filtered squares.
56
+ #
57
+ # @return [SquareSet]
49
58
  def select(&block)
50
59
  self.class.new(squares: squares.select(&block))
51
60
  end
52
61
 
53
62
  # Filter the squares with a hash of attribute and matching values.
54
63
  #
64
+ # @param [Hash] hash
65
+ # attributes to query for.
66
+ #
67
+ # @return [SquareSet]
68
+ #
55
69
  # ==== Example:
56
70
  # # Find all squares where piece is nil
57
71
  # square_set.where(piece: nil)
@@ -64,6 +78,13 @@ module JustCheckers
64
78
 
65
79
  # Find the square with the matching x and y co-ordinates
66
80
  #
81
+ # @param [Fixnum] x
82
+ # the x co-ordinate.
83
+ #
84
+ # @param [Fixnum] y
85
+ # the y co-ordinate.
86
+ #
87
+ # @return [Square]
67
88
  # ==== Example:
68
89
  # # Find the square at 4,2
69
90
  # square_set.find_by_x_and_y(4, 2)
@@ -72,19 +93,37 @@ module JustCheckers
72
93
  end
73
94
 
74
95
  # Return all squares that are one square away from the passed square.
96
+ #
97
+ # @param [Square] square
98
+ # the square in question.
99
+ #
100
+ # @return [SquareSet]
75
101
  def one_square_away_from(square)
76
102
  select { |s| Vector.new(square, s).magnitude == 1 }
77
103
  end
78
104
 
79
105
  # Return all squares that are two squares away from the passed square.
106
+ #
107
+ # @param [Square] square
108
+ # the square in question.
109
+ #
110
+ # @return [SquareSet]
80
111
  def two_squares_away_from(square)
81
112
  select { |s| Vector.new(square, s).magnitude == 2 }
82
113
  end
83
114
 
84
- # Returns squares in the direction of the piece from the square
115
+ # Find squares in the direction of the piece from the square
85
116
  # If the piece is normal, it returns squares in front of it.
86
117
  # If the piece is king, it returns all squares.
87
118
  #
119
+ # @param [Piece] piece
120
+ # the piece in question.
121
+ #
122
+ # @param [Square] square
123
+ # the square the piece is on.
124
+ #
125
+ # @return [SquareSet]
126
+ #
88
127
  # ==== Example:
89
128
  # # Get all squares in the direction of the piece.
90
129
  # square_set.in_direction_of(piece, square)
@@ -94,7 +133,9 @@ module JustCheckers
94
133
  end
95
134
  end
96
135
 
97
- # Returns all squares without pieces on them.
136
+ # Find all squares without pieces on them.
137
+ #
138
+ # @return [SquareSet]
98
139
  def unoccupied
99
140
  select { |s| s.piece.nil? }
100
141
  end
@@ -102,6 +143,14 @@ module JustCheckers
102
143
  # Returns squares between a and b.
103
144
  # Only squares that are in the same diagonal will return squares.
104
145
  #
146
+ # @param [Square] a
147
+ # a square.
148
+ #
149
+ # @param [Square] b
150
+ # another square.
151
+ #
152
+ # @return [SquareSet]
153
+ #
105
154
  # ==== Example:
106
155
  # # Get all squares between square_a and square_b
107
156
  # square_set.between(square_a, square_b)
@@ -125,12 +174,22 @@ module JustCheckers
125
174
  self.class.new(squares: squares)
126
175
  end
127
176
 
128
- # takes a player number and returns all squares occupied by the opponent of the player
177
+ # Takes a player number and returns all squares occupied by the opponent of the player
178
+ #
179
+ # @param [Fixnum] player_number
180
+ # the player's number.
181
+ #
182
+ # @return [SquareSet]
129
183
  def occupied_by_opponent_of(player_number)
130
184
  select { |s| s.piece && s.piece.player_number != player_number }
131
185
  end
132
186
 
133
- # takes a player number and returns all squares occupied by the player
187
+ # Takes a player number and returns all squares occupied by the player
188
+ #
189
+ # @param [Fixnum] player_number
190
+ # the player's number.
191
+ #
192
+ # @return [SquareSet]
134
193
  def occupied_by(player_number)
135
194
  select { |s| s.piece && s.piece.player_number == player_number }
136
195
  end
@@ -138,6 +197,8 @@ module JustCheckers
138
197
  attr_reader :squares
139
198
 
140
199
  # serializes the squares as a hash
200
+ #
201
+ # @return [Hash]
141
202
  def as_json
142
203
  squares.map(&:as_json)
143
204
  end
@@ -9,8 +9,11 @@ module JustCheckers
9
9
 
10
10
  # New objects can be instantiated by passing in a two points with x and y co-ordinates
11
11
  #
12
- # * +a+ - A point with an x and y co-ordinates.
13
- # * +b+ - A point with an x and y co-ordinates.
12
+ # @param [Point] a
13
+ # the start point.
14
+ #
15
+ # @param [Point] b
16
+ # the end point.
14
17
  #
15
18
  # ==== Example:
16
19
  # # Instantiates a new Vector
@@ -22,9 +25,15 @@ module JustCheckers
22
25
  @a, @b = a, b
23
26
  end
24
27
 
25
- attr_reader :a, :b
28
+ # @return [Point] the start point.
29
+ attr_reader :a
30
+
31
+ # @return [Point] the end point.
32
+ attr_reader :b
26
33
 
27
- # returns how big the vector is if it's diagonal, otherwise, nil.
34
+ # How big the vector is if it's diagonal, otherwise, nil.
35
+ #
36
+ # @return [Fixnum,NilClass]
28
37
  def magnitude
29
38
  if diagonal
30
39
  dx.abs
@@ -33,12 +42,16 @@ module JustCheckers
33
42
  end
34
43
  end
35
44
 
36
- # returns a direction of the vector as a object
45
+ # The direction of the vector as a object
46
+ #
47
+ # @return [Direction]
37
48
  def direction
38
49
  Direction.new(direction_x, direction_y)
39
50
  end
40
51
 
41
- # returns the x component of the direction, 1 if moving down, -1 if moving up, 0 otherwise
52
+ # The x component of the direction, 1 if moving down, -1 if moving up, 0 otherwise
53
+ #
54
+ # @return [Fixnum]
42
55
  def direction_x
43
56
  if dx > 0
44
57
  1
@@ -49,7 +62,9 @@ module JustCheckers
49
62
  end
50
63
  end
51
64
 
52
- # returns the y component of the direction, 1 if moving right, -1 if moving left, 0 otherwise
65
+ # The y component of the direction, 1 if moving right, -1 if moving left, 0 otherwise
66
+ #
67
+ # @return [Fixnum]
53
68
  def direction_y
54
69
  if dy > 0
55
70
  1
@@ -60,7 +75,9 @@ module JustCheckers
60
75
  end
61
76
  end
62
77
 
63
- # returns true if the vector is diagonal.
78
+ # Is the vector diagonal?
79
+ #
80
+ # @return [Boolean]
64
81
  def diagonal
65
82
  dx.abs == dy.abs
66
83
  end
@@ -1,3 +1,3 @@
1
1
  module JustCheckers
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: just_checkers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Humphreys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-17 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -103,3 +103,4 @@ signing_key:
103
103
  specification_version: 4
104
104
  summary: A checkers engine written in ruby
105
105
  test_files: []
106
+ has_rdoc: