c4 0.1.5 → 0.2.0

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
  SHA256:
3
- metadata.gz: 9ea300d10d7612076f7efb5f18cfbfd763e674891d04f850674e3eaa4f2848b5
4
- data.tar.gz: 80c62666aa92cc084e374ce707d533d38a4654d7accc5f13b883d30fffea23aa
3
+ metadata.gz: 66a7aac8830babe8d7ec56990db865b61d4222ad12d7c17ccfac22648d94a568
4
+ data.tar.gz: 116a2ff08fc4cfaa905b390f1016e699238baca6e8eda39644335d6f0f710ce6
5
5
  SHA512:
6
- metadata.gz: 73d105ebb9ce2a126d9ed4bf54fb172d44bc3cdcdc8f185bfd5388b295a33b7c872a4804ac4cffee892350565bdb9ca9a53fcd2dd5ef8bc660095057aba8c231
7
- data.tar.gz: b76aff003e7af64788a1f235417eda15d8c3b49ab601b7efcc669ad1a72ca088b660a510fcef3ba76541bfbc006652acdd3bf6baabca6ad1bb71af637faae2b8
6
+ metadata.gz: 5bb095ce8e3205c0cc21546c38e043a4254eea2eb38627cbc6a6817d651951a08400d79ab26f2849e4555ddeac9a5fe080e174893534d2cd23dda0809c826904
7
+ data.tar.gz: ebb9614deb44861b6ee5a3bab83d40027e55ed7c256cf09e16a020cc9ef66106bedd7543c5b784f9e8825c4c973b3c1d9485a57ef8fa26da9ad1987b4cb9d845
@@ -9,14 +9,19 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  ast (2.4.0)
12
+ coderay (1.1.2)
12
13
  diff-lcs (1.3)
13
14
  docile (1.3.1)
14
15
  jaro_winkler (1.5.2)
15
16
  json (2.1.0)
17
+ method_source (0.9.0)
16
18
  parallel (1.13.0)
17
19
  parser (2.6.0.0)
18
20
  ast (~> 2.4.0)
19
21
  powerpack (0.1.2)
22
+ pry (0.11.3)
23
+ coderay (~> 1.1.0)
24
+ method_source (~> 0.9.0)
20
25
  rainbow (3.0.0)
21
26
  rake (10.5.0)
22
27
  require_all (2.0.0)
@@ -47,7 +52,7 @@ GEM
47
52
  json (>= 1.8, < 3)
48
53
  simplecov-html (~> 0.10.0)
49
54
  simplecov-html (0.10.2)
50
- thor (0.20.3)
55
+ thor (0.20.0)
51
56
  unicode-display_width (1.4.1)
52
57
 
53
58
  PLATFORMS
@@ -56,6 +61,7 @@ PLATFORMS
56
61
  DEPENDENCIES
57
62
  bundler (~> 2.0)
58
63
  c4!
64
+ pry
59
65
  rake (~> 10.0)
60
66
  rspec (~> 3.0)
61
67
  rubocop (~> 0.63)
data/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # C4
2
2
 
