text stringlengths 0 897 |
|---|
sunk = comp.fire(c+r, human) |
if sunk then print "Ha ha! I sunk your " + sunk + "!" |
end function |
``` |
No test code on this one, because it's referencing two global variables that we haven't defined yet. So go ahead and remove the previous test code, add Listing 9, save it, and run it. If you see just the welcome message, you're ready to move on to the next part, where we actually define those global variables. |
{caption: "Listing 10 (Sea Battle player setup).", number-from: 176} |
```miniscript |
// Set up the human player |
human = new Player |
human.init |
for s in shipTypes.indexes |
human.placeShipByInput s // (or placeShipRandomly to speed things up) |
end for |
human.print |
// Now set up the computer player |
comp = new Player |
comp.init |
for s in shipTypes.indexes |
comp.placeShipRandomly s |
end for |
comp.print // <-- Just for debugging. (No cheating!) |
``` |
When you run at this point, it should present you with a blank board, and then prompt you to place each of your ships, using the `for` loop on lines 179-181. Your map layout is printed after each entry. Then the computer very quickly places its ships, using its own `for` loop on lines 189-189, and that is printed as ... |
And now we're almost done! All that's left is the main game loop. Be sure to comment out that `comp.print` call on line 190, since the game doesn't have much challenge if you know where the computer's ships are! |
{caption: "Listing 11 (Sea Battle main loop).", number-from: 190} |
```miniscript |
//comp.print // <-- Just for debugging. (No cheating!) |
// Main game loop |
while true |
print |
sunk = human.fire(inputCoordinates("Fire at coordinates: "), comp) |
if sunk then print "You sunk my " + sunk + "!" |
if not comp.shipsLeft then |
print "You win. Good game!" |
exit |
end if |
print |
ai |
ai // (a second time, to make game more challenging) |
print |
human.print |
if not human.shipsLeft then |
print "I win! Thanks for the game." |
exit |
end if |
end while |
``` |
And that's it! Each time through the loop, we use `inputCoordinates` to get the human player's move, and `fire` at that location. This returns the name of the ship sunk, if any; so we print that out on line 196. If the computer doesn't have any ships left, then the human has won. |
Otherwise, we let the computer do its move via the `ai` function. In fact we let it move *twice* (lines 202-203). This is to compensate for the rather stupid AI. I find that this makes for a pretty interesting game -- sheer dumb luck (times two) against human wit. Give it a try, and see how you do! |
Remember, if you get errors or the behavior of the program is not what you think it should be, just go back and check your typing carefully. Pay attention to spelling, capitalization, and punctuation. Check the end of Chapter 7 for other trouble-shooting tips. |
## Going Farther |
You've worked hard today, building this game up section by section. Hopefully you've also built some good habits that will serve you well in the future. It would be completely reasonable to call it a day. |
But at some point, now or later, you may get the urge to pull this program out again and build on it. The most obvious thing to improve is the AI. Fortunately that's all contained in one function, `ai` (Listing 9). Instead of picking randomly, you might try first guessing any spot next to a hit, and then guessing sp... |
Also, over the next week or two you're going to learn how to use Mini Micro. This very same program will run as-is on Mini Micro, using the keyboard and text display... but Mini Micro can do so much more than that. You may want to revisit Sea Battle and put a nice graphical interface on it, or add sounds (or both!). |
So think of what you did today as a starter kit. You've assembled it according to the directions, but as you acquire more skills and tools, you can build on it to create something uniquely your own. |
A> **Chapter Review** |
A> - You built the *Sea Battle* game, pitting human versus computer in a classic game of strategy and luck. |
A> - You saw how to build up a big program as a series of smaller tests, stopping to test and debug at each step, rather than doing it all at the end. |
A> - You exercised everything you've learned about MiniScript so far, including functions and classes. The more you use this stuff, the easier it will be! |
{chapterHead: "Day 15: Introducing Mini Micro", startingPageNum:171} |
{width: "50%"} |
 |
Q> Engineering isn’t just the physics of how a transistor works. It’s using the technology, and being creative with it, and solving problems that people have or creating something beautiful with it. |
Q>— Limor Fried (engineer, co-founder of Adafruit Industries) |
A> **Chapter Objectives** |
A> - Download and install Mini Micro. |
A> - Learn the basic Mini Micro commands needed to load and run programs. |
A> - Explore the nifty demos that come with it. |
A> - Review the difference between a programming *language* and a programming *environment*. |
This chapter begins the second half of the book, which is quite honestly the more fun half. *Any* programming is fun, but the fun of text on a web page or terminal window pales in comparison to making pictures, animations, sound effects, and music! |
For the rest of this book you'll be using *Mini Micro*. |
Mini Micro |
: a neo-retro virtual home computer from an alternate, much cooler universe |
Mini Micro is an application you download install on your computer. At the time of this writing, it runs on desktop computers (Windows, Mac, and Linux), though a version for mobile phones and tablets is in the works and may well be done by the time you're reading this. Mini Micro is a MiniScript programming environme... |
- efficient, interactive graphics |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.