Spiral 6 – some finishing touches

A working program always gives its developer a sense of joy, and rightfully so. However, this doesn't mean you can leave the code as it is. On the contrary, carefully review your code for some time to see if there is room for improvement or optimization. For example, are the names you used clear enough? The color of a hidden cell is now named simply COLOR_CODE in board.dart, renaming it to HIDDEN_CELL_COLOR_CODE makes its meaning explicit. The List used in the Cells class can indicate that it is a List<Cell>, by applying the fact that Dart lists are generic. The parameter of the every method in the Cell class is more precise—it is a function that accepts a cell and returns bool. Our onMouseDown event handler contains our game logic, so it is very important to tune it if possible. After some thought we see that the code from the previous spiral can be improved; in the line below the second condition after && is in fact unnecessary:

  if (cell.twin == lastCellClicked && lastCellClicked.shown) {...}

Note

For code files of this section, refer to chapter 7educ_memory_gamespiralss06 in the code bundle.

When the player has guessed everything correctly, showing the completed screen for a few seconds will be more satisfactory (line (2)). So, this portion of our event handler code changes to:

if (cell.twin == lastCellClicked) {                          (1)
    lastCellClicked.hidden = false;
    if (memory.recalled) { // game over
        new Timer(const Duration(milliseconds: 5000), () =>    memory.hide());                            (2)
    }
} else if (cell.twin.hidden) {
      new Timer(const Duration(milliseconds: 800), () =>  cell.hidden = true);
  }

And why not show a banner "YOU HAVE WON!"? We will do this by drawing the text on the canvas (line (3)), so we must do it in the draw() method (otherwise, it would disappear after INTERVAL milliseconds):

void draw() {
  _clear();
  _boxes();
  if (memory.recalled) { // game over
   context.font = "bold 25px sans-serif";
   context.fillStyle = "red";
   context.fillText("YOU HAVE WON !", boxSize, boxSize * 2);   (3)
  }
}

Then the same game with the same configuration can be played again.

We could make it more obvious that a cell is hidden by decorating it with a small circle in the _colorBox method (line (4)):

  if (cell.hidden) {
    context.fillStyle = HIDDEN_CELL_COLOR_CODE;
    var centerX = cell.column * boxSize + boxSize / 2;
    var centerY = cell.row * boxSize + boxSize / 2;
    var radius = 4;
    context.arc(centerX, centerY, radius, 0, 2 * PI, false);   (4)
  }

We do want to give our player a chance to start over by supplying a Play again button. The easiest way is to simply refresh the screen (line (5)) by adding this code to the startup script:

void main() {
  canvas = querySelector('#canvas'),
  ButtonElement play = querySelector('#play'),
  play.onClick.listen(playAgain);
  new Board(canvas, new Memory(4));
}

playAgain(Event e) { 
  window.location.reload();                              (5)
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.37