-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.rb
58 lines (50 loc) · 1.26 KB
/
model.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
class Game
attr_accessor :board, :view
def initialize(board=nil)
if board
@board = [[board[0],board[1],board[2]],
[board[3],board[4],board[5]],
[board[6],board[7],board[8]]]
else
@board = [['1','2','3'],
['4','5','6'],
['7','8','9']]
end
end
def update_board(position_int, symbol)
indexed = position_int - 1
row = indexed / 3
column = indexed % 3
self.board[row][column] = symbol
end
def open_spots
@board.flatten.join.gsub('X','').gsub('O','').split('')
end
def over?
winner = row_finished(@board)
winner ||= row_finished(@board.transpose)
winner ||= diag_finished(@board)
winner ||= tied
end
def row_finished(board)
board.each do |row|
single_x_or_o = row.uniq.join
if single_x_or_o == 'X' || single_x_or_o == 'O'
return single_x_or_o
end
end
nil
end
def diag_finished(board)
down_right = [board[0][0],board[1][1],board[2][2]]
down_left = [board[2][0],board[1][1],board[0][2]]
diags = [down_left, down_right]
row_finished(diags)
end
def tied
return 'TIE' if self.open_spots == []
end
def last_pos
return self.open_spots.first if self.open_spots.count == 1
end
end