3
- C4 is a CLI connect four game.
3
+ [![Gem Version](https://badge.fury.io/rb/c4.svg)](https://badge.fury.io/rb/c4)
4
+
5
+ C4 is a CLI connect-four game.
4
6
 
5
7
  ## Installation
6
8
 
data/c4.gemspec CHANGED
@@ -40,6 +40,7 @@ Gem::Specification.new do |spec|
40
40
  spec.add_development_dependency 'rspec', '~> 3.0'
41
41
  spec.add_development_dependency 'rubocop', '~> 0.63'
42
42
  spec.add_development_dependency 'simplecov', '~> 0.16'
43
+ spec.add_development_dependency 'pry'
43
44
 
44
45
  spec.add_runtime_dependency 'require_all', '~> 2.0'
45
46
  spec.add_runtime_dependency 'thor', '~> 0.20'
data/lib/c4.rb CHANGED
@@ -27,14 +27,21 @@ module C4
27
27
  def start
28
28
  until game.winner || game.impasse?
29
29
  viewer.print
30
- puts 'Choose column: '
31
- column = STDIN.gets.chomp.to_i
32
- game.play!(column)
30
+ play
33
31
  end
34
32
 
35
33
  viewer.print
36
34
  end
37
35
 
36
+ def play
37
+ puts "Player \"#{game.current_player}\" - choose column (type a number from 0 to #{C4::Model::Game::COLUMNS}): "
38
+ column = STDIN.gets.chomp.to_i
39
+ game.play!(column)
40
+ rescue C4::Model::ColumnFullError, C4::Model::InvalidColumnError => e
41
+ puts e.message
42
+ retry
43
+ end
44
+
38
45
  def finish
39
46
  return puts("Player \"#{game.winner}\" won!") if game.winner
40
47
  return puts('No more moves possible. Game over.') if game.impasse?
@@ -1,12 +1,15 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'column'
4
+ require_relative 'matrix'
4
5
 
5
6
  module C4
6
7
  module Model
7
8
  class Board
8
9
  attr_reader :columns, :num_rows, :num_columns
9
10
 
11
+ using Refinement::Filler
12
+
10
13
  def initialize(num_rows, num_columns)
11
14
  @num_rows = num_rows
12
15
  @num_columns = num_columns
@@ -14,6 +17,8 @@ module C4
14
17
  end
15
18
 
16
19
  def put_stone!(column, mark)
20
+ raise InvalidColumnError, 'Column does not exist!' if column.negative? || column >= columns.size
21
+
17
22
  columns[column].put!(mark)
18
23
  end
19
24
 
@@ -22,30 +27,16 @@ module C4
22
27
  end
23
28
 
24
29
  def to_a(mode = :column_wise)
25
- column_wise = columns.map(&:to_a)
26
- return transpose if mode == :row_wise # Changes the shape
27
- return transpose.reverse if mode == :print
28
-
29
- column_wise
30
- end
30
+ column_wise = Matrix.new(columns.map(&:to_a), num_rows, num_columns)
31
+ return column_wise.transpose.matrix if mode == :row_wise # Changes the shape
32
+ return column_wise.transpose.reverse.matrix if mode == :print
31
33
 
32
- def transpose
33
- board_array = to_a(:column_wise)
34
- Array.new(num_rows).each_with_index.map do |_, row|
35
- Array.new(num_columns).each_with_index.map do |_, column|
36
- board_array[column][row]
37
- end
38
- end
34
+ column_wise.matrix
39
35
  end
40
36
 
41
37
  def diagonals
42
- []
43
- end
44
-
45
- def diagonal(matrix)
46
- Array.new(matrix.size).each_with_index.map do |_, i|
47
- matrix[i][i]
48
- end
38
+ matrix = Matrix.new(columns.map(&:to_a), num_rows, num_columns)
39
+ matrix.diagonals
49
40
  end
50
41
  end
51
42
  end
@@ -16,7 +16,7 @@ module C4
16
16
  end
17
17
 
18
18
  def put!(mark)
19
- raise ColumnFullError if full?
19
+ raise ColumnFullError, 'This column is full!' if full?
20
20
 
21
21
  stones.push(mark)
22
22
  end
@@ -3,5 +3,6 @@
3
3
  module C4
4
4
  module Model
5
5
  class ColumnFullError < StandardError; end
6
+ class InvalidColumnError < StandardError; end
6
7
  end
7
8
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'refinement/filler'
4
+
5
+ module C4
6
+ module Model
7
+ class Matrix
8
+ using Refinement::Filler
9
+
10
+ attr_reader :matrix, :rows, :columns
11
+
12
+ def initialize(matrix, rows, columns)
13
+ @matrix = matrix
14
+ @rows = rows
15
+ @columns = columns
16
+ end
17
+
18
+ def transpose
19
+ self.class.new(
20
+ Array.new(rows).each_with_index.map do |_, row|
21
+ Array.new(columns).each_with_index.map do |_, column|
22
+ matrix[column][row]
23
+ end
24
+ end,
25
+ columns,
26
+ rows
27
+ )
28
+ end
29
+
30
+ def size
31
+ [rows, columns].max
32
+ end
33
+
34
+ def partial_square(from, to)
35
+ self.class.new(matrix[from..to].map { |a| a[from..to] }, to - from + 1, to - from + 1)
36
+ end
37
+
38
+ # rubocop:disable Metrics/AbcSize
39
+ def square
40
+ sq = matrix.map do |col|
41
+ col[0, size].fill_up_to(size, nil)
42
+ end
43
+ sq[0, size].fill_up_to(size, Array.new(size))
44
+ self.class.new(sq, size, size)
45
+ end
46
+ # rubocop:enable Metrics/AbcSize
47
+
48
+ def diagonal
49
+ Array.new(size).each_with_index.map do |_, i|
50
+ matrix[i][i]
51
+ end
52
+ end
53
+
54
+ def reverse
55
+ self.class.new(matrix.reverse, rows, columns)
56
+ end
57
+
58
+ # rubocop:disable Metrics/AbcSize, Performance/TimesMap
59
+ def rotate
60
+ result = rows.times.map do |r|
61
+ columns.times.map do |c|
62
+ matrix[columns - c - 1][r]
63
+ end
64
+ end
65
+ self.class.new(result, columns, rows)
66
+ end
67
+ # rubocop:enable Metrics/AbcSize, Performance/TimesMap
68
+
69
+ def diagonals
70
+ quadrants.flat_map(&:quadrant_diagonals).uniq
71
+ end
72
+
73
+ def quadrants
74
+ [
75
+ square,
76
+ square.rotate,
77
+ square.rotate.rotate,
78
+ square.rotate.rotate.rotate
79
+ ]
80
+ end
81
+
82
+ def quadrant_diagonals
83
+ (0...size).map do |i|
84
+ partial_square(0, i).rotate.diagonal
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module C4
4
- VERSION = '0.1.5'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -28,7 +28,7 @@ module C4
28
28
  def printable_board
29
29
  board.map do |lv|
30
30
  lv.map do |e|
31
- e.nil? ? '*' : e
31
+ e.nil? ? ' ' : e
32
32
  end
33
33
  end
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: c4
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akihiko Itoh
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.16'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: require_all
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +150,7 @@ files:
136
150
  - lib/c4/model/column.rb
137
151
  - lib/c4/model/errors.rb
138
152
  - lib/c4/model/game.rb
153
+ - lib/c4/model/matrix.rb
139
154
  - lib/c4/model/refinement/filler.rb
140
155
  - lib/c4/model/refinement/streak.rb
141
156
  - lib/c4/version.rb
@@ -162,7 +177,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
177
  - !ruby/object:Gem::Version
163
178
  version: '0'
164
179
  requirements: []
165
- rubygems_version: 3.0.2
180
+ rubyforge_project:
181
+ rubygems_version: 2.7.6
166
182
  signing_key:
167
183
  specification_version: 4
168
184
  summary: A command line connect four.