-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboard.rb
More file actions
117 lines (93 loc) · 2.18 KB
/
board.rb
File metadata and controls
117 lines (93 loc) · 2.18 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
require 'matrix'
# hack - ruby Matrix is immutable by default
Matrix.send(:public, :[]=)
class Board
attr_reader :size, :matrix
def initialize(size=0)
@size = size
@matrix = new_square_matrix(size, 0)
end
def put_number(number, location)
@matrix[location.row, location.col] = number
end
def get_number(location)
@matrix.component(location.row, location.col)
end
def full?
! as_flatten_list.any?{|e| e == 0}
end
def as_flatten_list
@matrix.to_a.flatten
end
def empty_fields_count
as_flatten_list.count{|e| e == 0}
end
def put_at_random_location(number)
location = empty_locations.sample
put_number(number, location)
end
def empty_locations
result = []
0.upto(@size-1) {|row| 0.upto(@size-1) do |col|
location = Location.new(row, col); result << location if empty?(location)
end
}
return result
end
def each_vertical_pair_from_top_right
0.upto(@size-1) do |row|
(@size-1).downto(1) do |col|
yield(Location.new(row, col), Location.new(row, col-1))
end
end
end
def each_verticail_pair_from_top_left
0.upto(size-1) do |row|
(0).upto(size-2) do |col|
yield(Location.new(row, col), Location.new(row, col+1))
end
end
end
def to_s
string = "\n"
0.upto(@size-1) do |row|
0.upto(@size-1) do |col|
string += get_number(Location.new(row, col)).to_s
end
string << "\n"
end
string << "\n"
string
end
def empty?(location)
get_number(location) == 0
end
def get_row(row)
@matrix.row(row).to_a
end
def set_row(row, string)
0.upto(string.size-1) do |i|
put_number(string.scan(/\w/)[i].to_i, Location.new(row, i))
end
end
private
def new_square_matrix(size, default_element)
Matrix.rows(rows(size, default_element))
end
def rows(size, default_element)
Array.new(size, empty_row(size, default_element))
end
def empty_row(size, default_element)
Array.new(size, default_element)
end
end
class Location
attr_accessor :row, :col
def initialize(row, col)
@row = row
@col = col
end
def ==(other)
@row == other.row && @col == other.col
end
end