cnnct4 0.0.1 → 0.1.1

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: 18acb8b810dae3fe6c06643688995e5a710057226b4d3086e062cdbc1a995063
4
- data.tar.gz: 0e41015987b4b2b03ced04ed37b2ea04c89e5b0772824e2fa36fccaa3c4620fe
3
+ metadata.gz: f971f8cd6ef5a323c7c4beec7c3a0599b4140a8da25c5232f7600b7bcbace832
4
+ data.tar.gz: 277566ea1daee94bc86eef78a43508a10147394bf48c8afcaa3ef63d39e8999b
5
5
  SHA512:
6
- metadata.gz: 569d45620763f463c3d488f25fd00117a2d1287fb2923a1ec24f116a33e3c721996462f15bd1f4ab5b005a271d232b6fcc52fc095b6690bd48a031a50be1d4af
7
- data.tar.gz: 724da28d85b5f721966bd24a4c287a35a945d600104e8514aee536971561d46ef097bf86d284c613312cc854ae5592fa0f16397cf302d8c4118ab26fa6b83f91
6
+ metadata.gz: c8bfa99e96e43a52f6ef298a0f78a6f2d9165fb682ca526f40df2c610bcd109775d07ca8d380648188147e4f42e4cd1123d97b7950ab32c25e9b5210034ba993
7
+ data.tar.gz: '08a05afd5bf4dee0260b76af359e549a73f7d030ae693a5b4c59b33f840a368b88bdeffe8cc3f48d5ac8f5af0a729a8cb008e91226c23dd67a7c22c5fb2c8486'
data/Gemfile.lock CHANGED
@@ -1,13 +1,18 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cnnct4 (0.0.0)
4
+ cnnct4 (0.1.1)
5
5
  thor
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ coderay (1.1.3)
10
11
  diff-lcs (1.5.0)
12
+ method_source (1.0.0)
13
+ pry (0.14.2)
14
+ coderay (~> 1.1)
15
+ method_source (~> 1.0)
11
16
  rake (13.0.6)
12
17
  rspec (3.12.0)
13
18
  rspec-core (~> 3.12.0)
@@ -26,11 +31,14 @@ GEM
26
31
 
27
32
  PLATFORMS
28
33
  x86_64-darwin-21
34
+ x86_64-darwin-22
35
+ x86_64-linux
29
36
 
30
37
  DEPENDENCIES
31
38
  cnnct4!
39
+ pry
32
40
  rake (~> 13.0)
33
41
  rspec (~> 3.0)
34
42
 
35
43
  BUNDLED WITH
36
- 2.4.5
44
+ 2.4.3
data/cnnct4.gemspec CHANGED
@@ -5,8 +5,8 @@ require_relative "lib/cnnct4/version"
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "cnnct4"
7
7
  spec.version = Cnnct4::VERSION
8
- spec.authors = ["Reid Miller"]
9
- spec.email = ["reid.s.miller@gmail.com"]
8
+ spec.authors = ["Reid Miller", "Thomas Hawley"]
9
+ spec.email = ["reid.s.miller@gmail.com", "tchawley2@gmail.com"]
10
10
 
11
11
  spec.summary = "Thomas and Reid's Connect 4 game."
12
12
  spec.description = "A CLI connect 4 game playable by 1 or 2 players"
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
31
31
  # Uncomment to register a new dependency of your gem
32
32
  # spec.add_dependency "example-gem", "~> 1.0"
33
33
  spec.add_dependency "thor"
34
+ spec.add_development_dependency "pry"
34
35
  spec.add_development_dependency "rspec", "~> 3.2"
35
36
 
36
37
  # For more information and examples about making a new gem, check out our
data/lib/cnnct4/board.rb CHANGED
@@ -2,13 +2,22 @@ class Board
2
2
  attr_reader :board_array
3
3
 
4
4
  def initialize
5
- @board_array = Array.new(6) { Array.new(7, ".")}
5
+ @board_array = [
6
+ [Cell.new(0, 'A'), Cell.new(0, 'B'), Cell.new(0, 'C'), Cell.new(0, 'D'), Cell.new(0, 'E'), Cell.new(0, 'F'), Cell.new(0, 'G')],
7
+ [Cell.new(1, 'A'), Cell.new(1, 'B'), Cell.new(1, 'C'), Cell.new(1, 'D'), Cell.new(1, 'E'), Cell.new(1, 'F'), Cell.new(1, 'G')],
8
+ [Cell.new(2, 'A'), Cell.new(2, 'B'), Cell.new(2, 'C'), Cell.new(2, 'D'), Cell.new(2, 'E'), Cell.new(2, 'F'), Cell.new(2, 'G')],
9
+ [Cell.new(3, 'A'), Cell.new(3, 'B'), Cell.new(3, 'C'), Cell.new(3, 'D'), Cell.new(3, 'E'), Cell.new(3, 'F'), Cell.new(3, 'G')],
10
+ [Cell.new(4, 'A'), Cell.new(4, 'B'), Cell.new(4, 'C'), Cell.new(4, 'D'), Cell.new(4, 'E'), Cell.new(4, 'F'), Cell.new(4, 'G')],
11
+ [Cell.new(5, 'A'), Cell.new(5, 'B'), Cell.new(5, 'C'), Cell.new(5, 'D'), Cell.new(5, 'E'), Cell.new(5, 'F'), Cell.new(5, 'G')]
12
+ ]
6
13
  end
