boardsy 0.2.0 → 0.2.1

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: b750bc02093aeb9aea661c3b0d7bf93caa6cc2be38fabeffd433ca8522db2e6f
4
- data.tar.gz: ec74970ab7983c3147f65836eda15465d261ee14bdb8e9b40b2919b13da844f7
3
+ metadata.gz: 7647121cd08dd81183448c76927556e7a3108b8ba7389aa1c07b2e725f9f6bad
4
+ data.tar.gz: d40ab647023bceecd9cfd1ee36000e74af399a52e501f72cb9acb726ffd5c870
5
5
  SHA512:
6
- metadata.gz: c032c995235e197e62821bf4a9e8d386a5d8085e060756f139c5645886b516862fd4683984bb289eee350c848b0a1973dc61ce58222ffad26e339e35b7b68a3c
7
- data.tar.gz: dcedcc62f3221897f2a64eb69a7cb40e34916324a7cfe789e00279329ac62409da286bac48d24c9c966c5a80e73d29344f9061c13d1fca717b62cc0851b6546b
6
+ metadata.gz: 05d95b4953afaeb3a5580f5c9f827e3e3182f5f0c934fe30891030fc113371bdf0a42e60d2ba708e9d86e01fcc6897a1efc1117cf9b0e2d2a54f1a4777c42a40
7
+ data.tar.gz: 4556cb6740b1dff68354ef98385acb151d670e6edf5ceb54ea9ee92ab90233d5bd46aa4dcbd9165ba9aec22ef80dcffaa8ab97d6f5d5e91ff839bad6e390c272
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boardsy (0.2.0)
4
+ boardsy (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # Boardsy
2
2
 
3
- TODO: describe the gem
3
+ This gem provides a `Board` class to build Command Line Interface board games in Ruby. See the "Usage" section below for available methods.
4
+ This project is actively being worked on. Additional functionality will be added on a consistent basis.
4
5
 
5
6
  ## Installation
6
7
 
@@ -20,7 +21,177 @@ Or install it yourself as:
20
21
 
21
22
  ## Usage
22
23
 
23
- TODO: Write usage instructions here
24
+ The board is stored by the class as a 2D array of `Square` objects.
25
+ Each `Square` object contains a value and its x and y coordinates within the board.
26
+ By default, each `Square` object is initialized with its value set to `nil`.
27
+
28
+ ### Initialization
29
+
30
+ The board is initialized as an 8x8 board by default.
31
+
32
+ ```ruby
33
+ Board.new # returns a Board object of 8x8 Square objects
34
+ ```
35
+
36
+ The length and width can be specified by passing a single argument.
37
+
38
+ ```ruby
39
+ Board.new(4) # returns a Board object of 4x4 Square objects
40
+ ```
41
+
42
+ The length and width can be individually specified by passing an 'x' value and a 'y' value as arguments.
43
+
44
+ ```ruby
45
+ Board.new(6,8) # returns a Board object of 6x8 Square objects
46
+ ```
47
+
48
+ A `value` argument can be passed to initialize each `Square` object with the value passed.
49
+
50
+ ```ruby
51
+ Board.new(6, 8, value: 'empty')
52
+ ```
53
+
54
+ In addition, a block can be passed to `Board#new` to assign a value to each `Square` object.
55
+
56
+ ```ruby
57
+ Board.new(4) { |x,y| (x.odd? && y.odd?) ? 'X' : 'O' } # returns a Board object laid out as the following:
58
+ #|O|O|O|O|
59
+ #|X|O|X|O|
60
+ #|O|O|O|O|
61
+ #|X|O|X|O|
62
+ ```
63
+
64
+ ### Enumerable methods
65
+
66
+ The `Board` class includes the `Enumerable` module, and has access to each method provided `Enumerable`.
67
+ The squares of the board are passed row by row starting from the bottom left-square and ending with the upper-right square.
68
+ Calling `Board#each` without passing a block will return an `Enumerable` object.
69
+
70
+ ### Methods for retrieving information about the board
71
+
72
+ `Board#length` returns the length of the board as an integer.
73
+ `Board#width` returns the width of the board as an integer.
74
+
75
+ ```ruby
76
+ board = Board.new(5,8)
77
+ board.length # 5
78
+ board.width # 8
79
+ ```
80
+
81
+ `Board#squares` will return an array of each `Square` object, in the order that they are iterated by `Board#each`
82
+
83
+ ```ruby
84
+ board = Board.new(2)
85
+ board.squares.map { |sq| sq.coords } # [[1,1], [2,1], [1,2], [2,2]]
86
+ ```
87
+
88
+ ### Methods for selection portions of the board.
89
+
90
+ `Board#square` returns the `Square` object as specified by the coordinates passed as an argument.
91
+ The 'x' and 'y' coordinates can be passed as individual arguments, an `[x,y]` array, or an `'xy'` string.
92
+ The coordinates can additionally be passed an algebraic format, as done in chess: `d4`, `['d',5]`.
93
+ This also applies to any following method that takes coordinates as an argument.
94
+
95
+ ```ruby
96
+ board = Board.new
97
+ board.square(4,4)
98
+ board.square([7,2])
99
+ board.square('a5')
100
+ ```
101
+
102
+ `Board#row` returns the row,from left to right as an array, in which the given square is located.
103
+ `Board#col` or `Board#column` returns the column, from bottom to top as an array, in which the given square is located.
104
+ Both methods will accept a `Square` object as an argument as well as coordinates formatted in the same way as `Board#square`.
105
+
106
+ ```ruby
107
+ board = Board.new
108
+ board.row(5,8) # returns an array of the squares in the top row
109
+ board.col('a7') # returns an array of the squares in the left-most column
110
+
111
+ square = board.square(['h',4])
112
+ board.col(square) # returns an array of the squares in the right-most column
113
+ ```
114
+
115
+ `Board#diagonals` returns a 2D array where each nested array is an array of squares along a diagonal which intersects the passed coordinates.
116
+ This method accepts the same argument formats as `Board#row` and `Board#col`.
117
+
118
+ ```ruby
119
+ board = Board.new(4)
120
+ diagonals = board.diagonals(2,2)
121
+ diagonals.each { |diagonal| p diagonal.map { |sq| sq.coords } }
122
+ => [[1,1], [2,2], [3,3], [4,4]]
123
+ => [[1,3], [2,2], [3,1]]
124
+ ```
125
+
126
+ The standard `[]` notation can be used to select an array of squares by standard indexing.
127
+
128
+ ```ruby
129
+ board = Board.new(8)
130
+ square = board[0][3]
131
+ square.coords # [4,8]
132
+ ```
133
+
134
+ The `Board#index` method can be used to retrieve the indexes of a square within the 2D array that the board is represented in.
135
+
136
+ ```ruby
137
+ board = Board.new(4)
138
+ board.index([3,3]) # [1,2]
139
+ board[1][2] # [3,3]
140
+ ```
141
+
142
+ ### Testing the relationship between squares
143
+
144
+ Each of the following methods accepts an array of coordinates or an array of `Square` objects as an argument and returns a boolean value.
145
+ `Board#same_row?`, `Board#same_col?` (alias: `Board#same_column?`), `Board#same_diagonal?`.
146
+
147
+ ```ruby
148
+ board = Board.new
149
+ board.same_row?('a4', 'd4', 'g4') # true
150
+ board.same_col?([3,2], [3,7], [2,5]) # false
151
+
152
+ sq1 = board.square('a1')
153
+ sq2 = board.square('d4')
154
+ board.same_diagonal?(sq1,sq2) # true
155
+ ```
156
+
157
+ ### Altering the orientation of the board
158
+
159
+ The `Board` class provides methods for flipping the board.
160
+ At the moment, these methods will only flip the values of the squares along the given axis. The coordinates of the squares will stay the same. Methods allowing 'hard' flips will be added soon.
161
+ Methods succeeded by a '!' will mutate the `Board` object itself. Methods without the exclamation mark will leave the original `Board` object and return a new `Board` object.
162
+ The current available methods are `flip`, `flip!`, `flip_x`, `flip_y`, `flip_x!`, and `flip_y!`.
163
+ `flip` and `flip!` take as an argument the axis as either a string or a symbol. By default they flip along the x axis.
164
+
165
+ ```ruby
166
+ board = Board.new(3, value: 'O')
167
+ board.square([1,2]).value = 'X'
168
+ board.square([3,3]).value = 'X'
169
+ board_x = board.flip(:x)
170
+ board_y = board.flip('y')
171
+
172
+ board # returns the following Board object:
173
+ # |O|O|X|
174
+ # |X|O|O|
175
+ # |O|O|O|
176
+
177
+ board_x # returns the following Board object:
178
+ # |O|O|O|
179
+ # |X|O|O|
180
+ # |O|O|X|
181
+
182
+ board_y # returns the following Board object:
183
+ # |X|O|O|
184
+ # |O|O|X|
185
+ # |O|O|O|
186
+
187
+ board.flip!('x')
188
+ board.flip!(:y)
189
+
190
+ board # now returns the folling Board object:
191
+ # |O|O|O|
192
+ # |O|O|X|
193
+ # |X|O|O|
194
+ ```
24
195
 
25
196
  ## Development
26
197
 
@@ -13,32 +13,25 @@ class Board
13
13
  end
14
14
  end
15
15
 
16
- #TODO: write exception tests
17
- def square(*coords)
18
- coords = format_coords(coords)
19
- each { |square| return square if square.coords == coords }
20
- raise ArgumentError, 'Coordinates passed are out of bounds.'
16
+ def length
17
+ grid.first.size
21
18
  end
22
19
 
23
- def index(coords)
24
- x,y = format_coords(coords)
25
- [grid.size - y, x - 1]
26
- end
27
-
28
- def rows
20
+ def width
29
21
  grid.size
30
22
  end
31
23
 
32
- def cols
33
- grid.transpose.size
34
- end
35
-
36
24
  def squares
37
25
  each.to_a
38
26
  end
39
27
 
40
- alias :columns :cols
41
-
28
+ #TODO: write exception tests
29
+ def square(*coords)
30
+ coords = format_coords(coords)
31
+ each { |square| return square if square.coords == coords }
32
+ raise ArgumentError, 'Coordinates passed are out of bounds.'
33
+ end
34
+
42
35
  def col(*coords)
43
36
  grid.transpose[format_coords(coords).first - 1].reverse
44
37
  end
@@ -121,9 +114,7 @@ class Board
121
114
  def flip(axis = :x)
122
115
  raise ArgumentError unless [:x, :y].include?(axis.to_sym)
123
116
  flip!(axis)
124
- board = Board.new(grid.first.size, grid.size) do |x,y|
125
- square(x,y).value
126
- end
117
+ board = Board.new(grid.first.size, grid.size) { |x,y| square(x,y).value }
127
118
  flip!(axis)
128
119
  board
129
120
  end
@@ -158,6 +149,11 @@ class Board
158
149
  grid[idx]
159
150
  end
160
151
 
152
+ def index(coords)
153
+ x,y = format_coords(coords)
154
+ [grid.size - y, x - 1]
155
+ end
156
+
161
157
  private
162
158
 
163
159
  #write tests for this method
@@ -1,3 +1,3 @@
1
1
  module Boardsy
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boardsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bennett Struttman