-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsquare.rb
More file actions
78 lines (66 loc) · 1.21 KB
/
square.rb
File metadata and controls
78 lines (66 loc) · 1.21 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
75
76
77
78
class Square
attr_accessor :type, :position
def initialize(type, position)
@type = type
@position = position
end
end
class Type
def initialize(x_or_o)
@type = x_or_o
end
def x?
@type == 'X'
end
def o?
@type == 'O'
end
def to_s
@type
end
def to_i
@type == 'X' ? 1 : 0
end
end
class Position
attr_reader :row, :col
def initialize(row, col)
if %w(top middle bottom).include?(row)
@row = row
end
if %w(left center right).include?(col)
@col = col
end
raise ArgumentError if @col.nil? || @row.nil?
end
def row_i
if @row == 'top'
0
elsif @row == 'middle'
1
elsif @row == 'bottom'
2
end
end
def col_i
if @col == 'left'
0
elsif @col == 'center'
1
elsif @col == 'right'
2
end
end
def left_diaganal?
yield if @row == 'top' && @col == 'left' || @row == 'middle' && @col == 'center' || @row == 'bottom' && @col == 'right'
end
def right_diaganal?
yield if @row == 'top' && @col == 'right' || @row == 'middle' && @col == 'center' || @row == 'bottom' && @col == 'left'
end
def to_a
[@row, @col]
end
def to_s
@row.to_s + ', ' + @col.to_s
end
end