text
stringlengths
0
897
Yesterday we made a solid start on our word-finding game: we prepared the word list, wrote code to randomly select letters in the same proportions as real words, made a `Sprite` subclass named `Tile`, and animated a grid of tiles sliding into place. Today we're going to finish the game, with selection of letters to ma...
## Tile selection
Start by adding the code in Listing 1 to the program you began yesterday.
{caption:"Listing 1 (tile selection with the mouse).", number-from: 131}
```miniscript
tileUnderMouse = function()
for s in spriteDisp.sprites
if not s isa Tile then continue
if s.contains(mouse) then return s
end for
end function
wordInProgress = ""
addToWord = function(tile)
globals.wordInProgress = wordInProgress + tile.letter
if lastTile != null then
overlay.line lastTile.x, lastTile.y, tile.x, tile.y, "#88FF8888", 5
end if
globals.lastTile = tile
tile.tint = color.aqua
tile.selected = true
end function
trackMouseOnTiles = function()
while mouse.button
updateTime
yield
tile = tileUnderMouse
if tile == null or tile.selected then continue
if mathUtil.distance(tile, lastTile) > 150 then continue
addToWord tile
end while
end function
```
{i:"`isa` operator;operator, `isa`"}
We've added several new functions here. The `tileUnderMouse` function is responsible for figuring out which `Tile` instance, if any, is under the mouse. It does this by iterating over all sprites in the display, skipping any that is not actually a `Tile`. It checks whether each sprite `s` is a `Tile` by using MiniSc...
D> `isa` is the operator used to check data types in MiniScript; it returns 1 (true) if the operand on the left matches the type on the right, and 0 (false) otherwise. Built-in types can be identified as `number`, `string`, `list`, `map`, and `funcRef`. In the case of a class, `isa` returns true if the object on the ...
The `tileUnderMouse` function then uses the `Sprite.contains` method to check whether each tile contains the mouse. (Recall that this works because we assigned the `localBounds` of the `Tile` class.)
The next function, `addToWord`, adds a given tile to the growing word, stored in the `wordInProgress` global variable. Then it draws a line in the overlay display (a `PixelDisplay` we prepared at the top of the program), and highlights the given tile by tinting it aqua.
Finally, the `trackMouseOnTiles` function is a loop that continues as long as the mouse is down. It's meant to be called after the first tile has been clicked, and it handles dragging of the mouse over subsequent tiles. Lines 154 avoids any problems when the mouse is between tiles. Line 155 prevents users from cheat...
To test this code requires a little bit of work: we need to first initialize our grid, and then wait for the user to click the mouse. Then we need to update `lastTile` and call `addToWord` before we can finally call that `trackMouseOnTiles` function. Add the following test code to your program so you can try it out a...
{caption:"Test code for Listing 1.", number-from: 160}
```miniscript
// test code:
updateTime = null // (we'll fill this in later)
for i in range(1000); Tile.updateAll; end for
while not mouse.button; yield; end while
lastTile = tileUnderMouse
addToWord lastTile
trackMouseOnTiles
print wordInProgress
```
It's worth pointing out the trick on line 161: if you assign `null` to a variable, you can use that like a do-nothing function with no parameters. That's needed in this case because `trackMouseOnTiles` calls `updateTime` to update a timer, but we haven't gotten to that code yet.
With this test code in place, run your program. Tiles should settle very quickly into place, and then if you click any tile and drag, you can construct a word (or a string of random gibberish). Run it a few times. What's the longest word you can find? (Mine was "FILET"... but I bet you can do better!)
## Clearing tiles
To turn this word-selection functionality into a game, we'll need at least two more things. First, if the word is valid, then we need to clear out the tiles used, move any higher-up tiles down, and add new tiles to fill the columns. Second, in the case where the letters traced do not make a valid word, we need to jus...
The code in Listing 2 below provides these functions. Insert this code *before* your existing test code, as you'll still need that to test.
{caption:"Listing 2 (Tile clearing and deselection).", number-from: 160}
```miniscript
clearUsedTiles = function()
for col in range(0, kCols-1)
// first iterate top-down, deleting any selected tiles
for row in range(kRows-1, 0)
tile = grid[col][row]
if tile == null or not tile.selected then continue
spriteDisp.sprites.removeVal tile // remove sprite
grid[col].remove row // remove used tile
grid[col].push null // add empty spot to column
end for
// then iterate again bottom-up, adding new tiles
for row in range(0, kRows-1)
if grid[col][row] == null then
newTile col, row
else
grid[col][row].goToGridPos col, row
end if
end for
end for
overlay.clear
globals.lastTile = null
end function
deselectTiles = function()
for col in range(0, kCols-1)