board_game_grid 0.1.4 → 0.1.5

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: fd36b2b988acedde04d34d32c977268766edbe370565d7e4afc1af072e0bd49d
4
- data.tar.gz: e47c5ab5280c737da98d3757ac35864aaab0cb10d435a25ec6e8ee53db11c63d
3
+ metadata.gz: 10e78c90bda05726eb9b8b0d68f4526fa872279c063089eeb5ac0729411fadf2
4
+ data.tar.gz: 832fb5ae71b1a0e6be4a56ff8b1e4cbcfd694315a8ed7c628cf47d4c1ba152eb
5
5
  SHA512:
6
- metadata.gz: 7b9d4d05e209bad2899a5b721c457272d23e6a1f6242fa8015c3f7aac4f86bb490de63d195012f42df157d6e35bcf91ac3f8f601006511739c521f34c2e0d1e3
7
- data.tar.gz: ecb84eb4ed4e7f076a37aed8203409ee8e5275560d6b5368188ee865bcaa5911c090ae7e6034b54dd44d9ec00e6e595df00597d2471ea547ec959dbdbdbd1a1e
6
+ metadata.gz: 196ded6b6dbfa358570c041e9db00b7df582e59aaa92ab0cde8cd4121bac68db6a4eb2e9a13c72fe111f642c079b052742b185d441571913a78274980b8c95f2
7
+ data.tar.gz: dee9e3fdcd61efb4c6b8b9fcf9f549b930b4c6da2ba3b1e202ac62689542e537a8200e21d5c9bf86f9f6ef5708371ce3d32f085b23321a7a554ad3d821535e37
@@ -0,0 +1,124 @@
1
+ module BoardGameGrid
2
+
3
+ # = Piece
4
+ #
5
+ # A piece that can move on a board
6
+ class Piece
7
+ # The forwards direciton of the piece for each player
8
+ FORWARDS_DIRECTION = { 1 => -1, 2 => 1 }
9
+
10
+ def initialize(id: , player_number: , type: nil)
11
+ @id = id
12
+ @player_number = player_number
13
+ end
14
+
15
+ # @return [Fixnum] the identifier of the piece.
16
+ attr_reader :id
17
+
18
+ # @return [Fixnum] the owner of the piece.
19
+ attr_reader :player_number
20
+
21
+ # The opposing player number
22
+ #
23
+ # @return [Fixnum]
24
+ def opponent
25
+ player_number == 1 ? 2 : 1
26
+ end
27
+
28
+ # The stringified identifier of the piece type.
29
+ #
30
+ # @return [Fixnum]
31
+ def type
32
+ self.class.to_s.split('::').last.downcase
33
+ end
34
+
35
+ # Can the piece move from a square, to a square, given the game state?
36
+ #
37
+ # @param [Square] from
38
+ # the origin square.
39
+ #
40
+ # @param [Square] to
41
+ # the destination square.
42
+ #
43
+ # @param [GameState] game_state
44
+ # the current game state.
45
+ #
46
+ # @return [Boolean]
47
+ def can_move?(from, to, game_state)
48
+ destinations(from, game_state).include?(to)
49
+ end
50
+
51
+ # All the squares that the piece can move to and/or capture.
52
+ #
53
+ # @param [Square] square
54
+ # the origin square.
55
+ #
56
+ # @param [GameState] game_state
57
+ # the current game state.
58
+ #
59
+ # @return [SquareSet]
60
+ def destinations(square, game_state)
61
+ []
62
+ end
63
+
64
+ # All the squares that the piece can move to.
65
+ #
66
+ # @param [Square] square
67
+ # the origin square.
68
+ #
69
+ # @param [GameState] game_state
70
+ # the current game state.
71
+ #
72
+ # @return [SquareSet]
73
+ def move_squares(square, game_state)
74
+ destinations(square, game_state)
75
+ end
76
+
77
+ # All the squares that the piece can capture.
78
+ #
79
+ # @param [Square] square
80
+ # the origin square.
81
+ #
82
+ # @param [GameState] game_state
83
+ # the current game state.
84
+ #
85
+ # @return [SquareSet]
86
+ def capture_squares(squares, game_state)
87
+ destinations(squares, game_state)
88
+ end
89
+
90
+ # All the squares that the piece could potentially capture. (i.e. if a piece was there)
91
+ #
92
+ # @param [Square] square
93
+ # the origin square.
94
+ #
95
+ # @param [GameState] game_state
96
+ # the current game state.
97
+ #
98
+ # @return [SquareSet]
99
+ def potential_capture_squares(square, game_state)
100
+ capture_squares(square, game_state)
101
+ end
102
+
103
+ # returns a serialized piece as a hash
104
+ #
105
+ # @return [Hash]
106
+ def as_json
107
+ {
108
+ id: id,
109
+ player_number: player_number,
110
+ type: type
111
+ }
112
+ end
113
+
114
+ # returns the direction the piece moves
115
+ #
116
+ # player 1 moves up (-1)
117
+ # player 2 moves down (1)
118
+ #
119
+ # @return [Fixnum]
120
+ def forwards_direction
121
+ FORWARDS_DIRECTION[player_number]
122
+ end
123
+ end
124
+ end
@@ -1,3 +1,3 @@
1
1
  module BoardGameGrid
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: board_game_grid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Humphreys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-10 00:00:00.000000000 Z
11
+ date: 2020-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - board_game_grid.gemspec
72
72
  - lib/board_game_grid.rb
73
73
  - lib/board_game_grid/direction.rb
74
+ - lib/board_game_grid/piece.rb
74
75
  - lib/board_game_grid/point.rb
75
76
  - lib/board_game_grid/square.rb
76
77
  - lib/board_game_grid/square_set.rb