four_in_a_row 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4ab9333288e18d8d983d35bd4b3bfe1ce362a130
4
+ data.tar.gz: a006bdbb319f7334706ae05a61851279b760e743
5
+ SHA512:
6
+ metadata.gz: c64ab9ba23a310ded283f98429228a9c5815baf9db90fc55dae46d6992a6104b6d8565784e8409690f3630a5d8501bf6675882af26035032df2c8ee89f55c68f
7
+ data.tar.gz: a7652df1514924f2465942a8c096c3035c1abe06ee719a99fa6aa7a3227b51b43616906b86c41e0c75f08aa39d33750b3be1f3a34ce948289e653b57538456fb
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in four_in_a_row.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # FourInARow
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/four_in_a_row`. 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
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'four_in_a_row'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install four_in_a_row
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/four_in_a_row.
36
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "four_in_a_row"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/four_in_a_row ADDED
@@ -0,0 +1,4 @@
1
+ require 'four_in_a_row'
2
+
3
+ game = ConnectFour.new
4
+ game.play
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'four_in_a_row/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "four_in_a_row"
8
+ spec.version = FourInARow::VERSION
9
+ spec.authors = ["Thomas Lo", "Ellen Sun"]
10
+ spec.email = ["wagaiznogoud@gmail.com"]
11
+
12
+ spec.summary = %q{Simple Connect 4 game}
13
+ spec.description = %q{Have 4 connecting pieces to win!}
14
+ spec.homepage = "https://github.com/thomasjinlo/four_in_a_row.git"
15
+
16
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
+ # delete this section to allow pushing this gem to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.11"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
@@ -0,0 +1,8 @@
1
+ require_relative "four_in_a_row/version"
2
+ require_relative "four_in_a_row/connect_four"
3
+ require_relative "four_in_a_row/player"
4
+ require_relative "four_in_a_row/board"
5
+
6
+ module FourInARow
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,119 @@
1
+ class Board
2
+ attr_accessor :grid
3
+
4
+ # grid is in rows and columns, beginning of a column is the "top" of the board
5
+
6
+ def initialize
7
+ @grid = []
8
+ (0..5).each do |row|
9
+ @grid[row] = []
10
+ (0..6).each do |col|
11
+ @grid[row][col] = "_ "
12
+ end
13
+ end
14
+ end
15
+
16
+ def column_full?(col)
17
+ @grid[5][col] != "_ "
18
+ end
19
+
20
+ def full?
21
+ (0..6).all? {|idx| column_full?(idx) == true }
22
+ end
23
+
24
+ def add_piece(piece, col)
25
+ (0..6).each do |row|
26
+ if @grid[row][col] == "_ "
27
+ @grid[row][col] = "#{piece} "
28
+ break
29
+ end
30
+ end
31
+ end
32
+
33
+ def render
34
+ print "="*13
35
+ 5.downto(0).each do |row|
36
+ puts
37
+ (0..6).each do |cols|
38
+ print @grid[row][cols]
39
+ end
40
+ end
41
+ puts "\n"
42
+ puts "="*13
43
+ end
44
+
45
+ def winning_combination?(sym)
46
+ winning_diagonal?(sym) ||
47
+ winning_horizontal?(sym) ||
48
+ winning_vertical?(sym)
49
+ end
50
+
51
+ def four_in_row?(sym, possible_solutions)
52
+ solution = "#{sym} "*4
53
+ possible_solutions.each do |group_of_four|
54
+ if group_of_four == solution
55
+ return true
56
+ end
57
+ end
58
+ false
59
+ end
60
+
61
+ def winning_diagonal?(sym)
62
+ possible_solutions = diagonals
63
+ four_in_row?(sym, possible_solutions)
64
+ end
65
+
66
+ def winning_horizontal?(sym)
67
+ possible_solutions = horizontals
68
+ four_in_row?(sym, possible_solutions)
69
+ end
70
+
71
+ def winning_vertical?(sym)
72
+ possible_solutions = verticals
73
+ four_in_row?(sym, possible_solutions)
74
+ end
75
+
76
+ def diagonals
77
+ all_diags = up_down_diag + down_up_diag
78
+ end
79
+
80
+ def up_down_diag
81
+ diag = []
82
+ (0..3).each do |col|
83
+ 5.downto(3).each do |row|
84
+ diag << @grid[row][col] + @grid[row-1][col+1] + @grid[row-2][col+2] + @grid[row-3][col+3]
85
+ end
86
+ end
87
+ diag
88
+ end
89
+
90
+ def down_up_diag
91
+ diag = []
92
+ (0..3).each do |col|
93
+ (0..2).each do |row|
94
+ diag << @grid[row][col] + @grid[row+1][col+1] + @grid[row+2][col+2] + @grid[row+3][col+3]
95
+ end
96
+ end
97
+ diag
98
+ end
99
+
100
+ def verticals
101
+ vert = []
102
+ (0..6).each do |col|
103
+ (0..2).each do |row|
104
+ vert << @grid[row][col] + @grid[row+1][col] + @grid[row+2][col] + @grid[row+3][col]
105
+ end
106
+ end
107
+ vert
108
+ end
109
+
110
+ def horizontals
111
+ horiz = []
112
+ @grid.each do |row|
113
+ (0..3).each do |start|
114
+ horiz << row[start] + row[start+1] + row[start+2] + row[start+3]
115
+ end
116
+ end
117
+ horiz
118
+ end
119
+ end
@@ -0,0 +1,80 @@
1
+ class ConnectFour
2
+
3
+ def initialize
4
+ @board = Board.new
5
+ @player1 = Player.new("Player1")
6
+ @player2 = Player.new("Player2")
7
+ @turn = @player1
8
+ @game_status = false
9
+ end
10
+
11
+ def welcome
12
+ puts "********************************************************"
13
+ puts "Welcome to Connect Four!".center(55)
14
+ puts "Player1 please choose your piece.".center(55)
15
+ puts "Piece available: 'x' or 'o'".center(55)
16
+ puts "********************************************************"
17
+ set_symbol
18
+ end
19
+
20
+ def set_symbol
21
+ @turn.choose_symbol
22
+ @player1.symbol == "X" ? @player2.symbol = "O" : @player2.symbol = "X"
23
+ end
24
+
25
+ def play
26
+ welcome
27
+ until @game_status
28
+ get_input
29
+ make_move
30
+ @board.render
31
+ check_game_over
32
+ switch_players
33
+ end
34
+ end
35
+
36
+ def get_input
37
+ @turn.choose_column
38
+ valid_input?(@turn.column)
39
+ end
40
+
41
+ def make_move
42
+ @board.add_piece(@turn.symbol, @turn.column)
43
+ end
44
+
45
+ def valid_input?(column)
46
+ if @board.column_full?(column)
47
+ puts "That column is full, try again"
48
+ get_input
49
+ end
50
+ end
51
+
52
+ def switch_players
53
+ if @turn == @player1
54
+ @turn = @player2
55
+ else
56
+ @turn = @player1
57
+ end
58
+ end
59
+
60
+ def check_game_over
61
+ check_victory
62
+ check_draw
63
+ end
64
+
65
+ # check if winning combination exists
66
+ def check_victory
67
+ if @board.winning_combination?(@turn.symbol)
68
+ winner = @turn.name
69
+ puts "You won #{winner}!"
70
+ @game_status = true
71
+ end
72
+ end
73
+
74
+ def check_draw
75
+ if @board.full?
76
+ puts "It's a draw."
77
+ @game_status = true
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,20 @@
1
+ class Player
2
+ attr_accessor :symbol, :column
3
+ attr_reader :name
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ end
8
+
9
+ def choose_symbol
10
+ @symbol = gets.chomp.upcase
11
+ choose_symbol unless ["X", "O"].include?(@symbol)
12
+ end
13
+
14
+ def choose_column
15
+ puts "It's #{@name}'s turn, your piece is #{@symbol}"
16
+ puts "Which column do you want to place your piece? (1-7)"
17
+ @column = gets.chomp.to_i - 1
18
+ choose_column unless (0..6).include?(@column)
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module FourInARow
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: four_in_a_row
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Lo
8
+ - Ellen Sun
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.11'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.11'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ description: Have 4 connecting pieces to win!
43
+ email:
44
+ - wagaiznogoud@gmail.com
45
+ executables:
46
+ - four_in_a_row
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - exe/four_in_a_row
57
+ - four_in_a_row.gemspec
58
+ - lib/four_in_a_row.rb
59
+ - lib/four_in_a_row/board.rb
60
+ - lib/four_in_a_row/connect_four.rb
61
+ - lib/four_in_a_row/player.rb
62
+ - lib/four_in_a_row/version.rb
63
+ homepage: https://github.com/thomasjinlo/four_in_a_row.git
64
+ licenses: []
65
+ metadata:
66
+ allowed_push_host: https://rubygems.org
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 2.4.8
84
+ signing_key:
85
+ specification_version: 4
86
+ summary: Simple Connect 4 game
87
+ test_files: []