-
Notifications
You must be signed in to change notification settings - Fork 1
/
figure_masks.rb
87 lines (76 loc) · 1.88 KB
/
figure_masks.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
77
78
79
80
81
82
83
84
85
86
87
# Copyright (c) 2015 Sergey Tokarenko
module Tetris
module FigureMasks
BLOCKS = {
T: [
[1,1,1],
[0,1,0]
],
I: [
[1],
[1],
[1],
[1]
],
O: [
[1,1],
[1,1]
],
L: [
[1,0],
[1,0],
[1,1]
],
J: [
[0,1],
[0,1],
[1,1]
],
Z: [
[1,1,0],
[0,1,1]
],
S: [
[0,1,1],
[1,1,0]
]
}.freeze
@@figure_masks_cache = {}
class << self
def [](size)
@@figure_masks_cache[size] ||= begin
pairs = figure_patterns.map{ |figure_id, patterns|
masks = patterns.map{ |pattern|
pattern.dup.tap{ |mask|
line_patterns = mask.delete(:line_patterns)
mask[:mask] = MaskUtils.compile_line_patterns(line_patterns, size)
}
}
[figure_id, masks]
}
Hash[pairs].freeze
end
end
private
def figure_patterns
@@figure_patterns ||= begin
pairs = BLOCKS.map{ |figure_id, main_figure|
rotated_figures = [main_figure]
(1..3).each do |i|
rotated_figures[i] = rotated_figures[i-1].reverse.transpose
end
rotated_figures.uniq!
figure_rotation_patterns = rotated_figures.map{ |figure|
{
first_filled_cell: figure.first.index(1),
line_patterns: MaskUtils.line_patterns(figure)
}
}
[figure_id, figure_rotation_patterns]
}
Hash[pairs]
end
end
end
end
end