Spiral 4 – implementing the rules
But wait, our game can only work if the same color only appears in two cells: a cell and its twin cell. Moreover, a cell can be hidden or not, that is, the color can be seen or not? To take care of this, the Cell
class gets two new attributes:
Cell twin; bool hidden = true;
Note
For code files of this section, refer to chapter 7\educ_memory_game\spirals\s04
in the code bundle.
The method
_colorBox
in the Board
class can now show the color of the cell when hidden is false (line (2)
); when hidden = true
(the default state) a neutral gray color will be used for that cell (line (1)
):
static const String COLOR_CODE = '#f0f0f0';
We also gave the gap
variable a better name, boxSize
:
void _colorBox(Cell cell) { var x = cell.column * boxSize; var y = cell.row * boxSize; context.beginPath(); if (cell.hidden) { context.fillStyle = COLOR_CODE; (1) } else { context.fillStyle = colorMap[cell.color]; (2) ...