text
stringlengths
0
897
self.myMap = newMap
self.targetMap = newMap
self.shipsLeft = [] // names of ships not yet sunk
end function
Player.print = function()
print " YOUR SHIPS TARGET MAP" // 9 spaces in middle
print " +-----------+ +-----------+" // 9 dashes per group
for r in rows
temp = [r, "|"]
for c in cols
temp.push self.myMap[c + r]
end for
temp.push "| " + r + "|"
for c in cols
temp.push self.targetMap[c + r]
end for
temp.push "|"
print temp.join("")
end for
print " +-----------+ +-----------+"
print " " + cols.join("") + " " + cols.join("")
end function
p = new Player
p.init
p.print
```
Q> "Type it in, save it, test it out, HEY-O!"
Q> —lyrics from hit single *Code Me* by Noobz 2 Guruz
Each player has a map (created with the `newMap` function from Listing 2) called `myMap` that represents their own ships, and another one called `targetMap` that keeps track of where they have fired at the other player. The `Player.print` function prints these out side by side. It's a big function, but the structure ...
{width:75%}
![Output after Listing 4](battle-l4.png)
It's starting to look like a game! And you're more than a third done. Maybe stretch your legs a bit, and then delete lines 77-79, and enter Listing 5.
{caption: "Listing 5 (Sea Battle `writeToMap` function).", number-from: 77}
```miniscript
// Write a string representing a ship into our map, or optionally,
// just check whether we COULD write such a string. Return an
// error string if it runs out of bounds or hits another ship;
// or if everything is OK, then return null.
Player.writeToMap = function(ship, position, horizontal, checkOnly)
c = position[0] // column, e.g. "E"
r = position[1] // row, e.g. "5"
for i in ship.indexes
// check and maybe write to the map
if self.myMap[c + r] != EMPTY then return "Position blocked"
if not checkOnly then self.myMap[c + r] = ship[i]
// then, advance to the next position (unless we're done)
if i == ship.len-1 then return // all done!
if horizontal then
idx = cols.indexOf(c) + 1
if idx >= cols.len then return "Out of bounds"
c = cols[idx]
else
idx = rows.indexOf(r) + 1
if idx >= rows.len then return "Out of bounds"
r = rows[rows.indexOf(r) + 1]
end if
end for
end function
p = new Player
p.init
p.writeToMap "DDDD", "C4", true, false
p.print
```
This is a somewhat tricky but important function. The job of this function is to check whether a ship can be placed at a given position and orientation in the map, and optionally, to actually place it there by writing the letters that represent the ship into the map. The function takes four parameters: the string rep...
Why do we have this check-only behavior? Determining if the ship can fit is a bit tricky. It doesn't fit if it goes off the edge of the grid, or if it hits a spot that is not empty (because some earlier ship was already using that spot). If we're placing a big ship, the first few spots might be fine, but the next sp...
The test code in Listing 5 is a bit thin. But if all is working, you should see the destroyer placed as "DDDD" in cells C4 through F4. If you want to add some additional tests to make sure it returns an error if you give it an invalid ship placement, feel free! Move on when you're ready.
The next step will be to use that function to let the player place a ship. This function prompts the user for a location (using the `inputCoordinates` function from Listing 3) and an orientation, either horizontal or vertical. Then it calls the `writeToMap` function above twice, once to check, and once to actually do...
{caption: "Listing 6 (Sea Battle `placeShipByInput` function).", number-from: 103}
```miniscript
Player.placeShipByInput = function(name)
self.print
print
self.shipsLeft.push name
ship = shipTypes[name]
while true // keep trying till we succeed
print "Placing " + name + " (size " + ship.len + ")."
pos = inputCoordinates(" Top-left coordinates? ")
hv = ""
while hv != "H" and hv != "V"
hv = input(" Horizontal or Vertical (H/V)? ").upper
end while
err = self.writeToMap(ship, pos, hv=="H", true) // just check
if err then
print err + ". Please try again!"
else
self.writeToMap ship, pos, hv=="H", false // place ship