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
|
class Board
attr_accessor :current_player
def initialize
@board = [
['.', '.', '.'],
['.', '.', '.'],
['.', '.', '.']
]
end
def render
output = ''
@board.each {|row| output << row.join + "\n" }
output
end
# Raises an ArgumentError if integer conversion fails
def transform_coordinates(str)
coordinates = str.split(',')
begin
coordinates[0] = Integer(coordinates[0])
coordinates[1] = Integer(coordinates[1])
coordinates if coordinates.length > 1
rescue
end
end
def update_cell(row_index, column_index, value)
@board[row_index][column_index] = value
end
def winner?
initial = '.'
# Check horizontal
@board.each do |row|
return row[0] if array_items_equal(row) and row[0] != initial
end
# Check vertical
@board.transpose.each do |column|
return column[0] if array_items_equal(column) and column[0] != initial
end
# Check diagonals
descending = [
@board[0][0],
@board[1][1],
@board[2][2]
]
if array_items_equal(descending) and descending[0] != initial
return descending[0]
end
ascending = [
@board[2][0],
@board[1][1],
@board[0][2]
]
if array_items_equal(ascending) and ascending[0] != initial
return ascending[0]
end
nil
end
private
def array_items_equal(arr)
arr.uniq.length == 1
end
end
|