Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View board.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
 
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.