Skip to content

Commit b3ea305

Browse files
committed
Command modifications for documentation
1 parent 4612b06 commit b3ea305

File tree

1 file changed

+71
-43
lines changed

1 file changed

+71
-43
lines changed

lib/src/interpreter.dart

Lines changed: 71 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,47 @@ class CATInterpreter {
200200
_commandCaller.board.move.toPosition(row, column);
201201
}
202202

203+
/// It takes a list of commands and a list of positions where to copy the
204+
/// commands.
205+
/// Args:
206+
/// commands (List<String>): The commands to execute
207+
/// positions (List<String>): The positions where tto repeat the commands
208+
void repeatCommands(List<String> commands, List<String> positions) {
209+
final StringBuffer buffer = StringBuffer();
210+
final List<String> movements = positions;
211+
final List<List<String>> newCommands = _ofSetCommands(
212+
movements,
213+
commands,
214+
);
215+
for (int i = 0; i < movements.length; i++) {
216+
buffer
217+
..write(" go(${movements[i]}) ")
218+
..writeAll(newCommands[i], " ");
219+
}
220+
_parse(buffer.toString(), false);
221+
_commandCaller.board.move.copyMode = false;
222+
}
223+
224+
/// It takes a list of origin cells and a list of destination cells,
225+
/// the color of the origin cells is copied to the destination cells.
226+
/// Args:
227+
/// origin (List<String>): Origin cells from where to copy the colors
228+
/// destinations (List<String>): Destination cells
229+
void copyCells(List<String> origin, List<String> destination) {
230+
final StringBuffer buffer = StringBuffer();
231+
final Pair<List<String>, List<String>> newDestinationsAndColors =
232+
_copyCells(origin, destination);
233+
final List<String> newDestinations = newDestinationsAndColors.first;
234+
final List<String> colors = newDestinationsAndColors.second;
235+
for (int i = 0; i < newDestinations.length; i++) {
236+
buffer
237+
..write(" go(${newDestinations[i]}) ")
238+
..write(" paint(${colors[i]}) ");
239+
}
240+
_parse(buffer.toString(), false);
241+
_commandCaller.board.move.copyMode = false;
242+
}
243+
203244
/// It takes a list of commands, splits them by curly braces,
204245
/// and then executes the first command
205246
/// for each of the second command's values.
@@ -208,33 +249,16 @@ class CATInterpreter {
208249
/// command (List<String>): The command that was passed in.
209250
void _copy(List<String> command) {
210251
_commandCaller.board.move.copyMode = true;
211-
final StringBuffer buffer = StringBuffer();
212252
final List<String> toExecute =
213253
splitCommands(command[0].removeSurrounding(prefix: "{", suffix: "}"));
214254
if (toExecute.isNotEmpty) {
215-
final List<String> movements = splitByCurly(command[1]);
216-
final List<List<String>> newCommands = _ofSetCommands(
217-
movements,
218-
toExecute,
219-
);
220-
for (int i = 0; i < movements.length; i++) {
221-
buffer
222-
..write(" go(${movements[i]}) ")
223-
..writeAll(newCommands[i], " ");
224-
}
255+
repeatCommands(toExecute, splitByCurly(command[1]));
225256
} else {
226-
final Pair<List<String>, List<String>> newDestinationsAndColors =
227-
_copyCells(command);
228-
final List<String> newDestinations = newDestinationsAndColors.first;
229-
final List<String> colors = newDestinationsAndColors.second;
230-
for (int i = 0; i < newDestinations.length; i++) {
231-
buffer
232-
..write(" go(${newDestinations[i]}) ")
233-
..write(" paint(${colors[i]}) ");
234-
}
257+
final List<String> origin = splitByCurly(command[0]);
258+
final List<String> destination =
259+
splitByCurly(command[1]);
260+
copyCells(origin, destination);
235261
}
236-
_parse(buffer.toString(), false);
237-
_commandCaller.board.move.copyMode = false;
238262
}
239263

240264
List<List<String>> _ofSetCommands(
@@ -272,7 +296,8 @@ class CATInterpreter {
272296
final List<String> modifiedCommands = <String>[];
273297
for (final String k in toExecute) {
274298
if (k.startsWith("go")) {
275-
final List<String> oldPosition = k.replaceAll(RegExp("[go()]"), "").trim().split("");
299+
final List<String> oldPosition =
300+
k.replaceAll(RegExp("[go()]"), "").trim().split("");
276301
if (_rows.containsKey(oldPosition[0]) &&
277302
_columns.containsKey(oldPosition[1])) {
278303
modifiedCommands.add("go(${i[j]})");
@@ -319,18 +344,21 @@ class CATInterpreter {
319344
return newPositionsMovements;
320345
}
321346

322-
Pair<List<String>, List<String>> _copyCells(List<String> command) {
323-
final List<Pair<int, int>> origin = _sortCells(splitByCurly(command[0]));
324-
final List<Pair<int, int>> destination =
325-
_sortCells(splitByCurly(command[1]));
347+
Pair<List<String>, List<String>> _copyCells(
348+
List<String> origin,
349+
List<String> destination,
350+
) {
351+
final List<Pair<int, int>> originLocal = _sortCells(origin);
352+
final List<Pair<int, int>> destinationLocal =
353+
_sortCells(destination);
326354
final List<String> newDestinations = <String>[];
327355
final List<String> colors = <String>[];
328-
for (final Pair<int, int> i in destination) {
329-
for (final Pair<int, int> j in origin) {
330-
final int row =
331-
(j.first + (i.first - j.first)) + (j.first - origin.first.first);
356+
for (final Pair<int, int> i in destinationLocal) {
357+
for (final Pair<int, int> j in originLocal) {
358+
final int row = (j.first + (i.first - j.first)) +
359+
(j.first - originLocal.first.first);
332360
final int column = (j.second + (i.second - j.second)) +
333-
(j.second - origin.first.second);
361+
(j.second - originLocal.first.second);
334362
final Iterable<String> rowKeys =
335363
_rows.filterValues((int p0) => p0 == row).keys;
336364
final String columnKeys = "${column + 1}";
@@ -362,7 +390,7 @@ class CATInterpreter {
362390
///
363391
/// Args:
364392
/// direction (String): The direction to mirror the board.
365-
void _mirrorBoard(String direction) {
393+
void mirrorBoard(String direction) {
366394
switch (direction) {
367395
case "horizontal":
368396
{
@@ -389,7 +417,7 @@ class CATInterpreter {
389417
/// Args:
390418
/// cells (List<String>): A list of cells to mirror.
391419
/// direction (String): The direction to mirror the cells in.
392-
void _mirrorCells(List<String> cells, String direction) {
420+
void mirrorCells(List<String> cells, String direction) {
393421
switch (direction) {
394422
case "horizontal":
395423
{
@@ -433,7 +461,7 @@ class CATInterpreter {
433461
/// Args:
434462
/// commands (List<String>): The list of commands to mirror.
435463
/// direction (String): The direction to mirror the board.
436-
void _mirrorCommands(List<String> commands, String direction) {
464+
void mirrorCommands(List<String> commands, String direction) {
437465
final CommandCaller newCaller = CommandCaller(shape);
438466
final CommandCaller oldCaller = _commandCaller;
439467
_commandCaller = newCaller;
@@ -442,7 +470,7 @@ class CATInterpreter {
442470
oldCaller.board.move.column,
443471
);
444472
_parse(commands.join(","), false);
445-
_mirrorBoard(direction);
473+
mirrorBoard(direction);
446474
oldCaller.board.joinBoards(_commandCaller.board.getCross);
447475
_commandCaller = oldCaller;
448476
}
@@ -455,7 +483,7 @@ class CATInterpreter {
455483
/// command (List<String>): The command that was entered by the user.
456484
void _mirror(List<String> command) {
457485
if (command.length == 1) {
458-
_mirrorBoard(command.last);
486+
mirrorBoard(command.last);
459487

460488
return;
461489
}
@@ -470,11 +498,11 @@ class CATInterpreter {
470498
}
471499
}
472500
if (isCell) {
473-
_mirrorCells(toEvaluate, command.last);
501+
mirrorCells(toEvaluate, command.last);
474502

475503
return;
476504
} else {
477-
_mirrorCommands(toEvaluate, command.last);
505+
mirrorCommands(toEvaluate, command.last);
478506

479507
return;
480508
}
@@ -502,12 +530,12 @@ class CATInterpreter {
502530
/// and the index of the color.
503531
///
504532
/// Args:
505-
/// el (List<String>): The list of arguments passed to the command.
533+
/// colors (List<String>): The list of colors, only the first color is used.
506534
///
507535
/// Returns:
508536
/// The return type is void.
509-
void _fillEmpty(List<String> el) {
510-
final CatColors color = containsColor(el.first);
537+
void fillEmpty(List<String> colors) {
538+
final CatColors color = containsColor(colors.first);
511539
if (color != CatColors.NaC) {
512540
_commandCaller.color("fillEmpty", <dynamic>[color.index]);
513541
} else {
@@ -543,7 +571,7 @@ class CATInterpreter {
543571
_go(el);
544572
break;
545573
case "fill_empty":
546-
_fillEmpty(el);
574+
fillEmpty(el);
547575
break;
548576
case "copy":
549577
_copy(el);

0 commit comments

Comments
 (0)