cnnct4 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3f335181b60c748723b4d06e9513f5191b592bace4fa8e7cac6dcf6300cd5b33
4
+ data.tar.gz: 850a2db9a7d9622e3adfb123f6f55af959f026964780c2e8fcd4aad405d44cdb
5
+ SHA512:
6
+ metadata.gz: 3e71c0a8e1325e9886a37da63fb6ccb6285724d41e49659604a8b1f1f5f262df78e7ede5fc343930a45e147a407f7c909d1fdec0648ddf344656da1a3553175f
7
+ data.tar.gz: 01fb0039a18ab0f0099c9e9f67b742e63f02c5b6b663536263a277c0a0449ab7dab55436fc2241462d14a85ade3637feea80c832902924cfe145b459c5dd207a
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in cnnct4.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Reid Miller
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,35 @@
1
+ # Cnnct4
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ 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/cnnct4`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cnnct4.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/cnnct4.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/cnnct4/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cnnct4"
7
+ spec.version = Cnnct4::VERSION
8
+ spec.authors = ["Reid Miller"]
9
+ spec.email = ["reid.s.miller@gmail.com"]
10
+
11
+ spec.summary = "Thomas and Reid's Connect 4 game."
12
+ spec.description = "A CLI connect 4 game playable by 1 or 2 players"
13
+ spec.homepage = "https://github.com/reidsmiller/connect-4"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/reidsmiller/connect-4"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ # Uncomment to register a new dependency of your gem
32
+ # spec.add_dependency "example-gem", "~> 1.0"
33
+ spec.add_dependency "thor"
34
+ spec.add_development_dependency "rspec", "~> 3.2"
35
+
36
+ # For more information and examples about making a new gem, check out our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
data/exe/cnnct4 ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/cnnct4/cli'
3
+ Cnnct4::CLI.start
@@ -0,0 +1,37 @@
1
+ class Board
2
+ attr_reader :board_array
3
+
4
+ def initialize
5
+ @board_array = Array.new(6) { Array.new(7, ".")}
6
+ end
7
+
8
+ def render
9
+ puts " A B C D E F G"
10
+ @board_array.each do |row|
11
+ print " #{row.join(" ")}"
12
+ puts
13
+ end
14
+ end
15
+
16
+ # break out into a few different methods
17
+ def place(column, mark)
18
+ column.upcase!
19
+ if ("A".."G").member?(column)
20
+ column = column.ord - 65
21
+ row = 5
22
+ while row >= 0
23
+ if @board_array[row][column] == "."
24
+ @board_array[row][column] = mark
25
+ return true
26
+ else
27
+ row -= 1
28
+ end
29
+ end
30
+ end
31
+ false
32
+ end
33
+
34
+ def reset
35
+ @board_array = Array.new(6) { Array.new(7, ".")}
36
+ end
37
+ end
data/lib/cnnct4/cli.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'thor'
2
+ require 'cnnct4'
3
+
4
+ module Cnnct4
5
+ class CLI < Thor
6
+ desc "Start Game", "Start CLI game in terminal"
7
+ def self.start
8
+ game = GamePlay.new
9
+ game.game_menu
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,63 @@
1
+ class Game
2
+ attr_reader :board
3
+ attr_accessor :game_win, :game_draw
4
+
5
+ def initialize
6
+ @board = Board.new
7
+ @game_win = nil
8
+ @game_draw = false
9
+
10
+ end
11
+
12
+ def win?
13
+ check_for_win(@board.board_array)
14
+ check_for_win(vertical_sort)
15
+ check_for_win(diagonal_up_sort)
16
+ check_for_win(diagonal_down_sort)
17
+ check_for_draw
18
+ end
19
+
20
+ def check_for_win(array)
21
+ array.each do |row|
22
+ row.each_cons(4) do |group|
23
+ if group == ["X", "X", "X", "X"]
24
+ @game_win = true
25
+ elsif group == ["O", "O", "O", "O"]
26
+ @game_win = false
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ def check_for_draw
33
+ @game_draw = true if !@board.board_array.flatten.any?(/[.]/)
34
+ end
35
+
36
+ def vertical_sort
37
+ @board.board_array.transpose
38
+ end
39
+
40
+ # Look into .with_index
41
+ #possibly making both diagonal up and down one method with arguments
42
+ def diagonal_up_sort
43
+ padding = 5
44
+ padded_matrix = []
45
+ @board.board_array.each do |row|
46
+ inverse_padding = 5 - padding
47
+ padded_matrix << ([nil] * inverse_padding) + row + ([nil] * padding)
48
+ padding -= 1
49
+ end
50
+ padded_matrix.transpose.map {|array| array.compact}
51
+ end
52
+
53
+ def diagonal_down_sort
54
+ padding = 5
55
+ padded_matrix = []
56
+ @board.board_array.each do |row|
57
+ inverse_padding = 5 - padding
58
+ padded_matrix << ([nil] * padding) + row + ([nil] * inverse_padding)
59
+ padding -= 1
60
+ end
61
+ padded_matrix.transpose.map {|array| array.compact}
62
+ end
63
+ end
@@ -0,0 +1,122 @@
1
+ require_relative 'playable'
2
+
3
+ class GamePlay
4
+ include Playable
5
+
6
+ def initialize
7
+ @player1_name = nil
8
+ @player2_name = nil
9
+ @game = Game.new
10
+ @player_turns = []
11
+ @player2_turns = []
12
+ @comp_turns = []
13
+ @player_wins = 0
14
+ @player2_wins = 0
15
+ @comp_wins = 0
16
+ @draws = 0
17
+ end
18
+
19
+ def player1_turn
20
+ column = gets.chomp
21
+ if @game.board.place(column, 'X')
22
+ @player_turns << column
23
+ @game.win?
24
+ else
25
+ puts 'That is not a valid selection, please select a new column'
26
+ player1_turn
27
+ end
28
+ end
29
+
30
+ def player2_turn
31
+ column = gets.chomp
32
+ if @game.board.place(column, 'O')
33
+ @player2_turns << column
34
+ @game.win?
35
+ else
36
+ puts 'That is not a valid selection, please select a new column'
37
+ player2_turn
38
+ end
39
+ end
40
+
41
+ def computer_turn
42
+ comp_column = (65 + rand(7)).chr
43
+ if @game.board.place(comp_column, 'O')
44
+ @comp_turns << comp_column
45
+ @game.win?
46
+ comp_column
47
+ else
48
+ computer_turn
49
+ end
50
+ end
51
+
52
+ def game_menu
53
+ clear_terminal
54
+ puts logo
55
+ puts
56
+ puts 'Enter p to play the highest stakes game of your life with a computer. Enter 2 to play with two players. Enter q to wimp out and quit.'
57
+ ans = gets.chomp
58
+ decide_the_path(ans)
59
+ end
60
+
61
+ def turn_round_two_players
62
+ clear_terminal
63
+ if @player1_name == nil
64
+ obtain_player_names
65
+ clear_terminal
66
+ end
67
+ two_player_turns(@player1_name, @player2_name, @player_turns, @player2_turns)
68
+ game_end_two_player
69
+ end
70
+
71
+ def turn_round
72
+ loop do
73
+ clear_terminal
74
+ show_last_moves('You', 'computer', @player_turns, @comp_turns)
75
+ render_board_with_lines
76
+ column_choice("intrepid player")
77
+ player1_turn
78
+ break if game_over?
79
+ computer_turn
80
+ break if game_over?
81
+ end
82
+ game_end
83
+ end
84
+
85
+ def game_end
86
+ if @game.game_win == true
87
+ clear_terminal
88
+ @player_wins += 1
89
+ puts 'YOU WON! HOW COOL!'
90
+ elsif @game.game_win == false
91
+ clear_terminal
92
+ @comp_wins += 1
93
+ puts "YOU LOST! You're not smart enough to beat a computer choosing random columns? Sad day."
94
+ elsif @game.game_draw == true
95
+ clear_terminal
96
+ @draws += 1
97
+ puts "That was a draw. BOOOOOOOORRRRRRIIIIIINGGGGGGGG. You're literally not smart enough to beat a computer choosing random columns."
98
+ end
99
+ puts "================================\n\nYou've won #{@player_wins} times\n\nThe computer has won #{@comp_wins} times\n\nYou've had #{@draws} draw matches\n\n================================\n\nPress ENTER to continue"
100
+ gets.chomp
101
+ game_menu
102
+ end
103
+
104
+ def game_end_two_player
105
+ if @game.game_win == true
106
+ clear_terminal
107
+ @player_wins += 1
108
+ puts "#{@player1_name.capitalize} WON! HOW COOL!"
109
+ elsif @game.game_win == false
110
+ clear_terminal
111
+ @player2_wins += 1
112
+ puts "#{@player2_name.capitalize} WON! HOW COOL!"
113
+ elsif @game.game_draw == true
114
+ clear_terminal
115
+ @draws += 1
116
+ puts "That was a draw. BOOOOOOOORRRRRRIIIIIINGGGGGGGG."
117
+ end
118
+ puts "================================\n\n#{@player1_name.capitalize} won #{@player_wins} times\n\n#{@player2_name.capitalize} won #{@player2_wins} times\n\nYou've had #{@draws} draw matches\n\n================================\n\nPress ENTER to continue"
119
+ gets.chomp
120
+ game_menu
121
+ end
122
+ end
@@ -0,0 +1,110 @@
1
+ module Playable
2
+
3
+ def logo
4
+ '
5
+ ==============================================================
6
+ _________ __ _____
7
+ \_ ___ \ ____ ____ ____ ____ _____/ |_ / | |
8
+ / \ \/ / _ \ / \ / \_/ __ \_/ ___\ __\ / | |_
9
+ \ \___( <_> ) | \ | \ ___/\ \___| | / ^ /
10
+ \______ /\____/|___| /___| /\___ >\___ >__| \____ |
11
+ \/ \/ \/ \/ \/ |__|
12
+ =============================================================='
13
+ end
14
+
15
+ def game_over_logo
16
+ '
17
+ ________ _____ _____ ___________ ____________ _________________________
18
+ / _____/ / _ \ / \ \_ _____/ \_____ \ \ / /\_ _____/\______ \
19
+ / \ ___ / /_\ \ / \ / \ | __)_ / | \ Y / | __)_ | _/
20
+ \ \_\ \/ | \/ Y \| \ / | \ / | \ | | \
21
+ \______ /\____|__ /\____|__ /_______ / \_______ /\___/ /_______ / |____|_ /
22
+ \/ \/ \/ \/ \/ \/ \/ '
23
+ end
24
+ #don't know how to write a test for this.
25
+ def reset_turns
26
+ @game.board.reset
27
+ @game.game_win = nil
28
+ @game.game_draw = false
29
+ @player_turns = []
30
+ @player2_turns = []
31
+ @comp_turns = []
32
+ end
33
+
34
+ def clear_terminal
35
+ puts `clear`
36
+ end
37
+
38
+ def decide_the_path(user_input)
39
+ if user_input == "p"
40
+ reset_turns
41
+ turn_round
42
+ elsif user_input == '2'
43
+ reset_turns
44
+ turn_round_two_players
45
+ elsif user_input == "q"
46
+ clear_terminal
47
+ puts game_over_logo
48
+ puts "\n\n"
49
+ else
50
+ clear_terminal
51
+ puts "#{user_input} isn't a valid answer fool!!"
52
+ game_menu
53
+ end
54
+ end
55
+
56
+ def obtain_player_names
57
+ puts 'Player 1 enter your name, your token is a X'
58
+ @player1_name = gets.chomp
59
+ puts 'Player 2 enter your name, your token is an O'
60
+ @player2_name = gets.chomp
61
+ end
62
+
63
+ def render_board_with_lines
64
+ puts "\n\n==============="
65
+ @game.board.render
66
+ puts "===============\n\n"
67
+ end
68
+
69
+ def column_choice(player)
70
+ puts "What column do you choose, #{player.capitalize}?"
71
+ end
72
+
73
+ def game_over?
74
+ @game.game_win != nil || @game.game_draw
75
+ end
76
+
77
+ def show_last_moves(p1_name, p2_name, p1_turns, p2_turns)
78
+ puts "#{p1_name.capitalize} chose #{p1_turns.last} and #{p2_name.capitalize} chose #{p2_turns.last}" if p1_turns != []
79
+ end
80
+
81
+ def two_player_turns(p1_name, p2_name, p1_turns, p2_turns)
82
+ loop do
83
+ show_last_moves(p1_name, p2_name, p1_turns, p2_turns)
84
+ render_board_with_lines
85
+ column_choice(p1_name)
86
+ player1_turn
87
+ break if game_over?
88
+ clear_terminal
89
+ render_board_with_lines
90
+ column_choice(p2_name)
91
+ player2_turn
92
+ break if game_over?
93
+ clear_terminal
94
+ end
95
+
96
+ def player_v_comp_turns(p1_name, p2_name, p1_turns, p2_turns)
97
+ loop do
98
+ clear_terminal
99
+ show_last_moves('You', 'computer', p1_turns, p2_turns)
100
+ render_board_with_lines
101
+ column_choice("intrepid player")
102
+ player1_turn
103
+ break if game_over?
104
+ computer_turn
105
+ break if game_over?
106
+ end
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cnnct4
4
+ VERSION = "0.0.0"
5
+ end
data/lib/cnnct4.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "cnnct4/version"
4
+ require './lib/cnnct4/board'
5
+ require './lib/cnnct4/game'
6
+ require './lib/cnnct4/game_play'
7
+ require './lib/cnnct4/playable'
8
+
9
+
10
+
11
+ module Cnnct4
12
+ class Error < StandardError; end
13
+ # Your code goes here...
14
+ end
data/sig/cnnct4.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Cnnct4
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cnnct4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Reid Miller
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-02-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ description: A CLI connect 4 game playable by 1 or 2 players
42
+ email:
43
+ - reid.s.miller@gmail.com
44
+ executables:
45
+ - cnnct4
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".rspec"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - cnnct4.gemspec
55
+ - exe/cnnct4
56
+ - lib/cnnct4.rb
57
+ - lib/cnnct4/board.rb
58
+ - lib/cnnct4/cli.rb
59
+ - lib/cnnct4/game.rb
60
+ - lib/cnnct4/game_play.rb
61
+ - lib/cnnct4/playable.rb
62
+ - lib/cnnct4/version.rb
63
+ - sig/cnnct4.rbs
64
+ homepage: https://github.com/reidsmiller/connect-4
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ homepage_uri: https://github.com/reidsmiller/connect-4
69
+ source_code_uri: https://github.com/reidsmiller/connect-4
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.6.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.1.4
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Thomas and Reid's Connect 4 game.
89
+ test_files: []