Skip to content

Commit 914c4f2

Browse files
committed
solved copy command test errors
1 parent cfd043d commit 914c4f2

File tree

2 files changed

+92
-58
lines changed

2 files changed

+92
-58
lines changed

lib/src/interpreter.dart

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -242,46 +242,43 @@ class CATInterpreter {
242242
List<String> toExecute,
243243
) {
244244
final List<List<String>> ofSetCommands = <List<String>>[toExecute];
245-
final String fistPosition = movements.first;
246-
final Pair<int, int> fistPositionCoordinates =
247-
Pair<int, int>(_rows[fistPosition[0]]!, _columns[fistPosition[1]]!);
245+
final List<String> fistPosition = movements.first.trim().split("");
248246
final List<Pair<int, int>> offsets = <Pair<int, int>>[];
249-
for (final String i in toExecute) {
250-
if (i.startsWith("go")) {
251-
final String el = i.replaceAll(RegExp("[go()]"), "");
252-
offsets.add(
253-
Pair<int, int>(
254-
fistPositionCoordinates.first - _rows[el[0]]!,
255-
fistPositionCoordinates.second - _columns[el[1]]!,
256-
),
257-
);
258-
}
259-
}
260-
final List<List<String>> newPositionsMovements = <List<String>>[];
261-
for (int i = 1; i < movements.length; i++) {
262-
final String position = movements[i];
263-
final Pair<int, int> positionCoordinates =
264-
Pair<int, int>(_rows[position[0]]!, _columns[position[1]]!);
265-
final List<String> newPositions = <String>[];
266-
for (final Pair<int, int> j in offsets) {
267-
final String rowPosition = _rows.keys.firstWhere(
268-
(String element) =>
269-
_rows[element] == positionCoordinates.first - j.first,
270-
);
271-
final String columnPosition = _columns.keys.firstWhere(
272-
(String element) =>
273-
_columns[element] == positionCoordinates.second - j.second,
274-
);
275-
newPositions.add("$rowPosition$columnPosition");
247+
if (_rows.containsKey(fistPosition[0]) &&
248+
_columns.containsKey(fistPosition[1])) {
249+
final Pair<int, int> fistPositionCoordinates =
250+
Pair<int, int>(_rows[fistPosition[0]]!, _columns[fistPosition[1]]!);
251+
for (final String i in toExecute) {
252+
if (i.startsWith("go")) {
253+
final List<String> el =
254+
i.replaceAll(RegExp("[go()]"), "").trim().split("");
255+
if (_rows.containsKey(el[0]) && _columns.containsKey(el[1])) {
256+
offsets.add(
257+
Pair<int, int>(
258+
fistPositionCoordinates.first - _rows[el[0]]!,
259+
fistPositionCoordinates.second - _columns[el[1]]!,
260+
),
261+
);
262+
}
263+
}
276264
}
277-
newPositionsMovements.add(newPositions);
278265
}
266+
final List<List<String>> newPositionsMovements = _newPositionsMovements(
267+
movements,
268+
offsets,
269+
);
279270
for (final List<String> i in newPositionsMovements) {
280271
int j = 0;
281272
final List<String> modifiedCommands = <String>[];
282273
for (final String k in toExecute) {
283274
if (k.startsWith("go")) {
284-
modifiedCommands.add("go(${i[j]})");
275+
final List<String> oldPosition = k.trim().split("");
276+
if (_rows.containsKey(oldPosition[0]) &&
277+
_columns.containsKey(oldPosition[1])) {
278+
modifiedCommands.add("go(${i[j]})");
279+
} else {
280+
modifiedCommands.add(k);
281+
}
285282
j++;
286283
} else {
287284
modifiedCommands.add(k);
@@ -293,6 +290,35 @@ class CATInterpreter {
293290
return ofSetCommands;
294291
}
295292

293+
List<List<String>> _newPositionsMovements(
294+
List<String> movements,
295+
List<Pair<int, int>> offsets,
296+
) {
297+
final List<List<String>> newPositionsMovements = <List<String>>[];
298+
for (int i = 1; i < movements.length; i++) {
299+
final List<String> newPositions = <String>[];
300+
final List<String> position = movements[i].trim().split("");
301+
if (_rows.containsKey(position[0]) && _columns.containsKey(position[1])) {
302+
final Pair<int, int> positionCoordinates =
303+
Pair<int, int>(_rows[position[0]]!, _columns[position[1]]!);
304+
for (final Pair<int, int> j in offsets) {
305+
final String rowPosition = _rows.keys.firstWhere(
306+
(String element) =>
307+
_rows[element] == positionCoordinates.first - j.first,
308+
);
309+
final String columnPosition = _columns.keys.firstWhere(
310+
(String element) =>
311+
_columns[element] == positionCoordinates.second - j.second,
312+
);
313+
newPositions.add("$rowPosition$columnPosition");
314+
}
315+
}
316+
newPositionsMovements.add(newPositions);
317+
}
318+
319+
return newPositionsMovements;
320+
}
321+
296322
Pair<List<String>, List<String>> _copyCells(List<String> command) {
297323
final List<Pair<int, int>> origin = _sortCells(splitByCurly(command[0]));
298324
final List<Pair<int, int>> destination =

test/interpreter_test.dart

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ void schema_2() {
116116
expect(
117117
interpreter
118118
.validateOnScheme(
119-
"PAINT({blue}, 4, square), GO(A3), PAINT({blue}, :, up), GO(right), PAINT({yellow}, :, up), GO(C5), PAINT({yellow}, 4, square)",
120-
2)
119+
"PAINT({blue}, 4, square), GO(A3), PAINT({blue}, :, up), GO(right), PAINT({yellow}, :, up), GO(C5), PAINT({yellow}, 4, square)",
120+
2)
121121
.first
122122
.completed,
123123
isTrue);
@@ -142,8 +142,8 @@ void schema_2() {
142142
expect(
143143
interpreter
144144
.validateOnScheme(
145-
"PAINT({blue}, 4, square), GO(A3), PAINT({blue}, :, up), FILL_EMPTY(yellow)",
146-
2)
145+
"PAINT({blue}, 4, square), GO(A3), PAINT({blue}, :, up), FILL_EMPTY(yellow)",
146+
2)
147147
.first
148148
.completed,
149149
isTrue);
@@ -168,8 +168,8 @@ void schema_2() {
168168
expect(
169169
interpreter
170170
.validateOnScheme(
171-
"PAINT({blue}, 2, right), GO(D1), PAINT({blue}, 2, right), GO(F3), PAINT({blue})",
172-
2)
171+
"PAINT({blue}, 2, right), GO(D1), PAINT({blue}, 2, right), GO(F3), PAINT({blue})",
172+
2)
173173
.first
174174
.completed,
175175
isFalse);
@@ -202,8 +202,8 @@ void schema_3() {
202202
expect(
203203
interpreter
204204
.validateOnScheme(
205-
"PAINT({yellow}), GO(down), PAINT({yellow}), GO(diagonal up right), PAINT({red}), GO(down), PAINT({red}), GO(F3), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(F4), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(D5), PAINT({yellow}), GO(down), PAINT({yellow}), GO(diagonal up right), PAINT({red}), GO(down), PAINT({red})",
206-
3)
205+
"PAINT({yellow}), GO(down), PAINT({yellow}), GO(diagonal up right), PAINT({red}), GO(down), PAINT({red}), GO(F3), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(F4), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(down), PAINT({red}), GO(D5), PAINT({yellow}), GO(down), PAINT({yellow}), GO(diagonal up right), PAINT({red}), GO(down), PAINT({red})",
206+
3)
207207
.first
208208
.completed,
209209
isFalse);
@@ -228,8 +228,8 @@ void schema_3() {
228228
expect(
229229
interpreter
230230
.validateOnScheme(
231-
"PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(F3), PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(D5), PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down)",
232-
3)
231+
"PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(F3), PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(D5), PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down)",
232+
3)
233233
.first
234234
.completed,
235235
isTrue);
@@ -254,8 +254,8 @@ void schema_3() {
254254
expect(
255255
interpreter
256256
.validateOnScheme(
257-
"COPY({PAINT({yellow}, :, down)}, {D1, F3, D5}) COPY({PAINT({red}, :, down)}, {D2, F4, D6})",
258-
3)
257+
"COPY({PAINT({yellow}, :, down)}, {D1, F3, D5}) COPY({PAINT({red}, :, down)}, {D2, F4, D6})",
258+
3)
259259
.first
260260
.completed,
261261
isTrue);
@@ -280,8 +280,8 @@ void schema_3() {
280280
expect(
281281
interpreter
282282
.validateOnScheme(
283-
"PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(D1), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(F3), PAINT({yellow}), GO(right), PAINT({red}), GO(diagonal down left), PAINT({yellow}), GO(right), PAINT({red}), GO(B3), PAINT({yellow}), GO(right), PAINT({red}), GO(diagonal down left), PAINT({yellow}), GO(right), PAINT({red})",
284-
3)
283+
"PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(D1), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(F3), PAINT({yellow}), GO(right), PAINT({red}), GO(diagonal down left), PAINT({yellow}), GO(right), PAINT({red}), GO(B3), PAINT({yellow}), GO(right), PAINT({red}), GO(diagonal down left), PAINT({yellow}), GO(right), PAINT({red})",
284+
3)
285285
.first
286286
.completed,
287287
isTrue);
@@ -306,8 +306,8 @@ void schema_3() {
306306
expect(
307307
interpreter
308308
.validateOnScheme(
309-
"GO(D6), PAINT({red}, :, down), GO(left), PAINT({yellow}, :, down), GO(F4), PAINT({red}, :, down), GO(left), PAINT({yellow}, :, down), GO(D2), PAINT({red}, :, down), GO(left), PAINT({yellow}, :, down)",
310-
3)
309+
"GO(D6), PAINT({red}, :, down), GO(left), PAINT({yellow}, :, down), GO(F4), PAINT({red}, :, down), GO(left), PAINT({yellow}, :, down), GO(D2), PAINT({red}, :, down), GO(left), PAINT({yellow}, :, down)",
310+
3)
311311
.first
312312
.completed,
313313
isTrue);
@@ -340,8 +340,8 @@ void schema_4() {
340340
expect(
341341
interpreter
342342
.validateOnScheme(
343-
"PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(D1), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(F3), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(F4), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow})",
344-
4)
343+
"PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(D1), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(right), PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}), GO(F3), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(down), PAINT({blue}), GO(F4), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow}), GO(down), PAINT({yellow})",
344+
4)
345345
.first
346346
.completed,
347347
isTrue);
@@ -366,8 +366,8 @@ void schema_4() {
366366
expect(
367367
interpreter
368368
.validateOnScheme(
369-
"PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(F3), PAINT({blue}, :, down), GO(right), PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(right), PAINT({blue}, :, down)",
370-
4)
369+
"PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(F3), PAINT({blue}, :, down), GO(right), PAINT({yellow}, :, down), GO(right), PAINT({red}, :, down), GO(right), PAINT({blue}, :, down)",
370+
4)
371371
.first
372372
.completed,
373373
isFalse);
@@ -392,8 +392,8 @@ void schema_4() {
392392
expect(
393393
interpreter
394394
.validateOnScheme(
395-
"COPY({PAINT({yellow}, :, up), GO(right), PAINT({red}, :, up), GO(right), PAINT({blue}, :, up)}, {C1, A4})",
396-
4)
395+
"COPY({PAINT({yellow}, :, up), GO(right), PAINT({red}, :, up), GO(right), PAINT({blue}, :, up)}, {C1, A4})",
396+
4)
397397
.first
398398
.completed,
399399
isTrue);
@@ -418,8 +418,8 @@ void schema_4() {
418418
expect(
419419
interpreter
420420
.validateOnScheme(
421-
"COPY({PAINT({yellow}, :, up), GO(right), PAINT({red}, :, up), GO(right), PAINT({blue}, :, up)}, {C1, A4})",
422-
4)
421+
"COPY({PAINT({yellow}, :, up), GO(right), PAINT({red}, :, up), GO(right), PAINT({blue}, :, up)}, {C1, A4})",
422+
4)
423423
.first
424424
.completed,
425425
isTrue);
@@ -452,8 +452,8 @@ void other_schemas() {
452452
expect(
453453
interpreter
454454
.validateOnScheme(
455-
"GO(up) PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}, 3, up), MIRROR(horizontal) MIRROR(vertical)",
456-
1)
455+
"GO(up) PAINT({yellow}), GO(right), PAINT({red}), GO(right), PAINT({blue}, 3, up), MIRROR(horizontal) MIRROR(vertical)",
456+
1)
457457
.first
458458
.completed,
459459
isTrue);
@@ -636,6 +636,14 @@ void valid_patters() {
636636
expect(response.first.completed, isFalse);
637637
expect(response.second, equals(CatError.none));
638638
});
639+
test("", () {
640+
interpreter.reset();
641+
var response = interpreter.validateOnScheme(
642+
"copy({paint({blue},:,right),go(up),paint({blue},:,right)},{c1,a3,e3})",
643+
1);
644+
expect(response.first.completed, isTrue);
645+
expect(response.second, equals(CatError.none));
646+
});
639647
});
640648
}
641649

0 commit comments

Comments
 (0)