-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.rb
76 lines (63 loc) · 1.21 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require "Matrix"
require "pp"
class Board < Matrix
def self.construct(width,height)
Board.build(height,width) {"."}
end
def drop_coin column,sign
height.times do |y|
row = height-1-y
if self[column,row] == "."
self[column,row] = sign
break
end
end
end
def column_full? column
height.times do |row|
if self[column,row] == "."
return false
end
end
return true
end
def full?
width.times do |column|
if not column_full? column
return false
end
end
return true
end
def []=(y,x,e)
super(x,y,e)
end
def [](y,x)
super(x,y)
end
def simple_render
str = ""
self.each_with_index do |e, row, col|
str += "#{e}"
if col == width-1
str+= "\n"
end
end
str
end
def render
str = "0123456\n"
self.each_with_index do |e, row, col|
if e == 'X' then str+= "\e[32mX\e[0m" end
if e == 'O' then str+= "\e[31mO\e[0m" end
if e == '.' then str+= "." end
if col == width-1
str+= "\n"
end
end
str
end
alias_method :to_s, :render
alias_method :width, :column_count
alias_method :height, :row_count
end