-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangleFormation.kts
191 lines (174 loc) · 4.79 KB
/
TriangleFormation.kts
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/** Single-robot triangle formation algorithm from https://doi.org/10.1007/s11047-019-09774-2 */
fun getRobot(
node: Node,
orientation: Int,
): Robot {
return RobotImpl(node, orientation)
}
private enum class Phase {
FindBlockTile,
MoveBlockTile,
PlaceVertex,
CarryTileToVertex,
BuildTriangle,
BuildNewColumn,
ReturnToVertex,
Finished,
}
class RobotImpl(node: Node, orientation: Int) :
Robot(
node = node,
orientation = orientation,
carriesTile = false,
numPebbles = 0,
maxPebbles = 0,
) {
private var phase = Phase.FindBlockTile
private var testActive = false
private var numMovementsSW = 0
private var southernmost = false
override fun activate() {
when (phase) {
Phase.FindBlockTile -> findBlockTile()
Phase.MoveBlockTile -> moveBlockTile()
Phase.PlaceVertex -> placeVertex()
Phase.CarryTileToVertex -> carryTileToVertex()
Phase.BuildTriangle -> buildTriangle()
Phase.BuildNewColumn -> buildNewColumn()
Phase.ReturnToVertex -> returnToVertex()
Phase.Finished -> return
}
}
override fun finished() = phase == Phase.Finished
override fun getColor(): Color {
return when (phase) {
Phase.FindBlockTile -> Color.ORANGE
Phase.MoveBlockTile -> Color.TEAL
Phase.PlaceVertex -> Color.SKY
Phase.CarryTileToVertex -> Color.BLUE
Phase.BuildTriangle -> Color.SCARLET
Phase.BuildNewColumn -> Color.BROWN
Phase.ReturnToVertex -> Color.YELLOW
Phase.Finished -> Color.BLACK
}
}
private fun findBlockTile() {
intArrayOf(5, 4, 0).firstOrNull(::hasTileAtLabel)?.let { label ->
moveToLabel(label)
if (label == 4) {
numMovementsSW++
}
return
}
liftTile()
if (!hasTileAtLabel(1) && !testActive) {
testActive = true
southernmost = false
} else if (!hasTileAtLabel(3)) {
southernmost = true
}
phase = Phase.MoveBlockTile
}
private fun moveBlockTile() {
moveToLabel(2)
if (
(hasTileAtLabel(1) && !hasTileAtLabel(0)) || (hasTileAtLabel(3) && !hasTileAtLabel(4))
) {
testActive = false
}
if (!isOnTile()) {
placeTile()
if (hasTileAtLabel(2) || numMovementsSW > 1) {
testActive = false
} else if (testActive && southernmost) {
phase = Phase.PlaceVertex
return
}
numMovementsSW = 0
phase = Phase.FindBlockTile
return
}
}
private fun placeVertex() {
if (!carriesTile) {
liftTile()
if (!hasTileAtLabel(5)) {
moveToLabel(0)
}
return
}
if (hasTileAtLabel(5)) {
moveToLabel(5)
return
}
moveToLabel(4)
placeTile()
phase = Phase.CarryTileToVertex
}
private fun carryTileToVertex() {
if (!carriesTile) {
if (hasTileAtLabel(1) && !hasTileAtLabel(0)) {
moveToLabel(1)
return
}
if (!hasTileAtLabel(5) && hasTileAtLabel(0)) {
moveToLabel(0)
return
}
if (hasTileAtLabel(2)) {
moveToLabel(2)
return
}
liftTile()
return
}
if (hasTileAtLabel(5)) {
moveToLabel(5)
return
}
if (hasTileAtLabel(3)) {
moveToLabel(3)
return
}
moveToLabel(4)
phase = Phase.BuildTriangle
}
private fun buildTriangle() {
intArrayOf(5, 3).firstOrNull(::hasTileAtLabel)?.let { label ->
moveToLabel(label)
return
}
if (hasTileAtLabel(2)) {
moveToLabel(3)
placeTile()
phase = Phase.ReturnToVertex
return
}
moveToLabel(5)
phase = Phase.BuildNewColumn
}
private fun buildNewColumn() {
if (hasTileAtLabel(1)) {
moveToLabel(0)
return
}
placeTile()
phase = Phase.ReturnToVertex
}
private fun returnToVertex() {
if (hasTileAtLabel(0) && !hasTileAtLabel(5)) {
moveToLabel(0)
return
}
if (hasTileAtLabel(2)) {
moveToLabel(2)
return
}
phase =
if (hasTileAtLabel(1)) {
Phase.CarryTileToVertex
} else {
Phase.Finished
}
}
}