-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.rb
More file actions
77 lines (60 loc) · 1.94 KB
/
board.rb
File metadata and controls
77 lines (60 loc) · 1.94 KB
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
#contains functions for making a move, stores board as object inside itself, breaks down input
class Board
def initialize(board = [[" "," "," "],[" "," "," "],[" "," "," "]])
@board = board
end
def board_getter
@board
end
def valid_move?(position)
character_length = position.length
row = row_finder(position) #use row finder to give row on board
col = col_finder(position) #use col finder to give col on board
if row == false || col == false || character_length > 2
return false
elsif @board[row][col] == "X" or @board[row][col] == "O"
return false
else
return true
end
end
def move(position, counter)
if position == ""
return @board
end
row = row_finder(position)
col = col_finder(position)
if @board[row][col] == " " && counter.even?
@board[row][col] = "X"
return @board
elsif @board[row][col] == " " && counter.odd?
@board[row][col] = "O"
return @board
end
# A, B, C are rows represented by each subarray
# 1, 2, 3 are columns represented by each position in the subarray
end
def row_finder(position)
row_position = position[0,1].capitalize.to_sym
row = {:A => 0, :B => 1, :C => 2}
if row.key?(row_position) == false
return false
end
row[row_position]
end
def col_finder(position)
col_position = (position[1,1].to_i) - 1
if col_position.between?(0,2)
return col_position
else
return false
end
end
def print_board()
puts "\n"
@board.each do |r|
puts r.each { |p| p }.join("|")
puts "-----"
end
end
end