class Game

Attributes

board[RW]

This class initialized instances of all the other classes, contains the main game loop, and handles IO.

current_player[RW]

This class initialized instances of all the other classes, contains the main game loop, and handles IO.

other_player[RW]

This class initialized instances of all the other classes, contains the main game loop, and handles IO.

player1[RW]

This class initialized instances of all the other classes, contains the main game loop, and handles IO.

player2[RW]

This class initialized instances of all the other classes, contains the main game loop, and handles IO.

Public Class Methods

new() click to toggle source
# File lib/connect_four.rb, line 97
def initialize
        @board = Board.new #initialize the board, along with all the cells
        @player1 = Player.new #initialize player 1
        @player2 = Player.new #initialize player 2
        @current_player = who_goes_first #this var will keep track of whose turn it is.
        @other_player = other_player
        @moves_remaining = 42 #counts down the number of moves remaining
        @game_over = false #
        @winner = nil
end

Public Instance Methods

buffer_space() click to toggle source
# File lib/connect_four.rb, line 173
def buffer_space
        puts "\n\n\n\n"
end
check_for_winner() click to toggle source
# File lib/connect_four.rb, line 207
def check_for_winner
        #For each cell in current player's moves array (i.e. the cells on which they've made moves),
        #perform searches on each search path in that cell's list of valid search_paths.
        @current_player.moves.each do |move|
                #perform searches on each search_path
                move.search_paths.each do |path|
                        #initiate a counter at 1. Travel along the given search path and increment the counter if the next cell
                        #in the path is in that player's moves array.
                        counter = 1
                        for i in 1..3
                                if @current_player.moves.any?{|x| x==@board.offset(move,
                                                                                                                                   i*(@board.path_coords[path][0]),
                                                                                                                                   i*(@board.path_coords[path][1]))}
                                        counter+=1
                                else
                                end
                                if counter==4
                                        @game_over = true
                                        @winner = @current_player
                                        break
                                else
                                end
                        end
                end
        end

        #check for a draw
        if (@game_over==false) && (@player1.moves.length+@player2.moves.length==42)
                @game_over = true
        end
end
game_setup() click to toggle source
# File lib/connect_four.rb, line 131
        def game_setup
                puts <<-eos
                
                WELCOME TO RUBY CONNECT FOUR


                                eos

                print "\nPlayer 1, please enter your name: "
                @player1.name = gets.chomp
                print "\n\nPlayer 2, please enter your name: "
                @player2.name = gets.chomp

                set_player_symbols #randomly sets their symbols

                puts <<-eos

By luck of the draw, #{@current_player.name} will go first, and their symbol is #{@current_player.symbol}.
#{@other_player.name} will go second, and their symbol is #{@other_player.symbol}.

                                eos
        end
play() click to toggle source
# File lib/connect_four.rb, line 239
def play
        buffer_space #put blank space at the start
        game_setup
        until @game_over #main game loop, continues until the board reports a winner, which it checks for after every move.
        show_board
        turn
        buffer_space
        end
        show_board
        if @winner==nil
                puts "The game is a draw!"
        else
        puts "Congratulations! #{@winner.name} is the winner. Bye bye!"
        end
end
set_player_symbols() click to toggle source
# File lib/connect_four.rb, line 108
def set_player_symbols
        @player1.symbol = rand>0.5 ? "X".red : "O".blue  #randomly assigns player 1 red X's or blue O's as their symbol
        @player2.symbol = (@player1.symbol == "X".red) ? "O".blue : "X".red #assigns player 2 the other symbol.
end
show_board() click to toggle source
# File lib/connect_four.rb, line 154
        def show_board #prints a heredoc showing the board.
                puts <<-eos
   ===============================
   || #{state("A1")} | #{state("A2")} | #{state("A3")} | #{state("A4")} | #{state("A5")} | #{state("A6")} | #{state("A7")} ||
   -------------------------------
   || #{state("B1")} | #{state("B2")} | #{state("B3")} | #{state("B4")} | #{state("B5")} | #{state("B6")} | #{state("B7")} ||
   -------------------------------
   || #{state("C1")} | #{state("C2")} | #{state("C3")} | #{state("C4")} | #{state("C5")} | #{state("C6")} | #{state("C7")} ||
   -------------------------------
   || #{state("D1")} | #{state("D2")} | #{state("D3")} | #{state("D4")} | #{state("D5")} | #{state("D6")} | #{state("D7")} ||
   -------------------------------
   || #{state("E1")} | #{state("E2")} | #{state("E3")} | #{state("E4")} | #{state("E5")} | #{state("E6")} | #{state("E7")} ||
   -------------------------------
   || #{state("F1")} | #{state("F2")} | #{state("F3")} | #{state("F4")} | #{state("F5")} | #{state("F6")} | #{state("F7")} ||
   ===============================
      1   2   3   4   5   6   7   
                eos
        end
state(a1) click to toggle source
# File lib/connect_four.rb, line 127
def state(a1)
        @board.cell(a1).state
end
switch_current_player() click to toggle source
# File lib/connect_four.rb, line 121
def switch_current_player
        buffer = @current_player
        @current_player = @other_player
        @other_player = buffer
end
turn() click to toggle source
# File lib/connect_four.rb, line 177
def turn
        #the procedure every turn
        print "It is #{@current_player.name}'s turn. Make your move by entering the desired column number: "
        input = gets.chomp.to_i #get the move as a string
        validate_turn(input)
        check_for_winner
        switch_current_player
end
validate_turn(column) click to toggle source
# File lib/connect_four.rb, line 186
def validate_turn(column) #checks to see if the move is in bounds and on a vacant cell. If not, asks for another.
        if (1..7).member?(column)
                        for i in 0..5
                        if (i==5)&&(@board.cell(0,column-1).state!=" ")
                                print "Oops, that column is full! Pick a different column: "
                                input = gets.chomp.to_i
                                validate_turn(input)
                        elsif @board.cell(5-i,column-1).state == " "
                                @board.cell(5-i,column-1).state = @current_player.symbol #places their symbol in that cell
                                @current_player.add_move(@board.cell(5-i,column-1)) #adds that cell into their moves array
                                break
                        else        
                        end
                end                  
        else
                print "Uh-oh! Please enter a valid column number:"
                input = gets.chomp.to_i
                validate_turn(input) 
        end
end
who_goes_first() click to toggle source
# File lib/connect_four.rb, line 113
def who_goes_first
        rand>0.5 ? @player1 : @player2 #randomly decides who will go first
end