class ConnectFourCli::Game

Attributes

board[R]
turn[R]
winner[R]

Public Class Methods

new(size, computer = false) click to toggle source
# File lib/connect_four_cli/game.rb, line 5
def initialize(size, computer = false)
  @turn = :red
  @board = Board.new(size)
  @computer = Computer.new(board) if computer
end

Public Instance Methods

computer_move() click to toggle source
# File lib/connect_four_cli/game.rb, line 19
def computer_move
  @computer.make_move
  next_turn
end
computer_turn?() click to toggle source
# File lib/connect_four_cli/game.rb, line 24
def computer_turn?
  !!@computer && @turn == :yellow
end
display() click to toggle source
# File lib/connect_four_cli/game.rb, line 43
def display
  output = []
  output << board.display
  output << game_state
  output.join("\n\n")
end
game_state() click to toggle source
# File lib/connect_four_cli/game.rb, line 50
def game_state
  if board.four_in_a_row?(:yellow)
    @winner = :yellow
    @turn = :yellow
    "Yellow wins! Press q to quit."
  elsif board.four_in_a_row?(:red)
    @winner = :red
    @turn = :red
    "Red wins! Press q to quit."
  elsif board.full?
    @winner = :draw
    "It's a draw! Press q to quit"
  else
    if computer_turn?
      "Computer's turn."
    else
      "#{@turn.capitalize}'s turn."
    end
  end
end
next_turn() click to toggle source
# File lib/connect_four_cli/game.rb, line 11
def next_turn
  if @winner
      @turn = @winner
  else
    @turn = (@turn == :red ? :yellow : :red)
  end
end
place_turn_checker(position) click to toggle source
# File lib/connect_four_cli/game.rb, line 28
def place_turn_checker(position)
  checker = Checker.new(@turn)
  if board.place_checker(checker, at: position)
    next_turn
  end
end
red_locations() click to toggle source
# File lib/connect_four_cli/game.rb, line 35
def red_locations
  board.red_locations
end
yellow_locations() click to toggle source
# File lib/connect_four_cli/game.rb, line 39
def yellow_locations
  board.yellow_locations
end