Skip to content

Commit cfd043d

Browse files
committed
updated copy command
1 parent 6582191 commit cfd043d

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

analysis_options.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ linter:
9292
- flutter_style_todos
9393
- hash_and_equals
9494
- implementation_imports
95-
- invariant_booleans
9695
- iterable_contains_unrelated_type
9796
- join_return_with_assignment
9897
- leading_newlines_in_multiline_strings

lib/src/interpreter.dart

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import "package:interpreter/src/utils/colors.dart";
88
import "package:interpreter/src/utils/errors.dart";
99
import "package:interpreter/src/utils/helper.dart";
1010
import "package:interpreter/src/utils/shape.dart";
11-
import "dart:math";
1211

1312
/// This class is a Dart
1413
/// interpreter for the CAT language.
@@ -186,7 +185,8 @@ class CATInterpreter {
186185
bool call = false;
187186
try {
188187
final int repetitions = command[1].toInt();
189-
call = _commandCaller.color(color, <dynamic>[colorsParsed, repetitions]);
188+
call =
189+
_commandCaller.color(color, <dynamic>[colorsParsed, repetitions]);
190190
} on FormatException {
191191
call = _commandCaller.color(color, <dynamic>[
192192
colorsParsed,
@@ -213,10 +213,14 @@ class CATInterpreter {
213213
splitCommands(command[0].removeSurrounding(prefix: "{", suffix: "}"));
214214
if (toExecute.isNotEmpty) {
215215
final List<String> movements = splitByCurly(command[1]);
216-
for (final String move in movements) {
216+
final List<List<String>> newCommands = _ofSetCommands(
217+
movements,
218+
toExecute,
219+
);
220+
for (int i = 0; i < movements.length; i++) {
217221
buffer
218-
..write(" go($move) ")
219-
..writeAll(toExecute, " ");
222+
..write(" go(${movements[i]}) ")
223+
..writeAll(newCommands[i], " ");
220224
}
221225
} else {
222226
final Pair<List<String>, List<String>> newDestinationsAndColors =
@@ -233,6 +237,62 @@ class CATInterpreter {
233237
_commandCaller.board.move.copyMode = false;
234238
}
235239

240+
List<List<String>> _ofSetCommands(
241+
List<String> movements,
242+
List<String> toExecute,
243+
) {
244+
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]]!);
248+
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");
276+
}
277+
newPositionsMovements.add(newPositions);
278+
}
279+
for (final List<String> i in newPositionsMovements) {
280+
int j = 0;
281+
final List<String> modifiedCommands = <String>[];
282+
for (final String k in toExecute) {
283+
if (k.startsWith("go")) {
284+
modifiedCommands.add("go(${i[j]})");
285+
j++;
286+
} else {
287+
modifiedCommands.add(k);
288+
}
289+
}
290+
ofSetCommands.add(modifiedCommands);
291+
}
292+
293+
return ofSetCommands;
294+
}
295+
236296
Pair<List<String>, List<String>> _copyCells(List<String> command) {
237297
final List<Pair<int, int>> origin = _sortCells(splitByCurly(command[0]));
238298
final List<Pair<int, int>> destination =

0 commit comments

Comments
 (0)