7
14
 
8
15
  def render
9
16
  puts " A B C D E F G"
10
17
  @board_array.each do |row|
11
- print " #{row.join(" ")}"
18
+ mark_row = []
19
+ row.each {|cell| mark_row << cell.mark}
20
+ print " #{mark_row.join(" ")}"
12
21
  puts
13
22
  end
14
23
  end
@@ -20,8 +29,8 @@ class Board
20
29
  column = column.ord - 65
21
30
  row = 5
22
31
  while row >= 0
23
- if @board_array[row][column] == "."
24
- @board_array[row][column] = mark
32
+ if @board_array[row][column].mark == "."
33
+ @board_array[row][column].mark = mark
25
34
  return true
26
35
  else
27
36
  row -= 1
@@ -32,6 +41,13 @@ class Board
32
41
  end
33
42
 
34
43
  def reset
35
- @board_array = Array.new(6) { Array.new(7, ".")}
44
+ @board_array = [
45
+ [Cell.new(0, 'A'), Cell.new(0, 'B'), Cell.new(0, 'C'), Cell.new(0, 'D'), Cell.new(0, 'E'), Cell.new(0, 'F'), Cell.new(0, 'G')],
46
+ [Cell.new(1, 'A'), Cell.new(1, 'B'), Cell.new(1, 'C'), Cell.new(1, 'D'), Cell.new(1, 'E'), Cell.new(1, 'F'), Cell.new(1, 'G')],
47
+ [Cell.new(2, 'A'), Cell.new(2, 'B'), Cell.new(2, 'C'), Cell.new(2, 'D'), Cell.new(2, 'E'), Cell.new(2, 'F'), Cell.new(2, 'G')],
48
+ [Cell.new(3, 'A'), Cell.new(3, 'B'), Cell.new(3, 'C'), Cell.new(3, 'D'), Cell.new(3, 'E'), Cell.new(3, 'F'), Cell.new(3, 'G')],
49
+ [Cell.new(4, 'A'), Cell.new(4, 'B'), Cell.new(4, 'C'), Cell.new(4, 'D'), Cell.new(4, 'E'), Cell.new(4, 'F'), Cell.new(4, 'G')],
50
+ [Cell.new(5, 'A'), Cell.new(5, 'B'), Cell.new(5, 'C'), Cell.new(5, 'D'), Cell.new(5, 'E'), Cell.new(5, 'F'), Cell.new(5, 'G')]
51
+ ]
36
52
  end
37
53
  end
@@ -0,0 +1,10 @@
1
+ class Cell
2
+ attr_reader :row_pos, :column_pos
3
+ attr_accessor :mark
4
+
5
+ def initialize (row_pos, column_pos)
6
+ @mark = "."
7
+ @row_pos = row_pos
8
+ @column_pos = column_pos
9
+ end
10
+ end
data/lib/cnnct4/game.rb CHANGED
@@ -19,7 +19,9 @@ class Game
19
19
 
20
20
  def check_for_win(array)
21
21
  array.each do |row|
22
- row.each_cons(4) do |group|
22
+ mark_row = []
23
+ row.each {|cell| mark_row << cell.mark}
24
+ mark_row.each_cons(4) do |group|
23
25
  if group == ["X", "X", "X", "X"]
24
26
  @game_win = true
25
27
  elsif group == ["O", "O", "O", "O"]
@@ -30,15 +32,13 @@ class Game
30
32
  end
31
33
 
32
34
  def check_for_draw
33
- @game_draw = true if !@board.board_array.flatten.any?(/[.]/)
35
+ @game_draw = true if @board.board_array.flatten.none? {|cell| cell.mark == "."}
34
36
  end
35
37
 
36
38
  def vertical_sort
37
39
  @board.board_array.transpose
38
40
  end
39
41
 
40
- # Look into .with_index
41
- #possibly making both diagonal up and down one method with arguments
42
42
  def diagonal_up_sort
43
43
  padding = 5
44
44
  padded_matrix = []
@@ -60,4 +60,76 @@ class Game
60
60
  end
61
61
  padded_matrix.transpose.map {|array| array.compact}
62
62
  end
