Skip to content

Instantly share code, notes, and snippets.

@cmar
Created December 10, 2013 00:56
Show Gist options
  • Save cmar/7884029 to your computer and use it in GitHub Desktop.
Save cmar/7884029 to your computer and use it in GitHub Desktop.
Connect 4 from RubyLoCo Hack NIght
require 'pry'
class Board
def initialize
@columns = []
7.times do
@columns << Array.new(6, '_')
end
end
def drop(color, col)
col = col - 1
rows = @columns[col]
next_cell = rows.rindex { |x| x == "_" }
if next_cell
rows[next_cell] = color
else
puts 'NO MORE ROOM'
end
end
def check
@columns.each do |row|
# if row[5] == row[4] and row[4] == row[3] and row[3] == row[2]
# puts 'WINNER'
# end
end
end
def inspect
6.times do |row|
7.times do |col|
printf '%s ', @columns[col][row]
end
puts
end
end
end
board = Board.new
puts '*' * 100
puts 'Play Connect 4'
puts '*' * 100
color = 'R'
while true
print "#{color}:"
col = gets
col = col.to_i
unless col > 0 and col < 8
puts 'col must be 1-7'
next
end
board.drop(color, col.to_i)
puts board.check
board.inspect
if color == 'R'
color = 'B'
else
color = 'R'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment