getPattern method
Implementation
MapEntry<List<String>, Map<String, Item>> getPattern() {
final items = <Item, String>{};
var pattern = [' ' * 3, ' ' * 3, ' ' * 3];
ingredients.forEach((k, item) {
// If the item is not known
if (!items.containsKey(item)) {
// choose a new key from _CHARS
items[item] = _CHARS[items.length];
}
k--;
pattern[k ~/ 3] = _replaceCharAt(pattern[k ~/ 3], k % 3, items[item]!);
});
if (!exactlyPlaced) {
// remove empty columns
if (pattern[0][2] == ' ' &&
pattern[1][2] == ' ' &&
pattern[2][2] == ' ') {
pattern = pattern.map((row) => row.substring(0, 2)).toList();
}
if (pattern[0][0] == ' ' &&
pattern[1][0] == ' ' &&
pattern[2][0] == ' ') {
pattern = pattern.map((row) => row.substring(1)).toList();
// check second column if first is cleared
if (pattern[0][0] == ' ' &&
pattern[1][0] == ' ' &&
pattern[2][0] == ' ') {
pattern = pattern.map((row) => row.substring(1)).toList();
}
}
// remove empty rows
if (pattern.first.trimLeft().isEmpty) {
pattern = pattern.sublist(1);
// check second row if first is done
if (pattern.first.trimLeft().isEmpty) pattern = pattern.sublist(1);
} else if (pattern[1].trimLeft().isEmpty) {
pattern = [pattern[0], pattern[2]];
}
if (pattern.last.trimLeft().isEmpty) {
pattern = pattern.sublist(0, pattern.length - 1);
}
}
return MapEntry(pattern, items.map((k, v) => MapEntry(v, k)));
}