boardsy 0.1.1 → 0.2.0

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: c2b6ba54499ebfb0bbaaf7046b03cbde035c5154962af7b0d98a4c064c5fdb4f
4
- data.tar.gz: b88c6afbea96666a1a70d6364fe9b388d04f202693b4f6b596bcd13684112a33
3
+ metadata.gz: b750bc02093aeb9aea661c3b0d7bf93caa6cc2be38fabeffd433ca8522db2e6f
4
+ data.tar.gz: ec74970ab7983c3147f65836eda15465d261ee14bdb8e9b40b2919b13da844f7
5
5
  SHA512:
6
- metadata.gz: 0abb47150186fb05a6eb4e356f06169dedc00925a7b584460b27b977b65dade0b58e300c0cc57d1d89dd3d4039f0ffe663f28730efad396a128b17bf53416450
7
- data.tar.gz: 0f0e12cf1c7d39bd703446c1c19e4897c59231a188d90b780298d0d44271cb9095a57cdad9d59b97a46a528a0ae9ba770c509bb39cd96c014794c1816dd935dc
6
+ metadata.gz: c032c995235e197e62821bf4a9e8d386a5d8085e060756f139c5645886b516862fd4683984bb289eee350c848b0a1973dc61ce58222ffad26e339e35b7b68a3c
7
+ data.tar.gz: dcedcc62f3221897f2a64eb69a7cb40e34916324a7cfe789e00279329ac62409da286bac48d24c9c966c5a80e73d29344f9061c13d1fca717b62cc0851b6546b
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- boardsy (0.1.1)
4
+ boardsy (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Boardsy
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/boardsy`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ TODO: describe the gem
6
4
 
7
5
  ## Installation
8
6
 
@@ -6,20 +6,11 @@ Gem::Specification.new do |spec|
6
6
  spec.authors = ["Bennett Struttman"]
7
7
  spec.email = ["ben.struttman@gmail.com"]
8
8
 
9
- spec.summary = %q{A game board for CLI games in Ruby.}
10
- # spec.description = %q{TODO: Write a longer description or delete this line.}
9
+ spec.summary = %q{A game board class for CLI games in Ruby.}
11
10
  spec.homepage = "https://github.com/BenStrutt/Boardsy"
12
11
  spec.license = "MIT"
13
12
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
13
 
15
- # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
-
17
- # spec.metadata["homepage_uri"] = spec.homepage
18
- # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19
- # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
20
-
21
- # Specify which files should be added to the gem when it is released.
22
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
14
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
15
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
16
  end
@@ -1,12 +1,7 @@
1
1
  require "boardsy/version"
2
+ require_relative 'lib/board'
3
+ require_relative 'lib/square'
2
4
 
3
5
  module Boardsy
4
6
  class Error < StandardError; end
5
-
6
- class Board
7
- def self.testing
8
- puts "Hello World!"
9
- end
10
- end
11
- # Your code goes here...
12
- end
7
+ end
@@ -0,0 +1,186 @@
1
+ class Board
2
+ include Enumerable
3
+
4
+ attr_reader :grid
5
+
6
+ def initialize(rows, cols = rows, value: nil)
7
+ @grid = Array.new(cols) do |n|
8
+ Array.new(rows) do |m|
9
+ x = m.next; y = cols - n
10
+ value = yield(x,y,value) if block_given?
11
+ Square.new(value, x, y)
12
+ end
13
+ end
14
+ end
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.'
21
+ end
22
+
23
+ def index(coords)
24
+ x,y = format_coords(coords)
25
+ [grid.size - y, x - 1]
26
+ end
27
+
28
+ def rows
29
+ grid.size
30
+ end
31
+
32
+ def cols
33
+ grid.transpose.size
34
+ end
35
+
36
+ def squares
37
+ each.to_a
38
+ end
39
+
40
+ alias :columns :cols
41
+
42
+ def col(*coords)
43
+ grid.transpose[format_coords(coords).first - 1].reverse
44
+ end
45
+
46
+ alias :column :col
47
+
48
+ def row(*coords)
49
+ grid[grid.size - (format_coords(coords).last)]
50
+ end
51
+
52
+ # TODO: write tests for diagonals
53
+ def diagonals(coords)
54
+ coords = format_coords(coords)
55
+ diagonals = []
56
+ length = (1..grid.size); width = (1..grid[0].size)
57
+ [[1,1],[-1,1]].each do |m,n|
58
+ diagonal = []
59
+ x = coords.first; y = coords.last
60
+ while width.include?(x) && length.include?(y)
61
+ x -= m; y -= n
62
+ end
63
+ x += m; y += n
64
+ while width.include?(x) && length.include?(y)
65
+ diagonal << square(x,y)
66
+ x += m; y += n
67
+ end
68
+ diagonals << diagonal unless diagonal.size == 1
69
+ end
70
+ diagonals
71
+ end
72
+
73
+ def same_row?(*squares)
74
+ squares.map! { |sq| format_coords(sq) }
75
+ squares.all? { |_,y| y == squares.first[1] }
76
+ end
77
+
78
+ def same_col?(*squares)
79
+ squares.map! { |sq| format_coords(sq) }
80
+ squares.all? { |x,_| x == squares.first[0] }
81
+ end
82
+
83
+ alias :same_column? :same_col?
84
+
85
+ def same_diagonal?(*squares)
86
+ squares.map! { |sq| square(sq) }
87
+ diagonals(squares.first).any? do |diagonal|
88
+ squares.all? { |square| diagonal.include?(square) }
89
+ end
90
+ end
91
+
92
+ #TODO: there should be methods to flip the entire board, not only the values
93
+ #TODO: add a rotate method
94
+ def flip!(axis = :x)
95
+ raise ArgumentError unless [:x, :y].include?(axis.to_sym)
96
+ send("flip_#{axis}!")
97
+ end
98
+
99
+ def flip_x!
100
+ (0...grid.first.size).each do |m|
101
+ (0...grid.size / 2).each do |n|
102
+ sq1 = grid[n][m]
103
+ sq2 = grid[(grid.size - 1) - n][m]
104
+ sq1.value, sq2.value = sq2.value, sq1.value
105
+ end
106
+ end
107
+ self
108
+ end
109
+
110
+ def flip_y!
111
+ (0...grid.first.size / 2).each do |m|
112
+ (0...grid.size).each do |n|
113
+ sq1 = grid[n][m]
114
+ sq2 = grid[n][(grid.first.size - 1) - m]
115
+ sq1.value, sq2.value = sq2.value, sq1.value
116
+ end
117
+ end
118
+ self
119
+ end
120
+
121
+ def flip(axis = :x)
122
+ raise ArgumentError unless [:x, :y].include?(axis.to_sym)
123
+ flip!(axis)
124
+ board = Board.new(grid.first.size, grid.size) do |x,y|
125
+ square(x,y).value
126
+ end
127
+ flip!(axis)
128
+ board
129
+ end
130
+
131
+ def flip_x
132
+ flip(:x)
133
+ end
134
+
135
+ def flip_y
136
+ flip(:y)
137
+ end
138
+
139
+ #The each method iterates over the board starting from the bottom left
140
+ #square to the top right square.
141
+ def each
142
+ if block_given?
143
+ grid.reverse_each { |row| row.each { |square| yield(square) } }
144
+ else
145
+ Enumerator.new do |y|
146
+ grid.reverse_each { |row| row.each { |square| y << square } }
147
+ end
148
+ end
149
+ end
150
+
151
+ def to_s
152
+ grid.map do |row|
153
+ row.map { |square| square.value }.join(' ')
154
+ end.join("\n")
155
+ end
156
+
157
+ def [](idx)
158
+ grid[idx]
159
+ end
160
+
161
+ private
162
+
163
+ #write tests for this method
164
+ #any method that uses the format_coords method can also accept Square objects
165
+ def format_coords(coords)
166
+ format_coords_into_array(coords).map do |c|
167
+ raise ArgumentError unless c.to_s =~ /[0-9a-zA-Z]/
168
+ ord = c.ord
169
+ if c.is_a? Integer
170
+ c
171
+ else
172
+ c =~ /[a-zA-Z]/ ? c =~ /[a-z]/ ? ord - 96 : ord - 64 : c.to_i
173
+ end
174
+ end
175
+ end
176
+
177
+ def format_coords_into_array(coords)
178
+ coords = coords.first if coords.is_a?(Array) && coords.size == 1
179
+ coords = coords.coords if coords.is_a?(Square)
180
+ coords = coords.chars if coords.is_a?(String)
181
+ unless coords.size == 2
182
+ raise ArgumentError, "Expected an 'x' and a 'y' coordinate."
183
+ end
184
+ coords
185
+ end
186
+ end
@@ -0,0 +1,16 @@
1
+ class Square
2
+ attr_accessor :value
3
+ attr_reader :x, :y
4
+
5
+ def initialize(value, x, y)
6
+ @value,@x,@y = value,x,y
7
+ end
8
+
9
+ def coords
10
+ [x, y]
11
+ end
12
+
13
+ def empty?
14
+ value.nil?
15
+ end
16
+ end
@@ -1,3 +1,3 @@
1
1
  module Boardsy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: boardsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bennett Struttman
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-05 00:00:00.000000000 Z
11
+ date: 2020-12-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -29,6 +29,8 @@ files:
29
29
  - bin/setup
30
30
  - boardsy.gemspec
31
31
  - lib/boardsy.rb
32
+ - lib/boardsy/lib/board.rb
33
+ - lib/boardsy/lib/square.rb
32
34
  - lib/boardsy/version.rb
33
35
  homepage: https://github.com/BenStrutt/Boardsy
34
36
  licenses:
@@ -52,5 +54,5 @@ requirements: []
52
54
  rubygems_version: 3.0.6
53
55
  signing_key:
54
56
  specification_version: 4
55
- summary: A game board for CLI games in Ruby.
57
+ summary: A game board class for CLI games in Ruby.
56
58
  test_files: []