c4 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +12 -0
- data/.gitignore +152 -0
- data/.gitlab-ci.yml +27 -0
- data/.rspec +3 -0
- data/.rubocop.yml +11 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +65 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +8 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/c4.gemspec +46 -0
- data/exe/c4 +15 -0
- data/lib/c4/model/board.rb +52 -0
- data/lib/c4/model/column.rb +33 -0
- data/lib/c4/model/errors.rb +7 -0
- data/lib/c4/model/game.rb +74 -0
- data/lib/c4/model/refinement/filler.rb +18 -0
- data/lib/c4/model/refinement/streak.rb +21 -0
- data/lib/c4/version.rb +5 -0
- data/lib/c4/view/cli.rb +51 -0
- data/lib/c4.rb +43 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1c3a2d612d390d06ba53ed7fb0badaacee6e8e09fbbb8de320235f14861b8787
|
4
|
+
data.tar.gz: 7426aa0b5dec7010da27d1d872de27030b09c5778e978f10e43feb4db2db6207
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 402e4f7c1c06396260e2d2942c680328bc01009987195e1e44ad30d9c7e0b2b05b6182482395649d02f3b40fbcae51ed3f64dc1faa273193ccc5bb6a7400e5c4
|
7
|
+
data.tar.gz: 835dfd5a1d736070a0b51f23cfce5113b13bc18f2c21d0b3fad2baafcecbc10870f0d34ccbb403dc9fb8187eb1c4fb1de50d27f902680c932560bb9a15ad3bcf
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
# Created by https://www.gitignore.io/api/git,ruby,rubymine
|
2
|
+
# Edit at https://www.gitignore.io/?templates=git,ruby,rubymine
|
3
|
+
|
4
|
+
### Git ###
|
5
|
+
# Created by git for backups. To disable backups in Git:
|
6
|
+
# $ git config --global mergetool.keepBackup false
|
7
|
+
*.orig
|
8
|
+
|
9
|
+
# Created by git when using merge tools for conflicts
|
10
|
+
*.BACKUP.*
|
11
|
+
*.BASE.*
|
12
|
+
*.LOCAL.*
|
13
|
+
*.REMOTE.*
|
14
|
+
*_BACKUP_*.txt
|
15
|
+
*_BASE_*.txt
|
16
|
+
*_LOCAL_*.txt
|
17
|
+
*_REMOTE_*.txt
|
18
|
+
|
19
|
+
### Ruby ###
|
20
|
+
*.gem
|
21
|
+
*.rbc
|
22
|
+
/.config
|
23
|
+
/coverage/
|
24
|
+
/InstalledFiles
|
25
|
+
/pkg/
|
26
|
+
/spec/reports/
|
27
|
+
/spec/examples.txt
|
28
|
+
/test/tmp/
|
29
|
+
/test/version_tmp/
|
30
|
+
/tmp/
|
31
|
+
|
32
|
+
.rspec_status
|
33
|
+
|
34
|
+
# Used by dotenv library to load environment variables.
|
35
|
+
# .env
|
36
|
+
|
37
|
+
## Specific to RubyMotion:
|
38
|
+
.dat*
|
39
|
+
.repl_history
|
40
|
+
build/
|
41
|
+
*.bridgesupport
|
42
|
+
build-iPhoneOS/
|
43
|
+
build-iPhoneSimulator/
|
44
|
+
|
45
|
+
## Specific to RubyMotion (use of CocoaPods):
|
46
|
+
#
|
47
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
48
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
49
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
50
|
+
#
|
51
|
+
# vendor/Pods/
|
52
|
+
|
53
|
+
## Documentation cache and generated files:
|
54
|
+
/.yardoc/
|
55
|
+
/_yardoc/
|
56
|
+
/doc/
|
57
|
+
/rdoc/
|
58
|
+
|
59
|
+
## Environment normalization:
|
60
|
+
/.bundle/
|
61
|
+
/vendor/bundle
|
62
|
+
/lib/bundler/man/
|
63
|
+
|
64
|
+
# for a library or gem, you might want to ignore these files since the code is
|
65
|
+
# intended to run in multiple environments; otherwise, check them in:
|
66
|
+
# Gemfile.lock
|
67
|
+
# .ruby-version
|
68
|
+
# .ruby-gemset
|
69
|
+
|
70
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
71
|
+
.rvmrc
|
72
|
+
|
73
|
+
### RubyMine ###
|
74
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
|
75
|
+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
76
|
+
|
77
|
+
# User-specific stuff
|
78
|
+
.idea/**/workspace.xml
|
79
|
+
.idea/**/tasks.xml
|
80
|
+
.idea/**/usage.statistics.xml
|
81
|
+
.idea/**/dictionaries
|
82
|
+
.idea/**/shelf
|
83
|
+
|
84
|
+
# Generated files
|
85
|
+
.idea/**/contentModel.xml
|
86
|
+
|
87
|
+
# Sensitive or high-churn files
|
88
|
+
.idea/**/dataSources/
|
89
|
+
.idea/**/dataSources.ids
|
90
|
+
.idea/**/dataSources.local.xml
|
91
|
+
.idea/**/sqlDataSources.xml
|
92
|
+
.idea/**/dynamic.xml
|
93
|
+
.idea/**/uiDesigner.xml
|
94
|
+
.idea/**/dbnavigator.xml
|
95
|
+
|
96
|
+
# Gradle
|
97
|
+
.idea/**/gradle.xml
|
98
|
+
.idea/**/libraries
|
99
|
+
|
100
|
+
# Gradle and Maven with auto-import
|
101
|
+
# When using Gradle or Maven with auto-import, you should exclude module files,
|
102
|
+
# since they will be recreated, and may cause churn. Uncomment if using
|
103
|
+
# auto-import.
|
104
|
+
# .idea/modules.xml
|
105
|
+
# .idea/*.iml
|
106
|
+
# .idea/modules
|
107
|
+
|
108
|
+
# CMake
|
109
|
+
cmake-build-*/
|
110
|
+
|
111
|
+
# Mongo Explorer plugin
|
112
|
+
.idea/**/mongoSettings.xml
|
113
|
+
|
114
|
+
# File-based project format
|
115
|
+
*.iws
|
116
|
+
|
117
|
+
# IntelliJ
|
118
|
+
out/
|
119
|
+
|
120
|
+
# mpeltonen/sbt-idea plugin
|
121
|
+
.idea_modules/
|
122
|
+
|
123
|
+
# JIRA plugin
|
124
|
+
atlassian-ide-plugin.xml
|
125
|
+
|
126
|
+
# Cursive Clojure plugin
|
127
|
+
.idea/replstate.xml
|
128
|
+
|
129
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
130
|
+
com_crashlytics_export_strings.xml
|
131
|
+
crashlytics.properties
|
132
|
+
crashlytics-build.properties
|
133
|
+
fabric.properties
|
134
|
+
|
135
|
+
# Editor-based Rest Client
|
136
|
+
.idea/httpRequests
|
137
|
+
|
138
|
+
# Android studio 3.1+ serialized cache file
|
139
|
+
.idea/caches/build_file_checksums.ser
|
140
|
+
|
141
|
+
### RubyMine Patch ###
|
142
|
+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
143
|
+
|
144
|
+
# *.iml
|
145
|
+
# modules.xml
|
146
|
+
# .idea/misc.xml
|
147
|
+
# *.ipr
|
148
|
+
|
149
|
+
# Sonarlint plugin
|
150
|
+
.idea/sonarlint
|
151
|
+
|
152
|
+
# End of https://www.gitignore.io/api/git,ruby,rubymine
|
data/.gitlab-ci.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
image: "ruby:2.5"
|
2
|
+
|
3
|
+
stages:
|
4
|
+
- test
|
5
|
+
- build
|
6
|
+
- deploy
|
7
|
+
|
8
|
+
before_script:
|
9
|
+
- ruby -v
|
10
|
+
- gem update --system
|
11
|
+
- gem install bundler
|
12
|
+
- bundle install
|
13
|
+
|
14
|
+
rubocop:
|
15
|
+
stage: test
|
16
|
+
script:
|
17
|
+
- bundle exec rubocop
|
18
|
+
|
19
|
+
rspec:
|
20
|
+
stage: test
|
21
|
+
script:
|
22
|
+
- bundle exec rspec
|
23
|
+
|
24
|
+
build:
|
25
|
+
stage: build
|
26
|
+
script:
|
27
|
+
- gem build c4.gemspec
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.1
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
c4 (0.1.0)
|
5
|
+
require_all (~> 2.0)
|
6
|
+
thor (~> 0.20)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.0)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
docile (1.3.1)
|
14
|
+
jaro_winkler (1.5.2)
|
15
|
+
json (2.1.0)
|
16
|
+
parallel (1.13.0)
|
17
|
+
parser (2.6.0.0)
|
18
|
+
ast (~> 2.4.0)
|
19
|
+
powerpack (0.1.2)
|
20
|
+
rainbow (3.0.0)
|
21
|
+
rake (10.5.0)
|
22
|
+
require_all (2.0.0)
|
23
|
+
rspec (3.8.0)
|
24
|
+
rspec-core (~> 3.8.0)
|
25
|
+
rspec-expectations (~> 3.8.0)
|
26
|
+
rspec-mocks (~> 3.8.0)
|
27
|
+
rspec-core (3.8.0)
|
28
|
+
rspec-support (~> 3.8.0)
|
29
|
+
rspec-expectations (3.8.2)
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
31
|
+
rspec-support (~> 3.8.0)
|
32
|
+
rspec-mocks (3.8.0)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.8.0)
|
35
|
+
rspec-support (3.8.0)
|
36
|
+
rubocop (0.63.1)
|
37
|
+
jaro_winkler (~> 1.5.1)
|
38
|
+
parallel (~> 1.10)
|
39
|
+
parser (>= 2.5, != 2.5.1.1)
|
40
|
+
powerpack (~> 0.1)
|
41
|
+
rainbow (>= 2.2.2, < 4.0)
|
42
|
+
ruby-progressbar (~> 1.7)
|
43
|
+
unicode-display_width (~> 1.4.0)
|
44
|
+
ruby-progressbar (1.10.0)
|
45
|
+
simplecov (0.16.1)
|
46
|
+
docile (~> 1.1)
|
47
|
+
json (>= 1.8, < 3)
|
48
|
+
simplecov-html (~> 0.10.0)
|
49
|
+
simplecov-html (0.10.2)
|
50
|
+
thor (0.20.0)
|
51
|
+
unicode-display_width (1.4.1)
|
52
|
+
|
53
|
+
PLATFORMS
|
54
|
+
ruby
|
55
|
+
|
56
|
+
DEPENDENCIES
|
57
|
+
bundler (~> 2.0)
|
58
|
+
c4!
|
59
|
+
rake (~> 10.0)
|
60
|
+
rspec (~> 3.0)
|
61
|
+
rubocop (~> 0.63)
|
62
|
+
simplecov (~> 0.16)
|
63
|
+
|
64
|
+
BUNDLED WITH
|
65
|
+
2.0.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Akihiko Itoh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# C4
|
2
|
+
|
3
|
+
C4 is a CLI connect four game.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'c4'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install c4
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To start playing:
|
24
|
+
|
25
|
+
$ c4 start
|
26
|
+
|
27
|
+
Alternatively, in the project's root directory:
|
28
|
+
|
29
|
+
$ ./exec/c4 start
|
30
|
+
|
31
|
+
## Development
|
32
|
+
|
33
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
34
|
+
|
35
|
+
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).
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/c4.
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'c4'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/c4.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'c4/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'c4'
|
9
|
+
spec.version = C4::VERSION
|
10
|
+
spec.authors = ['Akihiko Itoh']
|
11
|
+
spec.email = ['akihiko.mus@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'A command line connect four.'
|
14
|
+
spec.homepage = 'https://gitlab.com/AkihikoITOH/connect-four'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org/'
|
21
|
+
|
22
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
23
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
24
|
+
else
|
25
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
26
|
+
'public gem pushes.'
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = 'exe'
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ['lib']
|
37
|
+
|
38
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
39
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
40
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
41
|
+
spec.add_development_dependency 'rubocop', '~> 0.63'
|
42
|
+
spec.add_development_dependency 'simplecov', '~> 0.16'
|
43
|
+
|
44
|
+
spec.add_runtime_dependency 'require_all', '~> 2.0'
|
45
|
+
spec.add_runtime_dependency 'thor', '~> 0.20'
|
46
|
+
end
|
data/exe/c4
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'column'
|
4
|
+
|
5
|
+
module C4
|
6
|
+
module Model
|
7
|
+
class Board
|
8
|
+
attr_reader :columns, :num_rows, :num_columns
|
9
|
+
|
10
|
+
def initialize(num_rows, num_columns)
|
11
|
+
@num_rows = num_rows
|
12
|
+
@num_columns = num_columns
|
13
|
+
@columns = Array.new(num_columns).map { |_| Column.new(num_rows) }
|
14
|
+
end
|
15
|
+
|
16
|
+
def put_stone!(column, mark)
|
17
|
+
columns[column].put!(mark)
|
18
|
+
end
|
19
|
+
|
20
|
+
def full?
|
21
|
+
columns.all?(&:full?)
|
22
|
+
end
|
23
|
+
|
24
|
+
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
|
31
|
+
|
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
|
39
|
+
end
|
40
|
+
|
41
|
+
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
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'errors'
|
4
|
+
require_relative 'refinement/filler'
|
5
|
+
|
6
|
+
module C4
|
7
|
+
module Model
|
8
|
+
class Column
|
9
|
+
using Refinement::Filler
|
10
|
+
|
11
|
+
attr_reader :stones, :max_size
|
12
|
+
|
13
|
+
def initialize(max_size)
|
14
|
+
@stones = []
|
15
|
+
@max_size = max_size
|
16
|
+
end
|
17
|
+
|
18
|
+
def put!(mark)
|
19
|
+
raise ColumnFullError if full?
|
20
|
+
|
21
|
+
stones.push(mark)
|
22
|
+
end
|
23
|
+
|
24
|
+
def full?
|
25
|
+
stones.size >= max_size
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_a
|
29
|
+
stones.fill_up_to(max_size, nil)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'board'
|
4
|
+
require_relative 'errors'
|
5
|
+
require_relative 'refinement/streak'
|
6
|
+
|
7
|
+
module C4
|
8
|
+
module Model
|
9
|
+
class Game
|
10
|
+
using Refinement::Streak
|
11
|
+
|
12
|
+
attr_reader :board, :players, :current_player_index
|
13
|
+
|
14
|
+
P1 = 'o'
|
15
|
+
P2 = 'x'
|
16
|
+
ROWS = 6
|
17
|
+
COLUMNS = 7
|
18
|
+
WINNING_LENGTH = 4
|
19
|
+
|
20
|
+
def initialize
|
21
|
+
@board = Board.new(ROWS, COLUMNS)
|
22
|
+
@players = [P1, P2]
|
23
|
+
@current_player_index = 0
|
24
|
+
end
|
25
|
+
|
26
|
+
def play!(column)
|
27
|
+
board.put_stone!(column, current_player)
|
28
|
+
toggle_player
|
29
|
+
end
|
30
|
+
|
31
|
+
def current_player
|
32
|
+
players[current_player_index]
|
33
|
+
end
|
34
|
+
|
35
|
+
def impasse?
|
36
|
+
board.full?
|
37
|
+
end
|
38
|
+
|
39
|
+
def winner
|
40
|
+
players.find do |player|
|
41
|
+
player if valid_streak_exists?(player)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def toggle_player
|
48
|
+
@current_player_index = (current_player_index + 1) % 2
|
49
|
+
end
|
50
|
+
|
51
|
+
def valid_streak_exists?(player)
|
52
|
+
vertical_streak_exists?(player) || horizontal_streak_exists?(player) || diagonal_streak_exists?(player)
|
53
|
+
end
|
54
|
+
|
55
|
+
def vertical_streak_exists?(player)
|
56
|
+
board.to_a(:column_wise).any? do |column|
|
57
|
+
column.streak?(player, WINNING_LENGTH)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def horizontal_streak_exists?(player)
|
62
|
+
board.to_a(:row_wise).any? do |row|
|
63
|
+
row.streak?(player, WINNING_LENGTH)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def diagonal_streak_exists?(player)
|
68
|
+
board.diagonals.any? do |row|
|
69
|
+
row.streak?(player, WINNING_LENGTH)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module C4
|
4
|
+
module Model
|
5
|
+
module Refinement
|
6
|
+
module Filler
|
7
|
+
refine Array do
|
8
|
+
# fill_up_to fills the array by appending the given value until the size reaches the disired size.
|
9
|
+
def fill_up_to(desired_size, value = nil)
|
10
|
+
return self if size >= desired_size
|
11
|
+
|
12
|
+
self + Array.new(desired_size - size, value)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module C4
|
4
|
+
module Model
|
5
|
+
module Refinement
|
6
|
+
module Streak
|
7
|
+
refine Array do
|
8
|
+
# streak? looks for a streak of the given mark of the given length.
|
9
|
+
# Returns true if it finds at least one streak.
|
10
|
+
def streak?(value, streak_length)
|
11
|
+
return false if streak_length <= 0 || streak_length > length
|
12
|
+
|
13
|
+
(0..(length - streak_length)).any? do |idx|
|
14
|
+
self[idx, streak_length].all? { |element| element == value }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/c4/version.rb
ADDED
data/lib/c4/view/cli.rb
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module C4
|
4
|
+
module View
|
5
|
+
class Cli
|
6
|
+
attr_reader :game
|
7
|
+
|
8
|
+
def initialize(game)
|
9
|
+
@game = game
|
10
|
+
end
|
11
|
+
|
12
|
+
def print
|
13
|
+
puts text
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def text
|
19
|
+
[text_columns,
|
20
|
+
text_divider,
|
21
|
+
text_column_numbers].join("\n") + "\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
def board
|
25
|
+
game.board.to_a(:print)
|
26
|
+
end
|
27
|
+
|
28
|
+
def printable_board
|
29
|
+
board.map do |lv|
|
30
|
+
lv.map do |e|
|
31
|
+
e.nil? ? '*' : e
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def text_columns
|
37
|
+
printable_board.map do |lv|
|
38
|
+
"|#{lv.join('|')}|"
|
39
|
+
end.join("\n")
|
40
|
+
end
|
41
|
+
|
42
|
+
def text_divider
|
43
|
+
'-' * (printable_board.first.size * 2 + 1)
|
44
|
+
end
|
45
|
+
|
46
|
+
def text_column_numbers
|
47
|
+
"|#{(0...printable_board.first.size).to_a.join('|')}|"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/c4.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'require_all'
|
4
|
+
require 'singleton'
|
5
|
+
require_rel 'c4/model'
|
6
|
+
require_rel 'c4/view'
|
7
|
+
require_rel 'c4/version.rb'
|
8
|
+
|
9
|
+
module C4
|
10
|
+
class App
|
11
|
+
include Singleton
|
12
|
+
|
13
|
+
attr_reader :game, :viewer
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@game = C4::Model::Game.new
|
17
|
+
@viewer = C4::View::Cli.new(game)
|
18
|
+
end
|
19
|
+
|
20
|
+
def run
|
21
|
+
start
|
22
|
+
finish
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def start
|
28
|
+
until game.winner || game.impasse?
|
29
|
+
viewer.print
|
30
|
+
puts 'Choose column: '
|
31
|
+
column = STDIN.gets.chomp.to_i
|
32
|
+
game.play!(column)
|
33
|
+
end
|
34
|
+
|
35
|
+
viewer.print
|
36
|
+
end
|
37
|
+
|
38
|
+
def finish
|
39
|
+
return puts("Player \"#{game.winner}\" won!") if game.winner
|
40
|
+
return puts('No more moves possible. Game over.') if game.impasse?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,170 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: c4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Akihiko Itoh
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.63'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.63'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.16'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.16'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: require_all
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '2.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: thor
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.20'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.20'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- akihiko.mus@gmail.com
|
114
|
+
executables:
|
115
|
+
- c4
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".editorconfig"
|
120
|
+
- ".gitignore"
|
121
|
+
- ".gitlab-ci.yml"
|
122
|
+
- ".rspec"
|
123
|
+
- ".rubocop.yml"
|
124
|
+
- ".ruby-version"
|
125
|
+
- Gemfile
|
126
|
+
- Gemfile.lock
|
127
|
+
- LICENSE.txt
|
128
|
+
- README.md
|
129
|
+
- Rakefile
|
130
|
+
- bin/console
|
131
|
+
- bin/setup
|
132
|
+
- c4.gemspec
|
133
|
+
- exe/c4
|
134
|
+
- lib/c4.rb
|
135
|
+
- lib/c4/model/board.rb
|
136
|
+
- lib/c4/model/column.rb
|
137
|
+
- lib/c4/model/errors.rb
|
138
|
+
- lib/c4/model/game.rb
|
139
|
+
- lib/c4/model/refinement/filler.rb
|
140
|
+
- lib/c4/model/refinement/streak.rb
|
141
|
+
- lib/c4/version.rb
|
142
|
+
- lib/c4/view/cli.rb
|
143
|
+
homepage: https://gitlab.com/AkihikoITOH/connect-four
|
144
|
+
licenses:
|
145
|
+
- MIT
|
146
|
+
metadata:
|
147
|
+
allowed_push_host: https://rubygems.org/
|
148
|
+
homepage_uri: https://gitlab.com/AkihikoITOH/connect-four
|
149
|
+
source_code_uri: https://gitlab.com/AkihikoITOH/connect-four
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: '0'
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubyforge_project:
|
166
|
+
rubygems_version: 2.7.6
|
167
|
+
signing_key:
|
168
|
+
specification_version: 4
|
169
|
+
summary: A command line connect four.
|
170
|
+
test_files: []
|