include Readline

def play()
  # create a window with a yellow background
  Canvas.init(175, 150, "ConnectFour")
  Canvas::Rectangle.new(0, 0, 175, 150, :fill=>:yellow)

  # initialize and draw a blank board
  board = new_board()
  display_board(board)
  player = 0

  42.times {

    prompt = "Player " + player.to_s + ": Which column (0-6)? "
    row = nil

    # Keep on asking for an input until the user gives a
    # column number onto which a disc can be placed.
    # Place the disc at the correct row in that column.
    while row == nil do
      input = readline(prompt)
      return if input.match("exit")

      # Determine whether input is a number by checking if it matches
      # a regular expression.  The ^ says that the regular expression
      # has to be at the beginning of the input. The rest of the regular
      # expression checks to see if the first character is either the 
      # numeral 0, 1, 2, 3, 4, 5, or 6
      is_number = input.match("^(0|1|2|3|4|5|6)") 
      column = input.to_i

      if !(is_number && column >= 0 && column <= 6) then
        puts "input must be a number from 0 to 6 or the word 'exit'"
      else
        row = add_piece(board, player, column)
        if row == nil then
          puts "column " + column.to_s + " already full"
        end
      end
    end

    display_board(board)

    if check_win(board,row,column) then
      puts "Player " + player.to_s + " won!"
      return
    end

    player = (player + 1) % 2

  }

  puts "The game ended in a tie."

end

def check_win(board, row, column)
  return true if check_win_vertical(board, row, column)
  return true if check_win_horizontal(board, row, column)
  return true if check_win_diagonal1(board, row, column)
  return true if check_win_diagonal2(board, row, column)
  return false
end

# INSTRUCTIONS:
# DO NOT CHANGE ANY CODE GIVEN ABOVE.
# Complete the required functions below based on the
# information given in the assignment.
# Remove the code stub we give you and replace it with
# the required code based on the assignment description.
# Do NOT remove more than one stub at a time. 
# Test each function you write before you move on to the 
# next function.

def new_board()
  return nil    # stub: remove this line when you write this function
end

def display_board(board)
  return        # stub: remove this line when you write this function
end

def add_piece(board, player, column)
  return nil    # stub: remove this line when you write this function
end

def check_win_vertical(board, row, column)
  return false  # stub: remove this line when you write this function
end

def check_win_horizontal(board, row, column)
  return false  # stub: remove this line when you write this function
end

def check_win_diagonal1(board, row, column)
  return false  # stub: remove this line when you write this function
end

def check_win_diagonal2(board, row, column)
  return false  # stub: remove this line when you write this function
end

