text
stringlengths
0
897
Notice what we did here: we began with just a tiny piece of the program. This is the right way to approach any program, and is in fact exactly what professional programmers do!
D> When building a large program, never try to do it all at once. Instead, build it up from smaller pieces, always testing and debugging as you go.
PRINT>Now that we have a working (trivial) script, the next most important thing we have to do is figure out how we are going to represent the data in the game. Each player has a couple of map grids: one that shows their own ships, along with where their opponent has fired; and one that keeps track of where they have ...
EBOOK>Now that we have a working (trivial) script, the next most important thing we have to do is figure out how we are going to represent the data in the game. Each player has a couple of map grids: one that shows their own ships, along with where their opponent has fired; and one that keeps track of where they have ...
![Conceptual diagram of the two map grids each player must keep: one for their own ships and where the opponent has fired, and one for where they have fired at the opponent.](BattleGrids.svg)
There are several ways we could represent such a grid in MiniScript. We could use a list of lists, indexed by row and column number. But instead we're going to use the `map` data type, where the key is a coordinate string like "C5", and the value is a character that indicates an empty cell, a part of a ship, a hit, o...
So, having figured that out, go ahead and add the code in Listing 2 to your `battle.ms` file. (Note that the line numbers start at 5 because line 4, not shown, is blank.)
{caption: "Listing 2 (Sea Battle data model).", number-from: 5}
```miniscript
rows = "987654321".split("")
cols = "ABCDEFGHIJK".split("")
shipTypes = {
"Battleship": "BBBBB",
"Destroyer": "DDDD",
"Cruiser": "CCC",
"Frigate": "FFF",
"Patrol Boat": "PP" }
EMPTY = "."
MISS = "w"
HIT = "*"
newMap = function()
m = {}
for r in rows
for c in cols
m[c + r] = EMPTY
end for
end for
return m
end function
print "rows: " + rows
print "cols: " + cols
print "test map: " + newMap
```
Those last three lines are just test code that we'll remove in a moment. But when you run this, after the "Welcome to SEA BATTLE!" message, you should see `rows` printed as a list of strings from "9" to "1", and "cols" as a list of strings from "A" to "K". You'll also see a test map printed as a rather large dictiona...
Strings like "." in this program are sometimes called *magic strings*.
magic string
: a string literal that has special meaning to the code, so that if you were to change it in one place without changing it elsewhere, the code would no longer work properly
Magic strings are generally not considered a good thing, because it's so easy to mess them up. So rather than using the string literals everywhere we need to refer to an empty cell, we assign this magic string to a variable called `EMPTY` on line 15 (and do something similar for the strings that represent a hit or mis...
You probably also noticed the `shipTypes` map, which maps each ship to a string of letters that will represent it on the map. You can change those too, as long as you stick to two rules: all the letters for a particular ship must be the same, and no two ships can use the same letters. We'll assume these rules later w...
Once you have that much working, let's keep going! Delete those last few lines (29-31), and continue with Listing 3.
{caption: "Listing 3 (Sea Battle input validation).", number-from: 29}
```miniscript
isRow = function(s)
return rows.indexOf(s) != null
end function
isCol = function(s)
return cols.indexOf(s) != null
end function
// Get coordinates from the user, and return as string in
// standard form, e.g. "A1" (to index into one of our sea maps).
inputCoordinates = function(prompt)
while true
s = input(prompt).upper
if s.len == 2 and isRow(s[0]) and isCol(s[1]) then
return s[1] + s[0]
else if s.len == 2 and isRow(s[1]) and isCol(s[0]) then
return s
end if
print "Please enter a map position like A1 or G8."
end while
end function
print inputCoordinates("Map position? ")
```
This part begins by defining a couple of handy functions to determine whether a given character is a valid row indicator (like "5") or column indicator (like "E"). They work by calling `indexOf` to find the index of the given character in our `rows` or `cols` list; if the character is not found, then `indexOf` returns...
Then we define an `inputCoordinates` function that displays a prompt asking the user for a grid coordinate, and keeps asking until it gets a valid answer. It also standardizes that answer, so even if you enter "c5" or "5c" or "5C", it will be returned as "C5".
Again we have tacked a bit of test code to the end of this block. So, type in the above, and test it a few times, trying both valid and invalid map positions. Make sure it works before moving on.
What's next? The game has two players, and the data for each player is identical: two map grids, plus a list of ships that haven't been sunk yet. Any time you see a need for a collection of data, defining a class is probably a good idea; and if you have a need for two or more such collections, then a class is *defini...
So in Listing 4, we will define a Player class, including a function to initialize it, and a function to print out its maps. (In normal play we will never print the computer player's maps, but we might want to for debugging, so it's handy to have it as a general function on the Player class.) Again, remember to remov...
{caption: "Listing 4 (Sea Battle Player class).", number-from: 50}
```miniscript
Player = {}
Player.init = function()