63
- end
63
+
64
+ def comp_check
65
+ if check_for_3(vertical_sort)
66
+ true
67
+ elsif check_for_3(@board.board_array)
68
+ true
69
+ elsif check_for_3(diagonal_up_sort)
70
+ true
71
+ elsif check_for_3(diagonal_down_sort)
72
+ true
73
+ else
74
+ false
75
+ end
76
+ end
77
+
78
+ def check_for_3(method)
79
+ catch(:done) do
80
+ method.each do |row|
81
+ row.each_cons(4) do |cell_row|
82
+ mark_row = []
83
+ cell_row.each {|cell| mark_row << cell.mark}
84
+ check_win(cell_row, mark_row)
85
+ check_block(cell_row, mark_row)
86
+ end
87
+ end
88
+ false
89
+ end
90
+ end
91
+
92
+ def check_one_mark_down_and_place(cell_row, dot_pos)
93
+ cell_down = @board.board_array.flatten.select do |cell|
94
+ cell.row_pos == cell_row[dot_pos].row_pos + 1 && cell.column_pos == cell_row[dot_pos].column_pos
95
+ end
96
+ if cell_down[0]&.mark != "."
97
+ @board.place(cell_row[dot_pos].column_pos, "O")
98
+ throw(:done, true)
99
+ end
100
+ end
101
+
102
+ def check_win(cell_row, mark_row)
103
+ if mark_row == [".", "O", "O", "O"]
104
+ dot_pos = 0
105
+ check_one_mark_down_and_place(cell_row, dot_pos)
106
+ elsif mark_row == ["O", ".", "O", "O"]
107
+ dot_pos = 1
108
+ check_one_mark_down_and_place(cell_row, dot_pos)
109
+ elsif mark_row == ["O", "O", ".", "O"]
110
+ dot_pos = 2
111
+ check_one_mark_down_and_place(cell_row, dot_pos)
112
+ elsif mark_row == ["O", "O", "O", "."]
113
+ dot_pos = 3
114
+ check_one_mark_down_and_place(cell_row, dot_pos)
115
+ end
116
+ end
117
+
118
+ def check_block(cell_row, mark_row)
119
+ if mark_row == [".", "X", "X", "X"]
120
+ dot_pos = 0
121
+ check_one_mark_down_and_place(cell_row, dot_pos)
122
+ elsif mark_row == ["X", ".", "X", "X"]
123
+ dot_pos = 1
124
+ check_one_mark_down_and_place(cell_row, dot_pos)
125
+ elsif mark_row == ["X", "X", ".", "X"]
126
+ dot_pos = 2
127
+ check_one_mark_down_and_place(cell_row, dot_pos)
128
+ elsif mark_row == ["X", "X", "X", "."]
129
+ dot_pos = 3
130
+ check_one_mark_down_and_place(cell_row, dot_pos)
131
+ end
132
+ end
133
+ end
134
+
135
+
@@ -7,9 +7,9 @@ include Playable
7
7
  @player1_name = nil
8
8
  @player2_name = nil
9
9
  @game = Game.new
10
- @player_turns = []
10
+ @player1_turns = []
11
11
  @player2_turns = []
12
- @comp_turns = []
12
+ @comp_turns = [] #Need functionality for comp_check turns to be shoveled here
13
13
  @player_wins = 0
14
14
  @player2_wins = 0
15
15
  @comp_wins = 0
@@ -19,10 +19,10 @@ include Playable
19
19
  def player1_turn
20
20
  column = gets.chomp
21
21
  if @game.board.place(column, 'X')
22
- @player_turns << column
22
+ @player1_turns << column
23
23
  @game.win?
24
24
  else
25
- puts 'That is not a valid selection, please select a new column'
25
+ invalid_selection_message
26
26
  player1_turn
27
27
  end
28
28
  end
@@ -33,19 +33,23 @@ include Playable
33
33
  @player2_turns << column
34
34
  @game.win?
35
35
  else
36
- puts 'That is not a valid selection, please select a new column'
36
+ invalid_selection_message
37
37
  player2_turn
38
38
  end
39
39
  end
40
40
 
41
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
42
+ if @game.comp_check
43
+ @game.win?
44
+ else
45
+ comp_column = (65 + rand(7)).chr
46
+ if @game.board.place(comp_column, 'O')
47
+ @comp_turns << comp_column
48
+ @game.win?
49
+ comp_column
50
+ else
51
+ computer_turn
52
+ end
49
53
  end
50
54
  end
51
55
 
@@ -64,35 +68,29 @@ include Playable
64
68
  obtain_player_names
65
69
  clear_terminal
66
70
  end
67
- two_player_turns(@player1_name, @player2_name, @player_turns, @player2_turns)
71
+ two_player_turns(@player1_name, @player2_name, @player1_turns, @player2_turns)
68
72
  game_end_two_player
