From a59d239f4d8d8ed9b659bb62caeb9584f7f02611 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 19:47:22 -0500 Subject: [PATCH 01/37] Add name to README --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aaaae14..bf9947c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ ## project_game_center -[An Object-Oriented Ruby project from the Viking Code School](http://www.vikingcodeschool.com) \ No newline at end of file +[An Object-Oriented Ruby project from the Viking Code School](http://www.vikingcodeschool.com) + +Robert Wayne \ No newline at end of file From c9d4ca1d6f9bfa0047b987a8a1137371ba3ef0b6 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:08:08 -0500 Subject: [PATCH 02/37] Initial game outline --- connect_four/board.rb | 53 ++++++++++++++++++++++++++++++++++++ connect_four/connect_four.rb | 31 ++++++++++++++++++++- connect_four/player.rb | 20 ++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 connect_four/board.rb create mode 100644 connect_four/player.rb diff --git a/connect_four/board.rb b/connect_four/board.rb new file mode 100644 index 0000000..d0eeed2 --- /dev/null +++ b/connect_four/board.rb @@ -0,0 +1,53 @@ +# Maintains game board state +class Board +# initialize board + # set up blank data structure + +# render + # loop through data structure + # display an existing marker if any, else blank + +# add_piece + # IF piece_location_valid? + # place piece + # ELSE + # display error message + +# move_valid? + # Is the placement valid_column? + # Is the piece column_available? + +# valid_column? + # UNLESS column is in the acceptable range + # display an error message + +# columns_available? + # UNLESS column is not full + # display error message + +# winning_combination? + # is there a winning_diagonal? + # or winning_vertical? + # or winning_horizontal? for that piece? + +# winning_diagonal? + # check if specified piece has four in a row across diagonals + +# winning_vertical? + # check if specified piece has four in a row across verticals + +# winning_horizontal? + # check if specified piece has four in a row across horizontals + +# diagonals + # return the diagonal pieces + +# verticals + # return the vertical pieces + +# horizontals + # return the horizontal pieces + +# full? + # does every slot contain a piece? +end \ No newline at end of file diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index 34bc0e8..48d095f 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -1 +1,30 @@ -# Your code here! \ No newline at end of file +# Controls the game play +class ConnectFour +# initialize + # set up the board + # set up the players + # assign the starting player + +# play + # loop infinitely + # call the board rendering method + # ask for coordinates from the current player + # break the loop IF the game is over + # switch players + +# check_game_over + # check_victory + # check_draw + +# check_victory + # IF board says current player's piece has + # a winning_combination? + # display a victory message + +# check_draw + # IF board says it's full + # display a draw message + +# switch_players + # PlayerX >> PlayerO or vice versa +end \ No newline at end of file diff --git a/connect_four/player.rb b/connect_four/player.rb new file mode 100644 index 0000000..1315393 --- /dev/null +++ b/connect_four/player.rb @@ -0,0 +1,20 @@ +# Manages all player-related functionality +class Player +# initialize + # Set marker type (e.g. X or O) + +# get_column + # loop infinitely + # ask_for_column + # IF validate_column_format is true + # IF piece can be placed on Board + # break the loop + +# ask_for_column + # display message asking for column + # pull column choice from command line + +# validate_column_format + # UNLESS column choice is in the proper format + # display error message +end \ No newline at end of file From 1559e584de2dc5a6fd6817e58d23ddec4346a78c Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:11:20 -0500 Subject: [PATCH 03/37] Add initialize method --- connect_four/connect_four.rb | 40 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index 48d095f..ac54ef9 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -1,30 +1,38 @@ # Controls the game play class ConnectFour # initialize - # set up the board - # set up the players - # assign the starting player + def initialize + # set up the board + @board = Board.new + + # set up the players + @player_x = Player.new("Player 1", :x, @board) + @player_y = Player.new("Player 2", :y, @board) + + # assign the starting player + @current_player = @player_x + end # play - # loop infinitely - # call the board rendering method - # ask for coordinates from the current player - # break the loop IF the game is over - # switch players +# loop infinitely +# call the board rendering method +# ask for coordinates from the current player +# break the loop IF the game is over +# switch players # check_game_over - # check_victory - # check_draw +# check_victory +# check_draw # check_victory - # IF board says current player's piece has - # a winning_combination? - # display a victory message +# IF board says current player's piece has +# a winning_combination? +# display a victory message # check_draw - # IF board says it's full - # display a draw message +# IF board says it's full +# display a draw message # switch_players - # PlayerX >> PlayerO or vice versa +# PlayerX >> PlayerO or vice versa end \ No newline at end of file From 8d58a547a3f4215d221cc16a1e099c55eaa30230 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:14:17 -0500 Subject: [PATCH 04/37] Add play method --- connect_four/connect_four.rb | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index ac54ef9..ccecc0e 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -14,11 +14,22 @@ def initialize end # play -# loop infinitely -# call the board rendering method -# ask for coordinates from the current player -# break the loop IF the game is over -# switch players + def play + # loop infinitely + loop do + # call the board rendering method + @board.render + + # ask for column from the current player + @current_player.get_column + + # break the loop IF the game is over + break if check_game_over + + # switch players + switch_players + end + end # check_game_over # check_victory From a8a0dabc5296798255a98a1cee4a4a2cd3a35104 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:15:21 -0500 Subject: [PATCH 05/37] Add check_game_over method --- connect_four/connect_four.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index ccecc0e..cfe353f 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -32,8 +32,11 @@ def play end # check_game_over -# check_victory -# check_draw + def check_game_over + # check_victory + # check_draw + check_victory || check_draw + end # check_victory # IF board says current player's piece has From 3f23fa67009d4810cac144b6449da12b71adbbfc Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:18:48 -0500 Subject: [PATCH 06/37] Add check_victory method --- connect_four/connect_four.rb | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index cfe353f..da63c1e 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -1,6 +1,6 @@ # Controls the game play class ConnectFour -# initialize + # initialize def initialize # set up the board @board = Board.new @@ -13,7 +13,7 @@ def initialize @current_player = @player_x end -# play + # play def play # loop infinitely loop do @@ -31,17 +31,22 @@ def play end end -# check_game_over + # check_game_over def check_game_over # check_victory # check_draw check_victory || check_draw end -# check_victory -# IF board says current player's piece has -# a winning_combination? -# display a victory message + # check_victory + def check_victory + # IF board says current player's piece has + # a winning_combination? + if @board.winning_combination?(@current_player.piece) + # display a victory message + puts "Congratulations, #{current_player}! You won!" + end + end # check_draw # IF board says it's full From 45f2270034e3fc5eaf0a86480ac0a1767fc1f449 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:21:32 -0500 Subject: [PATCH 07/37] Add switch_players method --- connect_four/connect_four.rb | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index da63c1e..10480fe 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -48,10 +48,25 @@ def check_victory end end -# check_draw -# IF board says it's full -# display a draw message + # check_draw + def check_draw + # IF board says it's full + if @board.full? + # display a draw message + puts "It's a draw!" + true + else + false + end + end -# switch_players -# PlayerX >> PlayerO or vice versa + # switch_players + def switch_players + # PlayerX >> PlayerO or vice versa + if @current_player == @player_x + @current_player = @player_y + else + @current_player = @player_x + end + end end \ No newline at end of file From e80c0577acac8b5cb013020310d053ff4ddeabe8 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:23:48 -0500 Subject: [PATCH 08/37] Add initialize method --- connect_four/player.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/connect_four/player.rb b/connect_four/player.rb index 1315393..9c006cc 100644 --- a/connect_four/player.rb +++ b/connect_four/player.rb @@ -1,7 +1,15 @@ # Manages all player-related functionality class Player -# initialize - # Set marker type (e.g. X or O) + attr_accessor :name, :piece + + # initialize + def initialize(name = "Mystery Player", piece, board) + # Set marker type (e.g. X or O) + raise "Piece must be a symbol!" unless piece.is_a?(Symbol) + @name = name + @piece = piece + @board = board + end # get_column # loop infinitely From b3bd0c02246e036c31dbd6efa4e3dbf9df55d724 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:26:11 -0500 Subject: [PATCH 09/37] Add get_column method --- connect_four/player.rb | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/connect_four/player.rb b/connect_four/player.rb index 9c006cc..1be6eb9 100644 --- a/connect_four/player.rb +++ b/connect_four/player.rb @@ -11,18 +11,27 @@ def initialize(name = "Mystery Player", piece, board) @board = board end -# get_column - # loop infinitely - # ask_for_column + # get_column + def get_column + # loop infinitely + loop do + # ask_for_column + ask_for_column + # IF validate_column_format is true + if validate_volumn_format # IF piece can be placed on Board - # break the loop + # break the loop + break if @board.add_piece(column, @piece) + end + end + end # ask_for_column - # display message asking for column - # pull column choice from command line +# display message asking for column +# pull column choice from command line # validate_column_format - # UNLESS column choice is in the proper format - # display error message +# UNLESS column choice is in the proper format +# display error message end \ No newline at end of file From 3b1da1022524cd402da0b49e7f42a72a394f2a43 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:29:30 -0500 Subject: [PATCH 10/37] Add ask_for_column method --- connect_four/player.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/connect_four/player.rb b/connect_four/player.rb index 1be6eb9..c24fd03 100644 --- a/connect_four/player.rb +++ b/connect_four/player.rb @@ -27,9 +27,14 @@ def get_column end end -# ask_for_column -# display message asking for column -# pull column choice from command line + # ask_for_column + def ask_for_column + # display message asking for column + puts "Please choose a column for your next move." + # # pull column choice from command line + gets.strip.to_i + end + # validate_column_format # UNLESS column choice is in the proper format From 359ba0e564f1af3bbd45f80109b3ac2f643d38d7 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Tue, 7 Feb 2017 23:35:04 -0500 Subject: [PATCH 11/37] Add validate_column_format method --- connect_four/player.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/connect_four/player.rb b/connect_four/player.rb index c24fd03..15efdda 100644 --- a/connect_four/player.rb +++ b/connect_four/player.rb @@ -16,10 +16,10 @@ def get_column # loop infinitely loop do # ask_for_column - ask_for_column + column = ask_for_column # IF validate_column_format is true - if validate_volumn_format + if validate_volumn_format(column) # IF piece can be placed on Board # break the loop break if @board.add_piece(column, @piece) @@ -37,6 +37,14 @@ def ask_for_column # validate_column_format -# UNLESS column choice is in the proper format -# display error message + def validate_column_format(column) + # UNLESS column choice is in the proper format + if column >= 1 && column <= 7 + true + else + # display error message + puts "Your choice is not in the proper format." + end + end + end \ No newline at end of file From a1c8a67a387560e68c8cbe623bcad620547cb3c0 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 10:07:18 -0500 Subject: [PATCH 12/37] Add initialize method --- connect_four/board.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/connect_four/board.rb b/connect_four/board.rb index d0eeed2..1273d41 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -1,7 +1,10 @@ # Maintains game board state class Board # initialize board + def initialize # set up blank data structure + @board = Array.new() + end # render # loop through data structure From fcf9fe7495f289fbd1738881a1a375cb49ca4778 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 10:14:47 -0500 Subject: [PATCH 13/37] Add render method --- connect_four/board.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 1273d41..f9cfa59 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -3,12 +3,24 @@ class Board # initialize board def initialize # set up blank data structure - @board = Array.new() + @board = Array.new(6) { Array.new(7) } end # render + def render + puts # loop through data structure - # display an existing marker if any, else blank + @board.each do |row| + # display an existing marker if any, else blank + row.each do |slot| + print " " + slot.nil? ? print("-") : print(slot) + print " " + end + puts + end + puts + end # add_piece # IF piece_location_valid? From be86f4a23b3d4ae1d2f19de51f2eccc0fb5a6c1b Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 10:21:31 -0500 Subject: [PATCH 14/37] Add valid_move? method --- connect_four/board.rb | 62 ++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 24 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index f9cfa59..8402853 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -2,14 +2,14 @@ class Board # initialize board def initialize - # set up blank data structure - @board = Array.new(6) { Array.new(7) } + # set up blank data structure + @board = Array.new(6) { Array.new(7) } end # render def render puts - # loop through data structure + # loop through data structure @board.each do |row| # display an existing marker if any, else blank row.each do |slot| @@ -23,46 +23,60 @@ def render end # add_piece - # IF piece_location_valid? - # place piece - # ELSE - # display error message + def add_piece(column, piece) + # IF valid_move? + if valid_move?(column) + # TODO - Finish add_piece method + true + # ELSE + false + end + end + +# valid_move?(column) + def valid_move?(column) + # Is the placement valid_column? + # Is the column_available? + if valid_column?(column) && column_available?(column) + true + else + puts "Invalid move." + false + end + end -# move_valid? - # Is the placement valid_column? - # Is the piece column_available? # valid_column? - # UNLESS column is in the acceptable range - # display an error message +# UNLESS column is in the acceptable range +# display an error message # columns_available? - # UNLESS column is not full - # display error message +# UNLESS column is not full +# display error message # winning_combination? - # is there a winning_diagonal? - # or winning_vertical? - # or winning_horizontal? for that piece? +# is there a winning_diagonal? +# or winning_vertical? +# or winning_horizontal? for that piece? # winning_diagonal? - # check if specified piece has four in a row across diagonals +# check if specified piece has four in a row across diagonals # winning_vertical? - # check if specified piece has four in a row across verticals +# check if specified piece has four in a row across verticals # winning_horizontal? - # check if specified piece has four in a row across horizontals +# check if specified piece has four in a row across horizontals # diagonals - # return the diagonal pieces +# return the diagonal pieces # verticals - # return the vertical pieces +# return the vertical pieces # horizontals - # return the horizontal pieces +# return the horizontal pieces # full? - # does every slot contain a piece? +# does every slot contain a piece? end \ No newline at end of file From 51fc19573096c0c261624f77bbec8cd6eb481e23 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 10:39:56 -0500 Subject: [PATCH 15/37] Add column_valid? method --- connect_four/board.rb | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 8402853..6f744ef 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -24,8 +24,8 @@ def render # add_piece def add_piece(column, piece) - # IF valid_move? - if valid_move?(column) + # IF move_valid?(column) + if move_valid?(column) # TODO - Finish add_piece method true # ELSE @@ -33,11 +33,11 @@ def add_piece(column, piece) end end -# valid_move?(column) - def valid_move?(column) - # Is the placement valid_column? +# move_valid?(column) + def move_valid?(column) + # Is the column_valid? # Is the column_available? - if valid_column?(column) && column_available?(column) + if column_valid?(column) && column_available?(column) true else puts "Invalid move." @@ -45,12 +45,18 @@ def valid_move?(column) end end +# column_valid?(column) + def column_valid?(column) + # UNLESS column is in the acceptable range + if column >= 1 && column <= 7 + true + else + # display an error message + puts "Invalid move." + end + end -# valid_column? -# UNLESS column is in the acceptable range -# display an error message - -# columns_available? +# column_available?(column) # UNLESS column is not full # display error message From 6ba9f983fe4e7077923db23f3ada2a81717654b8 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 11:11:02 -0500 Subject: [PATCH 16/37] Build column_available? method --- connect_four/board.rb | 44 +++++++++++++++++++++++++----------- connect_four/connect_four.rb | 8 ++++++- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 6f744ef..6428fd6 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -10,22 +10,27 @@ def initialize def render puts # loop through data structure - @board.each do |row| + 5.downto(0) do |row| # display an existing marker if any, else blank - row.each do |slot| - print " " - slot.nil? ? print("-") : print(slot) - print " " + @board[row].each do |column| + print "| " + column.nil? ? print("-") : print(column) + print " |" end puts end + + 7.times { print "|---|" } + puts + 1.upto(7) { |i| print "| #{i} |" } + puts puts end # add_piece - def add_piece(column, piece) + def add_piece(column_label, piece) # IF move_valid?(column) - if move_valid?(column) + if move_valid?(column_label) # TODO - Finish add_piece method true # ELSE @@ -34,10 +39,10 @@ def add_piece(column, piece) end # move_valid?(column) - def move_valid?(column) + def move_valid?(column_label) # Is the column_valid? # Is the column_available? - if column_valid?(column) && column_available?(column) + if column_valid?(column_label) && column_available?(column_label) true else puts "Invalid move." @@ -46,9 +51,9 @@ def move_valid?(column) end # column_valid?(column) - def column_valid?(column) + def column_valid?(column_label) # UNLESS column is in the acceptable range - if column >= 1 && column <= 7 + if column_label >= 1 && column_num <= 7 true else # display an error message @@ -57,8 +62,21 @@ def column_valid?(column) end # column_available?(column) -# UNLESS column is not full -# display error message + def column_available?(column_label) + column(column_label).include?(nil) ? true : false + end + + def column(column_label) + column_arr = [] + @board.each_with_index do |row| + if row[column_label - 1].nil? + break + else + column_arr.push(row[column_label - 1]) + end + end + column_arr + end # winning_combination? # is there a winning_diagonal? diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index 10480fe..71cd7c0 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -1,3 +1,6 @@ +require_relative "board.rb" +require_relative "player.rb" + # Controls the game play class ConnectFour # initialize @@ -69,4 +72,7 @@ def switch_players @current_player = @player_x end end -end \ No newline at end of file +end + +game = ConnectFour.new +game.play \ No newline at end of file From f5b921f0ca232d958d0e0654e44fc0baad829b53 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 11:31:25 -0500 Subject: [PATCH 17/37] Finish building add_piece method --- connect_four/board.rb | 25 +++++++++++++++---------- connect_four/connect_four.rb | 8 ++++---- connect_four/player.rb | 2 +- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 6428fd6..7959ad2 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -29,11 +29,18 @@ def render # add_piece def add_piece(column_label, piece) + column_index = column_label - 1 + # IF move_valid?(column) if move_valid?(column_label) - # TODO - Finish add_piece method - true - # ELSE + @board.each_with_index do |row, i| + if row[column_index].nil? + #place piece + @board[i][column_index] = piece + return true + end + end + else false end end @@ -53,7 +60,7 @@ def move_valid?(column_label) # column_valid?(column) def column_valid?(column_label) # UNLESS column is in the acceptable range - if column_label >= 1 && column_num <= 7 + if column_label >= 1 && column_label <= 7 true else # display an error message @@ -68,13 +75,11 @@ def column_available?(column_label) def column(column_label) column_arr = [] - @board.each_with_index do |row| - if row[column_label - 1].nil? - break - else - column_arr.push(row[column_label - 1]) - end + + @board.each do |row| + column_arr.push(row[column_label - 1]) end + column_arr end diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index 71cd7c0..3d72e13 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -9,8 +9,8 @@ def initialize @board = Board.new # set up the players - @player_x = Player.new("Player 1", :x, @board) - @player_y = Player.new("Player 2", :y, @board) + @player_x = Player.new("Player 1", :X, @board) + @player_o = Player.new("Player 2", :O, @board) # assign the starting player @current_player = @player_x @@ -27,7 +27,7 @@ def play @current_player.get_column # break the loop IF the game is over - break if check_game_over + # TODO - break if check_game_over # switch players switch_players @@ -67,7 +67,7 @@ def check_draw def switch_players # PlayerX >> PlayerO or vice versa if @current_player == @player_x - @current_player = @player_y + @current_player = @player_o else @current_player = @player_x end diff --git a/connect_four/player.rb b/connect_four/player.rb index 15efdda..bc0a5d7 100644 --- a/connect_four/player.rb +++ b/connect_four/player.rb @@ -19,7 +19,7 @@ def get_column column = ask_for_column # IF validate_column_format is true - if validate_volumn_format(column) + if validate_column_format(column) # IF piece can be placed on Board # break the loop break if @board.add_piece(column, @piece) From 877f78442f751f46a946418fbe08ef82117c2d81 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 13:13:50 -0500 Subject: [PATCH 18/37] Add winning_combination? method --- connect_four/board.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 7959ad2..3aaead7 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -77,16 +77,19 @@ def column(column_label) column_arr = [] @board.each do |row| - column_arr.push(row[column_label - 1]) + column_arr.push(row[column_label - 1]) end column_arr end -# winning_combination? +# winning_combination?(piece) + def winning_combination?(piece) # is there a winning_diagonal? # or winning_vertical? # or winning_horizontal? for that piece? + winning_diagonal?(piece) || winning_vertical?(piece) || winning_horizontal?(piece) + end # winning_diagonal? # check if specified piece has four in a row across diagonals From 1a29f54475b2f5dbe5bfc5ede3d66987a9bd1ab5 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 14:30:32 -0500 Subject: [PATCH 19/37] Add horizontals method --- connect_four/board.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 3aaead7..902ad40 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -95,7 +95,8 @@ def winning_combination?(piece) # check if specified piece has four in a row across diagonals # winning_vertical? -# check if specified piece has four in a row across verticals + def winning_vertical?(piece) + end # winning_horizontal? # check if specified piece has four in a row across horizontals @@ -104,10 +105,13 @@ def winning_combination?(piece) # return the diagonal pieces # verticals -# return the vertical pieces + # horizontals -# return the horizontal pieces + def horizontals +# return the horizontal rows + @board + end # full? # does every slot contain a piece? From c41220286fa46c930c693d71beb60718712bca8f Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 14:38:59 -0500 Subject: [PATCH 20/37] Add verticals method --- connect_four/board.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/connect_four/board.rb b/connect_four/board.rb index 902ad40..c8188a3 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -105,7 +105,15 @@ def winning_vertical?(piece) # return the diagonal pieces # verticals + def verticals + columns = [] + 1.upto(7) do |column_label| + columns.push(column(column_label)) + end + + columns + end # horizontals def horizontals From 10f4cbc7053207751cfcacd34688258c8ee4ae6c Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 14:55:27 -0500 Subject: [PATCH 21/37] Render board before displaying winning message --- connect_four/connect_four.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index 3d72e13..41f9c75 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -27,7 +27,7 @@ def play @current_player.get_column # break the loop IF the game is over - # TODO - break if check_game_over + break if check_game_over # switch players switch_players @@ -47,7 +47,11 @@ def check_victory # a winning_combination? if @board.winning_combination?(@current_player.piece) # display a victory message - puts "Congratulations, #{current_player}! You won!" + @board.render1 + puts "Congratulations, #{@current_player.name}! You won!" + true + else + false end end From 189e6e5a2085331434b410c081cb7cfc6f60477e Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 14:55:45 -0500 Subject: [PATCH 22/37] Add winning_vertical? method --- connect_four/board.rb | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index c8188a3..9a1bdd3 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -92,14 +92,34 @@ def winning_combination?(piece) end # winning_diagonal? -# check if specified piece has four in a row across diagonals + def winning_diagonal?(piece) +# TODO - check if specified piece has four in a row across diagonals + false + end + # winning_vertical? def winning_vertical?(piece) + verticals.each do |column| + consecutive_pieces = 0 + column.each do |slot| + if slot == piece + consecutive_pieces += 1 + return true if consecutive_pieces == 4 + else + consecutive_pieces = 0 + end + end + end + + false end # winning_horizontal? -# check if specified piece has four in a row across horizontals + def winning_horizontal?(piece) + # TODO - check if specified piece has four in a row across horizontals + false + end # diagonals # return the diagonal pieces @@ -122,5 +142,8 @@ def horizontals end # full? -# does every slot contain a piece? + def full? + # TODO - does every slot contain a piece? + false + end end \ No newline at end of file From f64aadb08201a36be81668bafd3b1297dcc16ebb Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 14:58:37 -0500 Subject: [PATCH 23/37] Add winning_horizontal? method --- connect_four/board.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 9a1bdd3..92788e8 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -117,7 +117,18 @@ def winning_vertical?(piece) # winning_horizontal? def winning_horizontal?(piece) - # TODO - check if specified piece has four in a row across horizontals + horizontals.each do |row| + consecutive_pieces = 0 + row.each do |slot| + if slot == piece + consecutive_pieces += 1 + return true if consecutive_pieces == 4 + else + consecutive_pieces = 0 + end + end + end + false end From 315cd64b970ee819b954d9073bcccf0dc8699262 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 14:59:55 -0500 Subject: [PATCH 24/37] Add winning_diagonals? method --- connect_four/board.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/connect_four/board.rb b/connect_four/board.rb index 92788e8..f737b45 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -132,6 +132,22 @@ def winning_horizontal?(piece) false end + def winning_diagonals?(piece) + diagonals.each do |diagonal| + consecutive_pieces = 0 + diagonal.each do |slot| + if slot == piece + consecutive_pieces += 1 + return true if consecutive_pieces == 4 + else + consecutive_pieces = 0 + end + end + end + + false + end + # diagonals # return the diagonal pieces From 3320a88f2dbcef306986272306b366d1ab866aa5 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 15:00:02 -0500 Subject: [PATCH 25/37] Fix typo --- connect_four/connect_four.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index 41f9c75..dc4fd3a 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -47,7 +47,7 @@ def check_victory # a winning_combination? if @board.winning_combination?(@current_player.piece) # display a victory message - @board.render1 + @board.render puts "Congratulations, #{@current_player.name}! You won!" true else From b523b62be191b937df69cd537b2737a34b47e5a8 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 16:06:44 -0500 Subject: [PATCH 26/37] Add winning_diagonal? method --- connect_four/board.rb | 102 +++++++++++++++++++++++++++++++++++------- 1 file changed, 87 insertions(+), 15 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index f737b45..cdff5af 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -93,7 +93,18 @@ def winning_combination?(piece) # winning_diagonal? def winning_diagonal?(piece) -# TODO - check if specified piece has four in a row across diagonals + diagonals.each do |diagonal| + consecutive_pieces = 0 + diagonal.each do |slot| + if slot == piece + consecutive_pieces += 1 + return true if consecutive_pieces == 4 + else + consecutive_pieces = 0 + end + end + end + false end @@ -132,24 +143,85 @@ def winning_horizontal?(piece) false end - def winning_diagonals?(piece) - diagonals.each do |diagonal| - consecutive_pieces = 0 - diagonal.each do |slot| - if slot == piece - consecutive_pieces += 1 - return true if consecutive_pieces == 4 - else - consecutive_pieces = 0 - end +# diagonals + def diagonals + # return the diagonal pieces + diagonals_arr = [] + + # column constant at 0, row increasing by 1 + 3.times do |i| + column_start = 0 + row_start = 0 + row_start += i + column = column_start + row = row_start + diagonal = [] + + while column <= 6 && row <= 5 + diagonal.push(@board[row][column]) + column += 1 + row += 1 end + + diagonals_arr.push(diagonal) end - false - end + # row constant at 0, column increasing by 1 + 3.times do |i| + column_start = 1 + row_start = 0 + column_start += i + column = column_start + row = row_start + diagonal = [] + + while column <= 6 && row <= 5 + diagonal.push(@board[row][column]) + column += 1 + row += 1 + end -# diagonals -# return the diagonal pieces + diagonals_arr.push(diagonal) + end + + # column constant at 6, row increasing by 1 + 3.times do |i| + column_start = 6 + row_start = 0 + row_start += i + column = column_start + row = row_start + diagonal = [] + + while column >= 0 && row <= 5 + diagonal.push(@board[row][column]) + column -= 1 + row += 1 + end + + diagonals_arr.push(diagonal) + end + + # row constant at 0, column decreasing by 1 + 3.times do |i| + column_start = 5 + row_start = 0 + column_start -= i + column = column_start + row = row_start + diagonal = [] + + while column >= 0 && row <= 5 + diagonal.push(@board[row][column]) + column -= 1 + row += 1 + end + + diagonals_arr.push(diagonal) + end + + diagonals_arr + end # verticals def verticals From 7bf460c88c3deb9bc1d07d414797d7c182c6a2e5 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 16:07:58 -0500 Subject: [PATCH 27/37] Add full? method --- connect_four/board.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index cdff5af..9541aba 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -242,7 +242,12 @@ def horizontals # full? def full? - # TODO - does every slot contain a piece? - false + @board.each do |row| + row.each do |slot| + return false if slot.nil? + end + end + + true end end \ No newline at end of file From b8f3cc8db64717c12c97a3f9dd85dfc14893d0b4 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 16:51:57 -0500 Subject: [PATCH 28/37] Add ComputerPlayer class --- connect_four/computer_player.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 connect_four/computer_player.rb diff --git a/connect_four/computer_player.rb b/connect_four/computer_player.rb new file mode 100644 index 0000000..8c5c8c9 --- /dev/null +++ b/connect_four/computer_player.rb @@ -0,0 +1,15 @@ +class ComputerPlayer + # initialize + def initialize(name = "Computer Player", piece, board) + # Set marker type (e.g. X or O) + raise "Piece must be a symbol!" unless piece.is_a?(Symbol) + @name = name + @piece = piece + @board = board + end + + # ask_for_column + def ask_for_column + + end +end \ No newline at end of file From 851f14fd102eea19f81a4fed58ef1412d17239d4 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 17:18:10 -0500 Subject: [PATCH 29/37] Add dup_board method --- connect_four/board.rb | 22 +++++++++++++++++++++- connect_four/computer_player.rb | 4 ++-- connect_four/connect_four.rb | 3 ++- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 9541aba..8642175 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -108,7 +108,6 @@ def winning_diagonal?(piece) false end - # winning_vertical? def winning_vertical?(piece) verticals.each do |column| @@ -143,6 +142,27 @@ def winning_horizontal?(piece) false end + #winning_move_available?(piece) + def winning_move_available?(piece) + + end + + #board_dup + def dup_board(board) + new_board = [] + + board.each do |row| + row_dup = [] + row.each do |slot| + row_dup.push(slot.dup) + end + new_board.push(row_dup) + end + + new_board + end + end + # diagonals def diagonals # return the diagonal pieces diff --git a/connect_four/computer_player.rb b/connect_four/computer_player.rb index 8c5c8c9..7cd790d 100644 --- a/connect_four/computer_player.rb +++ b/connect_four/computer_player.rb @@ -1,4 +1,4 @@ -class ComputerPlayer +class ComputerPlayer < Player # initialize def initialize(name = "Computer Player", piece, board) # Set marker type (e.g. X or O) @@ -10,6 +10,6 @@ def initialize(name = "Computer Player", piece, board) # ask_for_column def ask_for_column - + rand(6) + 1 end end \ No newline at end of file diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index dc4fd3a..a30b7ed 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -1,5 +1,6 @@ require_relative "board.rb" require_relative "player.rb" +require_relative "computer_player.rb" # Controls the game play class ConnectFour @@ -10,7 +11,7 @@ def initialize # set up the players @player_x = Player.new("Player 1", :X, @board) - @player_o = Player.new("Player 2", :O, @board) + @player_o = ComputerPlayer.new("Player 2", :O, @board) # assign the starting player @current_player = @player_x From 807fb1cfe0d44d22222f722117ee6bb77e24138c Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 17:52:05 -0500 Subject: [PATCH 30/37] Add winning_vertical_available? method --- connect_four/board.rb | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 8642175..0518ab2 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -125,6 +125,21 @@ def winning_vertical?(piece) false end +# winning_vertical_available?(piece) + def winning_vertical_available?(piece) + verticals.each do |column| + consecutive_pieces = 0 + column.each do |slot| + if slot == piece + consecutive_pieces += 1 + elsif slot == nil && consecutive_pieces == 3 + return true + end + end + end + false + end + # winning_horizontal? def winning_horizontal?(piece) horizontals.each do |row| @@ -142,27 +157,6 @@ def winning_horizontal?(piece) false end - #winning_move_available?(piece) - def winning_move_available?(piece) - - end - - #board_dup - def dup_board(board) - new_board = [] - - board.each do |row| - row_dup = [] - row.each do |slot| - row_dup.push(slot.dup) - end - new_board.push(row_dup) - end - - new_board - end - end - # diagonals def diagonals # return the diagonal pieces From adbe2c5354b136754e18b26054e2cf198eb8809d Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 18:16:58 -0500 Subject: [PATCH 31/37] Add slot_available? and possible_move methods --- connect_four/board.rb | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/connect_four/board.rb b/connect_four/board.rb index 0518ab2..7835a5c 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -140,6 +140,21 @@ def winning_vertical_available?(piece) false end + def winning_vertical(piece) + verticals.each_with_index do |column, column_index| + consecutive_pieces = 0 + column.each do |slot| + if slot == piece + consecutive_pieces += 1 + elsif slot == nil && consecutive_pieces == 3 + column_label = column_index + 1 + return column_label + end + end + end + false + end + # winning_horizontal? def winning_horizontal?(piece) horizontals.each do |row| @@ -254,6 +269,25 @@ def horizontals @board end + def slot_available? + if @board[row_index][column_index].nil? + true + else + false + end + + def possible_move?(column_index, row_index) + if slot_available? + if row_index == 0 + true + elsif @board[row_index - 1][column_index].nil? + false + else + true + end + end + end + # full? def full? @board.each do |row| From 7d5de01f23784a3958d88f59f418ac0f48f34081 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 18:42:10 -0500 Subject: [PATCH 32/37] Add winning_horizontal_available? method --- connect_four/board.rb | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 7835a5c..a7b74f5 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -132,8 +132,10 @@ def winning_vertical_available?(piece) column.each do |slot| if slot == piece consecutive_pieces += 1 - elsif slot == nil && consecutive_pieces == 3 + elsif slot.nil? && consecutive_pieces == 3 return true + else + consecutive_pieces = 0 end end end @@ -149,6 +151,8 @@ def winning_vertical(piece) elsif slot == nil && consecutive_pieces == 3 column_label = column_index + 1 return column_label + else + consecutive_pieces = 0 end end end @@ -172,6 +176,29 @@ def winning_horizontal?(piece) false end +# winning_horizontal_available? + def winning_horizontal_available?(piece) + horizontals.each_with_index do |row, row_index| + consecutive_pieces = 0 + + row.each_with_index do |slot, column_index| + if slot == piece + consecutive_pieces += 1 + elsif consecutive_pieces == 3 + if column_index >= 4 + column_before_sequence = column_index - 4 + slot_before_sequence = @board[row_index][column_before_sequence] + return true if slot_before_sequence.nil? + end + + return true if slot.nil? + end + end + end + + false + end + # diagonals def diagonals # return the diagonal pieces @@ -274,6 +301,7 @@ def slot_available? true else false + end end def possible_move?(column_index, row_index) From 3049a21875567a9a4b4d522bce64a80be6d71663 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 18:56:38 -0500 Subject: [PATCH 33/37] Add to ask_for_column method to make ComputerPlayer smarter --- connect_four/board.rb | 29 ++++++++++++++++++++++++++--- connect_four/computer_player.rb | 8 +++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index a7b74f5..8e5ab10 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -142,20 +142,20 @@ def winning_vertical_available?(piece) false end - def winning_vertical(piece) + def winning_vertical_move(piece) verticals.each_with_index do |column, column_index| consecutive_pieces = 0 column.each do |slot| if slot == piece consecutive_pieces += 1 elsif slot == nil && consecutive_pieces == 3 - column_label = column_index + 1 - return column_label + return column_index else consecutive_pieces = 0 end end end + false end @@ -199,6 +199,29 @@ def winning_horizontal_available?(piece) false end +# winning_horizontal_available? + def winning_horizontal_move(piece) + horizontals.each_with_index do |row, row_index| + consecutive_pieces = 0 + + row.each_with_index do |slot, column_index| + if slot == piece + consecutive_pieces += 1 + elsif consecutive_pieces == 3 + if column_index >= 4 + column_before_sequence = column_index - 4 + slot_before_sequence = @board[row_index][column_before_sequence] + return column_before_sequence if slot_before_sequence.nil? + end + + return column_index if slot.nil? + end + end + end + + false + end + # diagonals def diagonals # return the diagonal pieces diff --git a/connect_four/computer_player.rb b/connect_four/computer_player.rb index 7cd790d..8d5685e 100644 --- a/connect_four/computer_player.rb +++ b/connect_four/computer_player.rb @@ -10,6 +10,12 @@ def initialize(name = "Computer Player", piece, board) # ask_for_column def ask_for_column - rand(6) + 1 + if @board.winning_vertical_available?(piece) + @board.winning_vertical_move(piece) + 1 + elsif @board.winning_horizontal_available?(piece) + @board.winning_horizontal_move(piece) + 1 + else + rand(6) + 1 + end end end \ No newline at end of file From 2113034437932e2e48581e685ff0f5a604b0a1b5 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Wed, 8 Feb 2017 19:07:58 -0500 Subject: [PATCH 34/37] Remove notes --- connect_four/board.rb | 37 +-------------------------------- connect_four/computer_player.rb | 3 --- connect_four/connect_four.rb | 35 ++++++------------------------- connect_four/player.rb | 17 --------------- 4 files changed, 7 insertions(+), 85 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 8e5ab10..7e2c29e 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -1,17 +1,12 @@ -# Maintains game board state class Board -# initialize board def initialize - # set up blank data structure @board = Array.new(6) { Array.new(7) } end -# render def render puts - # loop through data structure + 5.downto(0) do |row| - # display an existing marker if any, else blank @board[row].each do |column| print "| " column.nil? ? print("-") : print(column) @@ -27,15 +22,12 @@ def render puts end -# add_piece def add_piece(column_label, piece) column_index = column_label - 1 - # IF move_valid?(column) if move_valid?(column_label) @board.each_with_index do |row, i| if row[column_index].nil? - #place piece @board[i][column_index] = piece return true end @@ -45,10 +37,7 @@ def add_piece(column_label, piece) end end -# move_valid?(column) def move_valid?(column_label) - # Is the column_valid? - # Is the column_available? if column_valid?(column_label) && column_available?(column_label) true else @@ -57,18 +46,14 @@ def move_valid?(column_label) end end -# column_valid?(column) def column_valid?(column_label) - # UNLESS column is in the acceptable range if column_label >= 1 && column_label <= 7 true else - # display an error message puts "Invalid move." end end -# column_available?(column) def column_available?(column_label) column(column_label).include?(nil) ? true : false end @@ -83,15 +68,10 @@ def column(column_label) column_arr end -# winning_combination?(piece) def winning_combination?(piece) -# is there a winning_diagonal? -# or winning_vertical? -# or winning_horizontal? for that piece? winning_diagonal?(piece) || winning_vertical?(piece) || winning_horizontal?(piece) end -# winning_diagonal? def winning_diagonal?(piece) diagonals.each do |diagonal| consecutive_pieces = 0 @@ -108,7 +88,6 @@ def winning_diagonal?(piece) false end -# winning_vertical? def winning_vertical?(piece) verticals.each do |column| consecutive_pieces = 0 @@ -125,7 +104,6 @@ def winning_vertical?(piece) false end -# winning_vertical_available?(piece) def winning_vertical_available?(piece) verticals.each do |column| consecutive_pieces = 0 @@ -159,7 +137,6 @@ def winning_vertical_move(piece) false end -# winning_horizontal? def winning_horizontal?(piece) horizontals.each do |row| consecutive_pieces = 0 @@ -176,7 +153,6 @@ def winning_horizontal?(piece) false end -# winning_horizontal_available? def winning_horizontal_available?(piece) horizontals.each_with_index do |row, row_index| consecutive_pieces = 0 @@ -199,7 +175,6 @@ def winning_horizontal_available?(piece) false end -# winning_horizontal_available? def winning_horizontal_move(piece) horizontals.each_with_index do |row, row_index| consecutive_pieces = 0 @@ -222,12 +197,9 @@ def winning_horizontal_move(piece) false end -# diagonals def diagonals - # return the diagonal pieces diagonals_arr = [] - # column constant at 0, row increasing by 1 3.times do |i| column_start = 0 row_start = 0 @@ -245,7 +217,6 @@ def diagonals diagonals_arr.push(diagonal) end - # row constant at 0, column increasing by 1 3.times do |i| column_start = 1 row_start = 0 @@ -263,7 +234,6 @@ def diagonals diagonals_arr.push(diagonal) end - # column constant at 6, row increasing by 1 3.times do |i| column_start = 6 row_start = 0 @@ -281,7 +251,6 @@ def diagonals diagonals_arr.push(diagonal) end - # row constant at 0, column decreasing by 1 3.times do |i| column_start = 5 row_start = 0 @@ -302,7 +271,6 @@ def diagonals diagonals_arr end -# verticals def verticals columns = [] @@ -313,9 +281,7 @@ def verticals columns end -# horizontals def horizontals -# return the horizontal rows @board end @@ -339,7 +305,6 @@ def possible_move?(column_index, row_index) end end -# full? def full? @board.each do |row| row.each do |slot| diff --git a/connect_four/computer_player.rb b/connect_four/computer_player.rb index 8d5685e..e94d140 100644 --- a/connect_four/computer_player.rb +++ b/connect_four/computer_player.rb @@ -1,14 +1,11 @@ class ComputerPlayer < Player - # initialize def initialize(name = "Computer Player", piece, board) - # Set marker type (e.g. X or O) raise "Piece must be a symbol!" unless piece.is_a?(Symbol) @name = name @piece = piece @board = board end - # ask_for_column def ask_for_column if @board.winning_vertical_available?(piece) @board.winning_vertical_move(piece) + 1 diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index a30b7ed..06b870f 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -2,65 +2,44 @@ require_relative "player.rb" require_relative "computer_player.rb" -# Controls the game play class ConnectFour - # initialize def initialize - # set up the board @board = Board.new - - # set up the players @player_x = Player.new("Player 1", :X, @board) @player_o = ComputerPlayer.new("Player 2", :O, @board) - - # assign the starting player @current_player = @player_x end - # play def play - # loop infinitely loop do - # call the board rendering method @board.render - - # ask for column from the current player @current_player.get_column - - # break the loop IF the game is over break if check_game_over - - # switch players switch_players end end - # check_game_over def check_game_over - # check_victory - # check_draw check_victory || check_draw end - # check_victory def check_victory - # IF board says current player's piece has - # a winning_combination? if @board.winning_combination?(@current_player.piece) - # display a victory message @board.render - puts "Congratulations, #{@current_player.name}! You won!" + if @current_player.is_a?(ComputerPlayer) + puts "The computer beat you!" + else + puts "Congratulations, #{@current_player.name}! You won!" + end + true else false end end - # check_draw def check_draw - # IF board says it's full if @board.full? - # display a draw message puts "It's a draw!" true else @@ -68,9 +47,7 @@ def check_draw end end - # switch_players def switch_players - # PlayerX >> PlayerO or vice versa if @current_player == @player_x @current_player = @player_o else diff --git a/connect_four/player.rb b/connect_four/player.rb index bc0a5d7..b7113a7 100644 --- a/connect_four/player.rb +++ b/connect_four/player.rb @@ -1,50 +1,33 @@ -# Manages all player-related functionality class Player attr_accessor :name, :piece - # initialize def initialize(name = "Mystery Player", piece, board) - # Set marker type (e.g. X or O) raise "Piece must be a symbol!" unless piece.is_a?(Symbol) @name = name @piece = piece @board = board end - # get_column def get_column - # loop infinitely loop do - # ask_for_column column = ask_for_column - # IF validate_column_format is true if validate_column_format(column) - # IF piece can be placed on Board - # break the loop break if @board.add_piece(column, @piece) end end end - # ask_for_column def ask_for_column - # display message asking for column puts "Please choose a column for your next move." - # # pull column choice from command line gets.strip.to_i end - -# validate_column_format def validate_column_format(column) - # UNLESS column choice is in the proper format if column >= 1 && column <= 7 true else - # display error message puts "Your choice is not in the proper format." end end - end \ No newline at end of file From 55a6b6d4ff53daa72f4ea411ef3621ff32b5c9a7 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Thu, 9 Feb 2017 16:44:12 -0500 Subject: [PATCH 35/37] Make methods private where possible --- connect_four/board.rb | 183 ++++++++++++++++---------------- connect_four/computer_player.rb | 2 + connect_four/connect_four.rb | 2 + connect_four/player.rb | 2 + 4 files changed, 99 insertions(+), 90 deletions(-) diff --git a/connect_four/board.rb b/connect_four/board.rb index 7e2c29e..66aa38e 100644 --- a/connect_four/board.rb +++ b/connect_four/board.rb @@ -22,6 +22,20 @@ def render puts end + def winning_combination?(piece) + winning_diagonal?(piece) || winning_vertical?(piece) || winning_horizontal?(piece) + end + + def full? + @board.each do |row| + row.each do |slot| + return false if slot.nil? + end + end + + true + end + def add_piece(column_label, piece) column_index = column_label - 1 @@ -37,6 +51,85 @@ def add_piece(column_label, piece) end end + def winning_vertical_available?(piece) + verticals.each do |column| + consecutive_pieces = 0 + column.each do |slot| + if slot == piece + consecutive_pieces += 1 + elsif slot.nil? && consecutive_pieces == 3 + return true + else + consecutive_pieces = 0 + end + end + end + false + end + + def winning_vertical_move(piece) + verticals.each_with_index do |column, column_index| + consecutive_pieces = 0 + column.each do |slot| + if slot == piece + consecutive_pieces += 1 + elsif slot == nil && consecutive_pieces == 3 + return column_index + else + consecutive_pieces = 0 + end + end + end + + false + end + + def winning_horizontal_available?(piece) + horizontals.each_with_index do |row, row_index| + consecutive_pieces = 0 + + row.each_with_index do |slot, column_index| + if slot == piece + consecutive_pieces += 1 + elsif consecutive_pieces == 3 + if column_index >= 4 + column_before_sequence = column_index - 4 + slot_before_sequence = @board[row_index][column_before_sequence] + return true if slot_before_sequence.nil? + end + + return true if slot.nil? + end + end + end + + false + end + + def winning_horizontal_move(piece) + horizontals.each_with_index do |row, row_index| + consecutive_pieces = 0 + + row.each_with_index do |slot, column_index| + if slot == piece + consecutive_pieces += 1 + elsif consecutive_pieces == 3 + if column_index >= 4 + column_before_sequence = column_index - 4 + slot_before_sequence = @board[row_index][column_before_sequence] + return column_before_sequence if slot_before_sequence.nil? + end + + return column_index if slot.nil? + end + end + end + + false + end + + private + def move_valid?(column_label) if column_valid?(column_label) && column_available?(column_label) true @@ -68,10 +161,6 @@ def column(column_label) column_arr end - def winning_combination?(piece) - winning_diagonal?(piece) || winning_vertical?(piece) || winning_horizontal?(piece) - end - def winning_diagonal?(piece) diagonals.each do |diagonal| consecutive_pieces = 0 @@ -104,39 +193,6 @@ def winning_vertical?(piece) false end - def winning_vertical_available?(piece) - verticals.each do |column| - consecutive_pieces = 0 - column.each do |slot| - if slot == piece - consecutive_pieces += 1 - elsif slot.nil? && consecutive_pieces == 3 - return true - else - consecutive_pieces = 0 - end - end - end - false - end - - def winning_vertical_move(piece) - verticals.each_with_index do |column, column_index| - consecutive_pieces = 0 - column.each do |slot| - if slot == piece - consecutive_pieces += 1 - elsif slot == nil && consecutive_pieces == 3 - return column_index - else - consecutive_pieces = 0 - end - end - end - - false - end - def winning_horizontal?(piece) horizontals.each do |row| consecutive_pieces = 0 @@ -153,50 +209,6 @@ def winning_horizontal?(piece) false end - def winning_horizontal_available?(piece) - horizontals.each_with_index do |row, row_index| - consecutive_pieces = 0 - - row.each_with_index do |slot, column_index| - if slot == piece - consecutive_pieces += 1 - elsif consecutive_pieces == 3 - if column_index >= 4 - column_before_sequence = column_index - 4 - slot_before_sequence = @board[row_index][column_before_sequence] - return true if slot_before_sequence.nil? - end - - return true if slot.nil? - end - end - end - - false - end - - def winning_horizontal_move(piece) - horizontals.each_with_index do |row, row_index| - consecutive_pieces = 0 - - row.each_with_index do |slot, column_index| - if slot == piece - consecutive_pieces += 1 - elsif consecutive_pieces == 3 - if column_index >= 4 - column_before_sequence = column_index - 4 - slot_before_sequence = @board[row_index][column_before_sequence] - return column_before_sequence if slot_before_sequence.nil? - end - - return column_index if slot.nil? - end - end - end - - false - end - def diagonals diagonals_arr = [] @@ -305,13 +317,4 @@ def possible_move?(column_index, row_index) end end - def full? - @board.each do |row| - row.each do |slot| - return false if slot.nil? - end - end - - true - end end \ No newline at end of file diff --git a/connect_four/computer_player.rb b/connect_four/computer_player.rb index e94d140..900c1d5 100644 --- a/connect_four/computer_player.rb +++ b/connect_four/computer_player.rb @@ -6,6 +6,8 @@ def initialize(name = "Computer Player", piece, board) @board = board end + private + def ask_for_column if @board.winning_vertical_available?(piece) @board.winning_vertical_move(piece) + 1 diff --git a/connect_four/connect_four.rb b/connect_four/connect_four.rb index 06b870f..b8a3bde 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/connect_four.rb @@ -19,6 +19,8 @@ def play end end + private + def check_game_over check_victory || check_draw end diff --git a/connect_four/player.rb b/connect_four/player.rb index b7113a7..3521dd8 100644 --- a/connect_four/player.rb +++ b/connect_four/player.rb @@ -18,6 +18,8 @@ def get_column end end + private + def ask_for_column puts "Please choose a column for your next move." gets.strip.to_i From adc09ad59b1722d4e451260aec2ef95856aff1d7 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Thu, 9 Feb 2017 18:56:28 -0500 Subject: [PATCH 36/37] Add examples to test for Board class --- connect_four/{ => lib}/board.rb | 4 +- connect_four/{ => lib}/computer_player.rb | 2 + connect_four/{ => lib}/connect_four.rb | 11 +-- connect_four/{ => lib}/player.rb | 0 connect_four/spec/board_spec.rb | 107 ++++++++++++++++++++++ connect_four/spec/computer_player_spec.rb | 5 + connect_four/spec/connect_four_spec.rb | 6 ++ connect_four/spec/player_spec.rb | 8 ++ 8 files changed, 134 insertions(+), 9 deletions(-) rename connect_four/{ => lib}/board.rb (98%) rename connect_four/{ => lib}/computer_player.rb (94%) rename connect_four/{ => lib}/connect_four.rb (88%) rename connect_four/{ => lib}/player.rb (100%) create mode 100644 connect_four/spec/board_spec.rb create mode 100644 connect_four/spec/computer_player_spec.rb create mode 100644 connect_four/spec/connect_four_spec.rb create mode 100644 connect_four/spec/player_spec.rb diff --git a/connect_four/board.rb b/connect_four/lib/board.rb similarity index 98% rename from connect_four/board.rb rename to connect_four/lib/board.rb index 66aa38e..1ec0848 100644 --- a/connect_four/board.rb +++ b/connect_four/lib/board.rb @@ -1,6 +1,6 @@ class Board - def initialize - @board = Array.new(6) { Array.new(7) } + def initialize(*board_state) + @board = board_state || Array.new(6) { Array.new(7) } end def render diff --git a/connect_four/computer_player.rb b/connect_four/lib/computer_player.rb similarity index 94% rename from connect_four/computer_player.rb rename to connect_four/lib/computer_player.rb index 900c1d5..4578a6f 100644 --- a/connect_four/computer_player.rb +++ b/connect_four/lib/computer_player.rb @@ -1,3 +1,5 @@ +require_relative 'player' + class ComputerPlayer < Player def initialize(name = "Computer Player", piece, board) raise "Piece must be a symbol!" unless piece.is_a?(Symbol) diff --git a/connect_four/connect_four.rb b/connect_four/lib/connect_four.rb similarity index 88% rename from connect_four/connect_four.rb rename to connect_four/lib/connect_four.rb index b8a3bde..4509b7b 100644 --- a/connect_four/connect_four.rb +++ b/connect_four/lib/connect_four.rb @@ -1,6 +1,6 @@ -require_relative "board.rb" -require_relative "player.rb" -require_relative "computer_player.rb" +require_relative 'board.rb' +require_relative 'player.rb' +require_relative 'computer_player.rb' class ConnectFour def initialize @@ -56,7 +56,4 @@ def switch_players @current_player = @player_x end end -end - -game = ConnectFour.new -game.play \ No newline at end of file +end \ No newline at end of file diff --git a/connect_four/player.rb b/connect_four/lib/player.rb similarity index 100% rename from connect_four/player.rb rename to connect_four/lib/player.rb diff --git a/connect_four/spec/board_spec.rb b/connect_four/spec/board_spec.rb new file mode 100644 index 0000000..e6706d6 --- /dev/null +++ b/connect_four/spec/board_spec.rb @@ -0,0 +1,107 @@ +require 'board.rb' + +describe Board do + # initialize + + # winning_combination? + describe '#winning_combination?' do + it "returns a boolean value" do + + end + + it "returns true if a winning diagonal combination exists for the given piece" do + + end + + it "returns true if a winning vertical combination exists for the given piece" do + + end + + it "returns true if a winning horizontal combination exists for the given piece" do + + end + + it "returns false if no winning combination exists for the given piece" do + + end + end + + # full? + describe '#full?' do + it "returns false if there are any empty slots" do + + end + + it "returns true if there is a piece in every slot" do + + end + end + + # add_piece + describe '#add_piece' do + it "adds the given piece to the given column" do + + end + + it "adds the given piece to the correct row in the column" do + + end + + it "returns true if the piece has been added to the board" do + + end + + it "returns false if the piece has not been added to the board" do + + end + + it "adds the piece to the column according to the column label displayed to the user" do + + end + + it "does not add the piece to the column according to the zero-based column index" do + + end + end + + # winning_vertical_available? + describe '#winning_vertical_available?' do + it "returns true if there is a move available that will result in a vertical winning combination for the given piece" do + + end + + it "does not return true if there is a move available that will result in a vertical winning combination for a different piece" do + + end + end + + # winning_vertical_move + describe '#winning_vertical_move' do + it "returns the move that will result in a vertical winning combination for the given piece" do + + end + + it "does not return the move that will result in a vertical winning combination for a different piece" do + + end + end + + # winning_horizontal_available? + it "returns true if there is a move available that will result in a horizontal winning combination for the given piece" do + + end + + it "does not return true if there is a move available that will result in a horizontal winning combination for a different piece" do + + end + + # winning_horizontal_move + it "returns the move that will result in a horizontal winning combination for the given piece" do + + end + + it "does not return the move that will result in a horizontal winning combination for a different piece" do + + end + +end \ No newline at end of file diff --git a/connect_four/spec/computer_player_spec.rb b/connect_four/spec/computer_player_spec.rb new file mode 100644 index 0000000..4f6785a --- /dev/null +++ b/connect_four/spec/computer_player_spec.rb @@ -0,0 +1,5 @@ +require 'computer_player.rb' + +describe ComputerPlayer do + # +end \ No newline at end of file diff --git a/connect_four/spec/connect_four_spec.rb b/connect_four/spec/connect_four_spec.rb new file mode 100644 index 0000000..75b266d --- /dev/null +++ b/connect_four/spec/connect_four_spec.rb @@ -0,0 +1,6 @@ +require 'connect_four.rb' + +describe ConnectFour do + # initialize + # play +end \ No newline at end of file diff --git a/connect_four/spec/player_spec.rb b/connect_four/spec/player_spec.rb new file mode 100644 index 0000000..9458601 --- /dev/null +++ b/connect_four/spec/player_spec.rb @@ -0,0 +1,8 @@ +require 'player.rb' + +describe Player do + # name + # piece + # board + # get_column +end \ No newline at end of file From 4ee4b899ac401bd8b224722c666ebdc4b5049a59 Mon Sep 17 00:00:00 2001 From: Robert Wayne Date: Thu, 9 Feb 2017 19:04:01 -0500 Subject: [PATCH 37/37] Add examples to test for Board class --- connect_four/spec/board_spec.rb | 1 - connect_four/spec/computer_player_spec.rb | 2 +- connect_four/spec/player_spec.rb | 4 +--- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/connect_four/spec/board_spec.rb b/connect_four/spec/board_spec.rb index e6706d6..466fd6a 100644 --- a/connect_four/spec/board_spec.rb +++ b/connect_four/spec/board_spec.rb @@ -103,5 +103,4 @@ it "does not return the move that will result in a horizontal winning combination for a different piece" do end - end \ No newline at end of file diff --git a/connect_four/spec/computer_player_spec.rb b/connect_four/spec/computer_player_spec.rb index 4f6785a..f85d07c 100644 --- a/connect_four/spec/computer_player_spec.rb +++ b/connect_four/spec/computer_player_spec.rb @@ -1,5 +1,5 @@ require 'computer_player.rb' describe ComputerPlayer do - # + end \ No newline at end of file diff --git a/connect_four/spec/player_spec.rb b/connect_four/spec/player_spec.rb index 9458601..5fa838e 100644 --- a/connect_four/spec/player_spec.rb +++ b/connect_four/spec/player_spec.rb @@ -1,8 +1,6 @@ require 'player.rb' describe Player do - # name - # piece - # board + # initialize # get_column end \ No newline at end of file