text
stringlengths
0
897
```miniscript
// Show the treasure found; add it to our list and score
getTreasure = function(p, found)
td.setCell p.col, p.row, found
treasureSnd.play
img = td.tileSet.getImage(32*(found-kCellTreasure+2), 0, 32, 32)
gfx.drawImage img, treasureListX + 20, treasureListY
points = 100 + ceil(1E6/(10+time))
gfx.print points, treasureListX+60, treasureListY+6, gold, "small"
globals.treasureListY = treasureListY - 40
globals.score = score + points
drawTimeAndScore
wait 0.5
globals.treasuresLeft = treasuresLeft - 1
if not treasuresLeft then gameWon
end function
// Reveal the given grid position. If it's a bomb,
// game over! Otherwise show how many neighboring cells
// contain bombs. If that is 0, reveal all neighbors.
reveal = function(p)
if td.cell(p.col, p.row) < kCellGrass then return
found = hidden[p.col][p.row]
// If we found a bomb, then it's game-over
if found == kCellBomb then
doBoom p
return
end if
// If we found treasure, reveal it
if found >= kCellTreasure and found < kCellTreasure+6 then
getTreasure p, found
return
end if
// Otherwise, count bombs and show the number
bombCount = 0
for n in p.neighbors
if hidden[n.col][n.row] == kCellBomb then
bombCount = bombCount + 1
end if
end for
td.setCell p.col, p.row, bombCount
// ...and if the bomb count was 0, then reveal neighbors too
if bombCount == 0 then
for n in p.neighbors; reveal n; end for
end if
end function
```
The `getTreasure` function is fairly straightforward, but it contains a handy trick: it's calling `getImage` on the tile set image itself, to pluck out an image of the treasure for drawing on the pixel display. You probably noticed that this tile set actually contains two versions of each treasure: one on the sandy ba...
The `reveal` method contains a technique you may find surprising. If you've ever played Minesweeper, you know that when you click on a cell with *no* bombs next to it, then the game reveals not only that square, but all adjacent squares too. And if ane of *those* squares have no neighboring bombs, the process repeats...
{i:"recursion"}
It's actually quite simple: line 230 checks for the case where the bomb count is zero, and then that little loop on line 231 does the rest. We simply loop over the neighbors of this grid position, and call `reveal` on each one. The `reveal` method is calling itself, which is a technique called *recursion*. This was ...
Testing these functions is pretty straightforward. You'll need to pass `getTreasure` a grid position (use `GridPos.make` again) as well as a treasure tile index, from 10 to 15. You should see the treasure appear both on the map and on the "Treasures Found" list on the right side of the screen.
To test `reveal`, just pass it a grid position. But you might want to call `buryStuff` first, otherwise the map will be empty, and a single `reveal` will simply turn the whole map from grass to sand.
## Main Loop
Finally, as with most programs, the main loop comes at the end. The main loop in this program is very simple, because we have so neatly divided all the real work out into functions.
{caption:"Listing 7 (setup and main loop.)", number-from: 235}
```miniscript
// Setup and main loop
buryStuff
while true
drawTimeAndScore
if mouse.button(0) then handleClick(0)
if mouse.button(1) then handleClick(1)
yield
end while
```
And that's it! After you've put this in, go and play a while! It's not an easy game to win, but not impossible either. My high score is about 250,000 points. Can you beat that?
## Taking it Further
This is a pretty polished game, but you could make it even better!
You might start with some simple tweaks to optimize the fun. You can easily change the board size by tweaking those constants on lines 7-8. You could also change the number of bombs and treasures, by changing the default values on the `buryStuff` function on line 148, or by overriding those defaults where the functio...
Beyond that, you might want to do something more exciting when the game ends. If you lose, there's a fairly satisfying boom and a small animation, but you might want to do something more spectacular... maybe draw a *giant* explosion sprite that covers the whole screen? Or use a `SolidColorDisplay` to make the whole s...
A> **Chapter Review**
A> - You entered and debugged the *Treasure Hunt* game.
A> - You practiced working with tile displays.
A> - You also reviewed use of sounds, pixel displays, and text.
A> - Most importantly, you practiced the "code a little, test a little" habit, this time with your *own* tests at each step.
{chapterHead: "Day 27: Algorithms, Step by Step", startingPageNum:335}
{width: "50%"}
![](Chapter27.svg)
Q> First, solve the problem. Then, write the code.
Q>— John Johnson