Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
class Board
BLACK = :b
RED = :r
WIDTH = 7
HEIGHT = 6
def initialize()
@board = []
@current_player = BLACK
WIDTH.times do |x|
@board[x] = []
end
end
def render()
HEIGHT.times do |y|
WIDTH.times do |x|
print(@board[x][HEIGHT-y-1] || '.')
end
puts
end
end
def y_value()
@board[@last_move].size - 1
end
def won?
def template(func)
[].tap do |vector|
(1..3).each do |i|
vector.push *func.call(i)
end
end
end
def horizontal(i)
[ [@last_move-i, y_value], [@last_move+i, y_value] ]
end
def vertical(i)
[ [@last_move, y_value-i], [@last_move, y_value+i] ]
end
def backslash(i)
[ [@last_move-i, y_value+i], [@last_move+i, y_value-i] ]
end
def forwardslash(i)
[ [@last_move-i, y_value-i], [@last_move+i, y_value+i] ]
end
[:horizontal, :vertical, :backslash, :forwardslash].any? do |x|
check_winnig_vector(template(method(x)))
end
end
def check_winnig_vector(vector_positions)
current_color = @board[@last_move].last
vector_length = 1
vector_positions.each do |x, y|
if (@board[x][y] == current_color && x >= 0 && x < WIDTH)
vector_length += 1
end
return true if vector_length >= 4
end
return false
end
def moves
(0...WIDTH).reject { |x| @board[x].size >= HEIGHT}
end
def move(position)
@board[position].push(player_to_move)
@current_player = @current_player == BLACK ? RED : BLACK
@last_move = position
end
def player_to_move
@current_player
end
def self.starting_board
end
end
describe Board do
it "it should return all possible moves on an empty board " do
board = Board.new()
board.moves.should be == [0, 1, 2, 3, 4, 5, 6]
end
it "it should exclude a column if that column is full" do
board = Board.new()
6.times do |x|
board.move(0)
end
board.moves.should be == [1, 2, 3, 4, 5, 6]
end
it "it should not have any moves if all columns are full" do
board = Board.new()
7.times do |y|
6.times do |x|
board.move(y)
end
end
board.moves.should be == []
end
it "should cycle between black and red" do
board = Board.new
board.player_to_move.should be Board::BLACK
board.move(0)
board.player_to_move.should be Board::RED
end
it "should return false on an empty board" do
end
it "should return true if black wins horizontally" do
board = Board.new
board.move(0)
board.move(6) # red
board.move(1)
board.move(5) # red
board.move(2)
board.move(6) # red
board.move(3)
board.won?.should be == true
end
it "should return true if black wins vertically" do
board = Board.new
board.move(0)
board.move(6) # red
board.move(0)
board.move(6) # red
board.move(0)
board.move(6) # red
board.move(0)
board.won?.should be == true
end
it "should return true if black wins via blackslash vector" do
board = Board.new
board.move(6)
board.move(5) # red
board.move(5)
board.move(4) # red
board.move(3)
board.move(4) # red
board.move(4)
board.move(3) # red
board.move(2)
board.move(3) # red
board.move(3)
board.won?.should be == true
end
it "should return true if black wins via forwardslash vector" do
board = Board.new
board.move(0)
board.move(1) # red
board.move(1)
board.move(2) # red
board.move(3)
board.move(2) # red
board.move(2)
board.move(3) # red
board.move(4)
board.move(3) # red
board.move(3)
board.won?.should be == true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.