The task is complex, so build specialists that accomplish part of the task and then put them together. For example, you might start by building:
3 tiles
means three tiles on each row (and three rows), not three tiles altogether.)Test each block by itself before building a block that uses it.
Your top-level board-drawing block, draw n-by-n board
, might look something like this.
.
Abstraction: Why building the block clear and go to starting place
, when all it contains is a few commands to assure that the sprite starts in the right place, pointed the right way, with its pen down, and that the stages is cleared?
One purpose of hiding the details is to make your code clear, understandable, and easy to debug and update. Instead of seeing the details and wondering what they are for, you know exactly what this piece of code does just by reading the block's name.
The same is true of . Instead of building that block, you could have used
between?
block to report true
if the sprite is on the board, and reports false
if the sprite is dragged completely off the board.which cell?
that tells which cell the sprite has been dragged into.
Here is one way to do it that uses two other blocks that specialize in reporting which column?
and which row?
the sprite is in.
These blocks will, of course, need to know how many rows and columns there were, so they'll need the input n
.
which row?
and which column?
You can, of course, use lots of if
statements, but since the number of rows can change, it's hard to know how many to use. One convenient way is to divide the total size of the game board by the size of each cell. To use that result, you need to round down. floor
function is in the same block as sqrt
.move to tile (n)
that takes a tile number as input and moves your sprite to the center of that tile. Because the location of tile 9 depends on how many tiles there are, your move to tile
block will need to know the size of the board.mark tile (n)
to move to the specified tile and