gridToString method
Converts the matrix to a string representation.
This method creates a string representation of the matrix, with options to format it for code or for display.
Parameters:
forCode
: If true, formats the output as a Dart string literal. Default is false.onChar
: The character to represent true cells. Default is '#'.offChar
: The character to represent false cells. Default is '.'.
Returns:
A string representation of the matrix. If forCode
is true, the string
is formatted as a multi-line Dart string literal. Otherwise, it's a
simple string with newline characters separating rows.
Example:
final matrix = Matrix(/* ... */);
// For display (forCode = false):
print(matrix.gridToString());
// Output:
// #.#
// .#.
// #.#
// For code (forCode = true):
print(matrix.gridToString(forCode: true));
// Output:
// "#.#",
// ".#.",
// "#.#"
Note: This method uses gridToStrings internally to generate the list of strings representing each row of the matrix.
Implementation
String gridToString({
final bool forCode = false,
final String onChar = '#',
final String offChar = '.',
}) {
final List<String> list = gridToStrings(onChar: onChar, offChar: offChar);
return forCode ? '"${list.join('",\n"')}"' : list.join('\n');
}