Skip to content

Commit 67d1278

Browse files
committed
Commands modifications for documentation
1 parent b3ea305 commit 67d1278

File tree

2 files changed

+96
-58
lines changed

2 files changed

+96
-58
lines changed

lib/src/interpreter.dart

Lines changed: 96 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,36 @@ class CATInterpreter {
115115
)
116116
.toList();
117117
final String move = splited.join();
118-
bool call = _commandCaller.move(move, <int>[repetitions]);
118+
go(move, repetitions);
119+
if (_error == CatError.invalidMove) {
120+
goCell(splited[0]);
121+
}
122+
}
123+
124+
void goCell(String cell) {
125+
bool call = true;
126+
if (cell.length == 2 &&
127+
_rows.containsKey(cell[0]) &&
128+
_columns.containsKey(cell[1])) {
129+
call = _commandCaller.move(
130+
"toPosition",
131+
<int>[_rows[cell[0]]!, _columns[cell[1]]!],
132+
);
133+
} else {
134+
_error = CatError.invalidCell;
135+
136+
return;
137+
}
119138
if (!call) {
120-
if (splited[0].length == 2 &&
121-
_rows.containsKey(splited[0][0]) &&
122-
_columns.containsKey(splited[0][1])) {
123-
call = _commandCaller.move(
124-
"toPosition",
125-
<int>[_rows[splited[0][0]]!, _columns[splited[0][1]]!],
126-
);
127-
} else {
128-
_error = CatError.invalidCell;
139+
_error = CatError.invalidMove;
129140

130-
return;
131-
}
141+
return;
132142
}
143+
_error = CatError.none;
144+
}
145+
146+
void go(String move, int repetitions) {
147+
final bool call = _commandCaller.move(move, <int>[repetitions]);
133148
if (!call) {
134149
_error = CatError.invalidMove;
135150
}
@@ -145,61 +160,89 @@ class CATInterpreter {
145160
/// A list of strings.
146161
void _paint(List<String> command) {
147162
final List<String> colors = splitByCurly(command.first);
163+
if (command.length == 1) {
164+
paintSingleCell(colors.first);
165+
166+
return;
167+
}
168+
if (command.last.startsWith("{") && command.last.endsWith("}")) {
169+
final List<String> destinations = splitByCurly(command.last);
170+
paintMultileCells(colors, destinations);
171+
} else {
172+
paintPattern(colors, command[1], command[2]);
173+
}
174+
}
175+
176+
void paintPattern(List<String> colors, String repetitions, String pattern) {
148177
final List<int> colorsParsed =
149178
colors.map((String e) => containsColor(e.trim()).index).toList();
150179
if (colorsParsed.contains(CatColors.NaC.index)) {
151180
_error = CatError.invalidColor;
152181

153182
return;
154183
}
155-
if (command.length == 1) {
156-
_commandCaller.color(
157-
"color",
158-
<int>[colorsParsed[0]],
159-
);
184+
final int column = _commandCaller.board.move.column;
185+
final int row = _commandCaller.board.move.row;
186+
final String color = pattern
187+
.split(" ")
188+
.where((String element) => element.isNotNullOrEmpty)
189+
.mapIndexed(
190+
(int index, String p1) => index == 0 ? p1 : p1.capitalize(),
191+
)
192+
.map((String e) => e.replaceAll("-", ""))
193+
.join();
194+
bool call = false;
195+
try {
196+
final int repetitionsParsed = repetitions.toInt();
197+
call = _commandCaller
198+
.color(color, <dynamic>[colorsParsed, repetitionsParsed]);
199+
} on FormatException {
200+
call = _commandCaller.color(color, <dynamic>[
201+
colorsParsed,
202+
]);
203+
}
204+
205+
if (!call) {
206+
_error = CatError.invalidColoringCommand;
207+
}
208+
_commandCaller.board.move.toPosition(row, column);
209+
}
210+
211+
void paintMultileCells(List<String> colors, List<String> cellsPositions) {
212+
final List<int> colorsParsed =
213+
colors.map((String e) => containsColor(e.trim()).index).toList();
214+
if (colorsParsed.contains(CatColors.NaC.index)) {
215+
_error = CatError.invalidColor;
160216

161217
return;
162218
}
163219
final int column = _commandCaller.board.move.column;
164220
final int row = _commandCaller.board.move.row;
165-
if (command.last.startsWith("{") && command.last.endsWith("}")) {
166-
final List<String> destinations = splitByCurly(command.last);
167-
final StringBuffer newCommand = StringBuffer();
168-
int j = 0;
169-
for (final String i in destinations) {
170-
newCommand
171-
..write("go($i)")
172-
..write("paint(${colors[j]})");
173-
j = (j + 1) % colors.length;
174-
}
175-
_parse(newCommand.toString(), false);
176-
} else {
177-
final String color = command[2]
178-
.split(" ")
179-
.where((String element) => element.isNotNullOrEmpty)
180-
.mapIndexed(
181-
(int index, String p1) => index == 0 ? p1 : p1.capitalize(),
182-
)
183-
.map((String e) => e.replaceAll("-", ""))
184-
.join();
185-
bool call = false;
186-
try {
187-
final int repetitions = command[1].toInt();
188-
call =
189-
_commandCaller.color(color, <dynamic>[colorsParsed, repetitions]);
190-
} on FormatException {
191-
call = _commandCaller.color(color, <dynamic>[
192-
colorsParsed,
193-
]);
194-
}
195-
196-
if (!call) {
197-
_error = CatError.invalidColoringCommand;
198-
}
221+
final StringBuffer newCommand = StringBuffer();
222+
int j = 0;
223+
for (final String i in cellsPositions) {
224+
newCommand
225+
..write("go($i)")
226+
..write("paint(${colors[j]})");
227+
j = (j + 1) % colors.length;
199228
}
229+
_parse(newCommand.toString(), false);
200230
_commandCaller.board.move.toPosition(row, column);
201231
}
202232

233+
void paintSingleCell(String color) {
234+
final int colorParsed = containsColor(color.trim()).index;
235+
if (colorParsed == CatColors.NaC.index) {
236+
_error = CatError.invalidColor;
237+
238+
return;
239+
}
240+
_commandCaller.color(
241+
"color",
242+
<int>[colorParsed],
243+
);
244+
}
245+
203246
/// It takes a list of commands and a list of positions where to copy the
204247
/// commands.
205248
/// Args:
@@ -255,8 +298,7 @@ class CATInterpreter {
255298
repeatCommands(toExecute, splitByCurly(command[1]));
256299
} else {
257300
final List<String> origin = splitByCurly(command[0]);
258-
final List<String> destination =
259-
splitByCurly(command[1]);
301+
final List<String> destination = splitByCurly(command[1]);
260302
copyCells(origin, destination);
261303
}
262304
}
@@ -349,8 +391,7 @@ class CATInterpreter {
349391
List<String> destination,
350392
) {
351393
final List<Pair<int, int>> originLocal = _sortCells(origin);
352-
final List<Pair<int, int>> destinationLocal =
353-
_sortCells(destination);
394+
final List<Pair<int, int>> destinationLocal = _sortCells(destination);
354395
final List<String> newDestinations = <String>[];
355396
final List<String> colors = <String>[];
356397
for (final Pair<int, int> i in destinationLocal) {

lib/src/utils/shape.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import "package:interpreter/cat_interpreter.dart";
2-
import "package:interpreter/src/models/basic_shape.dart";
3-
import "package:interpreter/src/models/cross.dart";
4-
import "package:interpreter/src/models/square.dart";
52

63
/// A factory that returns a `BasicShape` object.
74
enum Shape {

0 commit comments

Comments
 (0)