-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.rb
73 lines (67 loc) · 1.39 KB
/
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
require 'colorize'
class Board
def initialize
@board = [" ", " ", " ", " ", " ", " ", " ", " ", " ",]
end
WIN_COMBOS = [
[0, 1, 2], # horizontals
[3, 4, 5],
[6, 7, 8],
[0, 3, 6], # verticals
[1, 4, 7],
[2, 5, 8],
[0, 4, 8], # diagonals
[2, 4, 6]
]
def display_board
pretty_board = pretty_up_board(@board)
board = <<-BOARD
-------------
| #{pretty_board[0]} | #{pretty_board[1]} | #{pretty_board[2]} |
-------------
| #{pretty_board[3]} | #{pretty_board[4]} | #{pretty_board[5]} |
-------------
| #{pretty_board[6]} | #{pretty_board[7]} | #{pretty_board[8]} |
-------------
BOARD
puts board
end
def place_piece(cell, piece)
if cell_available?(cell)
@board[cell] = piece
else
false
end
end
def winner?(player) #replace with player.piece once its built
WIN_COMBOS.any? do |combo|
combo.all? { |cell| @board[cell] == player.piece }
end
end
def draw?
if @board.include?(" ")
false
else
true
end
end
private
def pretty_up_board(board)
board.each_with_index.map do |cell, index|
if cell == " "
index
elsif cell == "x"
cell.colorize(:blue)
else
cell.colorize(:red)
end
end
end
def cell_available?(cell)
if @board[cell] == " "
true
else
false
end
end
end