connect-four 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 38d746592356ecae0ddb71eb70b6473b34bb29b3
4
+ data.tar.gz: 4b852629b1af75a52026acc22f6f72eb6e25876a
5
+ SHA512:
6
+ metadata.gz: 091756df60a978fa614e5d994da029d1e7ad3c3b90ad363c27576c70bbc218be67693b2af4029e1f887dc1fba1604a6e25efee394aabdf08d05918afd620b3b2
7
+ data.tar.gz: 2c47c19e38efe303538d2549ca84e2e08cbcb2ed3fe8699121aa1c0e8b62ca905efe47e0e831166ee3eef1622c4710240cd8bd5aeceb0180f789195609338492
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'colorize'
@@ -0,0 +1,10 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ colorize (0.7.7)
5
+
6
+ PLATFORMS
7
+ ruby
8
+
9
+ DEPENDENCIES
10
+ colorize
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Blaine Schmeisser
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,5 @@
1
+ task default: %w[run]
2
+
3
+ task :run do
4
+ ruby "bin/connect-four"
5
+ end
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/connect-four.rb'
4
+
5
+ board = Board.new
6
+ while board.draw and !board.won?
7
+ print "What would #{board.current_player} like to do: "
8
+ unless board.place(gets.chomp)
9
+ puts 'Invalid input.'
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
12
+
13
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
14
+
15
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.1.0, available at [http://contributor-covenant.org/version/1/1/0/](http://contributor-covenant.org/version/1/1/0/)
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.authors = 'Blaine Schmeisser'
3
+ s.name = 'connect-four'
4
+ s.email = 'blainesch@gmail.com'
5
+ s.homepage = 'https://github.com/blainesch/connect-four'
6
+ s.version = '0.0.1'
7
+ s.summary = 'Command line connect four game.'
8
+ s.description = 'Play connect four over the command line with your friends.'
9
+ s.licenses = ['MIT']
10
+ s.require_paths = ['lib', 'bin']
11
+ s.files = `git ls-files`.split("\n")
12
+ s.has_rdoc = false
13
+ s.executables << 'connect-four'
14
+
15
+ s.add_runtime_dependency 'colorize', '0.7.7'
16
+ end
@@ -0,0 +1,81 @@
1
+ require 'colorize'
2
+
3
+ class Board
4
+ attr_accessor :pieces
5
+
6
+ COLS = 7
7
+ ROWS = 6
8
+
9
+ def initialize
10
+ @pieces = ([nil] * COLS).map { [nil] * ROWS }
11
+ end
12
+
13
+ def draw
14
+ ROWS.times do |i|
15
+ row(ROWS - 1 - i).each do |char|
16
+ print "\u25A0 ".colorize(char || :light_black)
17
+ end
18
+ print "\n"
19
+ end
20
+ end
21
+
22
+ def moves
23
+ pieces.flatten.compact.length
24
+ end
25
+
26
+ def won?
27
+ ROWS.times.find do |r|
28
+ COLS.times.find do |c|
29
+ point_won?(r, c)
30
+ end
31
+ end
32
+ end
33
+
34
+ def point_won?(row, column)
35
+ possible_wins(row, column).find do |items|
36
+ items.compact.size == 4 and items.uniq.length == 1
37
+ end
38
+ end
39
+
40
+ def possible_wins(row, column)
41
+ directions = {n: [-1, 0], w: [0, -1], nw: [-1, -1], ne: [-1, 1]}
42
+ directions.keys.map do |direction|
43
+ relative_direction = directions[direction]
44
+ (0..3).map do |i|
45
+ x = row + (relative_direction[0] * i)
46
+ y = column + (relative_direction[1] * i)
47
+ pieces[x][y] if x >= 0 and y >= 0
48
+ end
49
+ end
50
+ end
51
+
52
+ def red?
53
+ moves % 2 == 0
54
+ end
55
+
56
+ def current_color
57
+ red? ? :red : :yellow
58
+ end
59
+
60
+ def current_player
61
+ current_color.to_s.colorize(current_color)
62
+ end
63
+
64
+ def place(col)
65
+ if col.match(/\A[^\d]/)
66
+ false
67
+ else
68
+ col = col.to_i
69
+ id = column(col).each_with_index.find { |el, i| el.nil? }[1]
70
+ column(col)[id] = current_color
71
+ end
72
+ end
73
+
74
+ def row(id)
75
+ pieces.collect { |el| el[id] }
76
+ end
77
+
78
+ def column(id)
79
+ pieces[id]
80
+ end
81
+ end
@@ -0,0 +1,22 @@
1
+ ## Connect 4
2
+
3
+ ### Running
4
+
5
+ ~~~
6
+ bundle install
7
+ rake
8
+ ~~~
9
+
10
+ Without rake
11
+
12
+ ~~~
13
+ bundle install
14
+ ./bin/connect-four
15
+ ~~~
16
+
17
+ ### Remote Play
18
+
19
+ Use [tmate](https://github.com/nviennot/tmate).
20
+
21
+ ### Screenshot
22
+ ![Screenshot](screenshot.png)
Binary file
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: connect-four
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Blaine Schmeisser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.7.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.7.7
27
+ description: Play connect four over the command line with your friends.
28
+ email: blainesch@gmail.com
29
+ executables:
30
+ - connect-four
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE
37
+ - Rakefile
38
+ - bin/connect-four
39
+ - code_of_conduct.md
40
+ - connect-four.gemspec
41
+ - lib/connect-four.rb
42
+ - readme.md
43
+ - screenshot.png
44
+ homepage: https://github.com/blainesch/connect-four
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ - bin
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.4.6
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Command line connect four game.
69
+ test_files: []