-
Notifications
You must be signed in to change notification settings - Fork 0
/
steps.rb
171 lines (161 loc) · 4.67 KB
/
steps.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
module Steps
require './strategies'
include Strategies
HORIZONTAL = [:up_mesh, :h_cycle, :down_mesh]
VERTICAL = [:left_mesh, :v_cycle, :right_mesh]
K = { 1 => :up_mesh, 2 => :h_cycle, 3 => :down_mesh}
L = { 1 => :left_mesh, 2 => :v_cycle, 3 => :right_mesh}
CLASTER_CIRCLE = {1 => [1,1], 2 => [1,2], 3 => [1,3], 4 => [2,3], 5 => [3,3], 6 => [3,2], 7 => [3,1], 8 => [2,1]}
def h_cycle
if (@from[:j] == 1 && @from[:l] == 1 && !@current_state[:forward]) || (@from[:j] == @step && @from[:l] == 3 && @current_state[:forward])
# shift to mesh
p "shift to mesh"
h_shift_to_other_place
else
test = @from.dup
test[:j] += @current_state[:forward] ? 1 : -1
test[:j] = @step if test[:j] == 0
test[:j] = 1 if test[:j] == @step+1
test[:l] = @current_state[:forward] ? 1 : 3
if broken?(test)
h_shift_to_other_place
else
@possible_variants[:tried] = []
@from = test
put_node(@from)
true
end
end
end
def v_cycle
if (@from[:i] == 1 && @from[:k] == 1 && !@current_state[:forward]) || (@from[:i] == @step && @from[:k] == 3 && @current_state[:forward])
# shift to mesh
p "shift to mesh"
v_shift_to_other_place
else
test = @from.dup
test[:i] += @current_state[:forward] ? 1 : -1
test[:i] = @step if test[:i] == 0
test[:i] = 1 if test[:i] == @step+1
test[:k] = @current_state[:forward] ? 1 : 3
if broken?(test)
v_shift_to_other_place
else
@possible_variants[:tried] = []
@from = test
put_node(@from)
true
end
end
end
def v_shift_to_other_place
@possible_variants[:tried] << @current_state[:type_v]
test = @from.dup
if @possible_variants[:tried].size == 3
@possible_variants[:tried] = []
return false unless @possible_variants[:forward_way]
@possible_variants[:forward_way] = false
@current_state[:forward] = !@current_state[:forward]
@current_state[:type_v] = L[@from[:l]]
return true
end
@current_state[:type_v] = (VERTICAL - @possible_variants[:tried]).first
p @current_state
test[:l] = L.key(@current_state[:type_v])
if build_in_claster_to(test)
next_step
else
v_shift_to_other_place
end
end
def h_shift_to_other_place
p @possible_variants
@possible_variants[:tried] << @current_state[:type_h]
test = @from.dup
if @possible_variants[:tried].size == 3
@possible_variants[:tried] = []
return false unless @possible_variants[:forward_way]
@possible_variants[:forward_way] = false
@current_state[:forward] = !@current_state[:forward]
@current_state[:type_h] = K[@from[:k]]
return true
end
@current_state[:type_h] = (HORIZONTAL - @possible_variants[:tried]).first
p @current_state
test[:k] = K.key(@current_state[:type_h])
if build_in_claster_to(test)
next_step
else
h_shift_to_other_place
end
end
def up_mesh
test = @from.dup
test[:j] += @current_state[:forward] ? 1 : -1
test[:j] = @step if test[:j] == 0
test[:j] = 1 if test[:j] == @step+1
test[:l] = @current_state[:forward] ? 1 : 3
if broken?(test)
h_shift_to_other_place
else
@possible_variants[:tried] = []
@from = test
put_node(@from)
true
end
end
def down_mesh
test = @from.dup
test[:j] += @current_state[:forward] ? 1 : -1
test[:j] = @step if test[:j] == 0
test[:j] = 1 if test[:j] == @step+1
test[:l] = @current_state[:forward] ? 1 : 3
if broken?(test)
h_shift_to_other_place
else
@possible_variants[:tried] = []
@from = test
put_node(@from)
true
end
end
def right_mesh
test = @from.dup
test[:i] += @current_state[:forward] ? 1 : -1
test[:i] = @step if test[:i] == 0
test[:i] = 1 if test[:i] == @step+1
test[:k] = @current_state[:forward] ? 1 : 3
if broken?(test)
v_shift_to_other_place
else
@possible_variants[:tried] = []
@from = test
put_node(@from)
true
end
end
def left_mesh
test = @from.dup
test[:i] += @current_state[:forward] ? 1 : -1
test[:i] = @step if test[:i] == 0
test[:i] = 1 if test[:i] == @step+1
test[:k] = @current_state[:forward] ? 1 : 3
if broken?(test)
v_shift_to_other_place
else
@possible_variants[:tried] = []
@from = test
put_node(@from)
true
end
end
def build_in_claster(to)
build_in_claster_to(to)
end
def step_in_claster
current = CLASTER_CIRCLE.key([@from[:k], @from[:l]])
nex = (current + 1) % 8
nex = 8 if nex==0
@from[:k], @from[:l] = CLASTER_CIRCLE[nex]
end
end