69
73
  end
70
74
 
71
75
  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
76
+ player_v_comp_turns('You', 'the computer', @player1_turns, @comp_turns)
82
77
  game_end
83
78
  end
84
79
 
85
80
  def game_end
86
81
  if @game.game_win == true
87
82
  clear_terminal
83
+ @game.board.render
88
84
  @player_wins += 1
89
85
  puts 'YOU WON! HOW COOL!'
90
86
  elsif @game.game_win == false
91
87
  clear_terminal
88
+ @game.board.render
92
89
  @comp_wins += 1
93
90
  puts "YOU LOST! You're not smart enough to beat a computer choosing random columns? Sad day."
94
91
  elsif @game.game_draw == true
95
92
  clear_terminal
93
+ @game.board.render
96
94
  @draws += 1
97
95
  puts "That was a draw. BOOOOOOOORRRRRRIIIIIINGGGGGGGG. You're literally not smart enough to beat a computer choosing random columns."
98
96
  end
@@ -104,14 +102,17 @@ include Playable
104
102
  def game_end_two_player
105
103
  if @game.game_win == true
106
104
  clear_terminal
105
+ @game.board.render
107
106
  @player_wins += 1
108
107
  puts "#{@player1_name.capitalize} WON! HOW COOL!"
109
108
  elsif @game.game_win == false
110
109
  clear_terminal
110
+ @game.board.render
111
111
  @player2_wins += 1
112
112
  puts "#{@player2_name.capitalize} WON! HOW COOL!"
113
113
  elsif @game.game_draw == true
114
114
  clear_terminal
115
+ @game.board.render
115
116
  @draws += 1
116
117
  puts "That was a draw. BOOOOOOOORRRRRRIIIIIINGGGGGGGG."
117
118
  end
@@ -21,7 +21,7 @@ _________ __ _____
21
21
  \______ /\____|__ /\____|__ /_______ / \_______ /\___/ /_______ / |____|_ /
22
22
  \/ \/ \/ \/ \/ \/ \/ '
23
23
  end
24
- #don't know how to write a test for this.
24
+
25
25
  def reset_turns
26
26
  @game.board.reset
27
27
  @game.game_win = nil
@@ -31,6 +31,10 @@ _________ __ _____
31
31
  @comp_turns = []
32
32
  end
33
33
 
34
+ def invalid_selection_message
35
+ puts 'That is not a valid selection, please select a new column'
36
+ end
37
+
34
38
  def clear_terminal
35
39
  puts `clear`
36
40
  end
@@ -92,19 +96,18 @@ _________ __ _____
92
96
  break if game_over?
93
97
  clear_terminal
94
98
  end
99
+ end
95
100
 
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
101
+ def player_v_comp_turns(p1_name, p2_name, p1_turns, p2_turns)
102
+ loop do
103
+ clear_terminal
104
+ show_last_moves(p1_name, p2_name, p1_turns, p2_turns)
105
+ render_board_with_lines
106
+ column_choice("intrepid player")
107
+ player1_turn
108
+ break if game_over?
109
+ computer_turn
110
+ break if game_over?
107
111
  end
108
112
  end
109
-
110
113
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Cnnct4
4
- VERSION = "0.0.1"
4
+ VERSION = "0.1.1"
5
5
  end
data/lib/cnnct4.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "cnnct4/version"
4
+ require_relative 'cnnct4/cell'
4
5
  require_relative 'cnnct4/board'
5
6
  require_relative 'cnnct4/game'
6
7
  require_relative 'cnnct4/game_play'
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cnnct4
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Miller
8
+ - Thomas Hawley
8
9
  autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2023-02-23 00:00:00.000000000 Z
12
+ date: 2023-02-27 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: thor
@@ -24,6 +25,20 @@ dependencies:
24
25
  - - ">="
25
26
  - !ruby/object:Gem::Version
26
27
  version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: pry
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
27
42
  - !ruby/object:Gem::Dependency
28
43
  name: rspec
29
44
  requirement: !ruby/object:Gem::Requirement
@@ -41,6 +56,7 @@ dependencies:
41
56
  description: A CLI connect 4 game playable by 1 or 2 players
42
57
  email:
43
58
  - reid.s.miller@gmail.com
59
+ - tchawley2@gmail.com
44
60
  executables:
45
61
  - cnnct4
46
62
  extensions: []
@@ -56,6 +72,7 @@ files:
56
72
  - exe/cnnct4
57
73
  - lib/cnnct4.rb
58
74
  - lib/cnnct4/board.rb
75
+ - lib/cnnct4/cell.rb
59
76
  - lib/cnnct4/cli.rb
60
77
  - lib/cnnct4/game.rb
61
78
  - lib/cnnct4/game_play.rb