-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.rb
57 lines (49 loc) · 1.57 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
class Board
attr_accessor :board
def initialize
# @empty_square = ""
@board = Array.new(9, "")
end
def board_positions()
new_board = []
board.each_with_index do |value, index|
if value == "x" || value == "o"
new_board.push(value)
else
new_board.push(index + 1)
end
end
new_board
end
def update(position, symbol) #this updates the space on the board.
# @board[position] == @empty_square
@board[position.to_i] = symbol
end
def valid_space?(position)
if @board[position.to_i] != ""
false
else
true
end
end
def valid_input?(position)
if position.to_i >= 1 && position.to_i <= 9 && position =~ (/\d/)
true
else
false
end
end
def full_board?
board.count("") == 0
end
def winner?(symbol)
board[0] == symbol && board[1] == symbol && board[2] == symbol ||
board[3] == symbol && board[4] == symbol && board[5] == symbol ||
board[6] == symbol && board[7] == symbol && board[8] == symbol ||
board[0] == symbol && board[3] == symbol && board[6] == symbol ||
board[1] == symbol && board[4] == symbol && board[7] == symbol ||
board[2] == symbol && board[5] == symbol && board[8] == symbol ||
board[0] == symbol && board[4] == symbol && board[8] == symbol ||
board[2] == symbol && board[4] == symbol && board[6] == symbol
end
end