text
stringlengths
0
897
return // and we're done
end if
end while
end function
p = new Player
p.init
p.placeShipByInput "Destroyer"
p.placeShipByInput "Battleship"
p.print
```
Run this several times, trying different positions and orientations, including some that would run off the map, or place the ships overlapping. The code should detect the problem, and keep prompting you until you pick a valid place.
We're also going to need a function to place ships randomly. In the finished game, this is used by the computer player to place its ships. However during development, I also used it for the human player, so that I could get into a game more quickly and test whatever I was working on without having to place each of my...
(Note that you can save yourself a bit of typing by not deleting the test code from Listing 6, but instead only updating it to match Listing 7.)
{caption: "Listing 7 (Sea Battle `placeShipRandomly` function).", number-from: 125}
```miniscript
Player.placeShipRandomly = function(name)
self.shipsLeft.push name
ship = shipTypes[name]
while true // keep trying till we succeed
pos = cols[rnd * cols.len] + rows[rnd * rows.len]
horizontal = (rnd < 0.5)
err = self.writeToMap(ship, pos, horizontal, true) // check
if err then continue
self.writeToMap ship, pos, horizontal, false // place ship
return // and we're done
end while
end function
p = new Player
p.init
p.placeShipRandomly "Destroyer"
p.placeShipRandomly "Battleship"
p.print
```
Now we're getting there! The core of the game is firing shots at your opponent's map. We need a function that does that, checks the opponent's map, reports *hit* or *miss*, and records the shot in both player's maps. That function is given in Listing 8. (Lines 141 and 156 are long enough to wrap in this book, but j...
{caption: "Listing 8 (Sea Battle `fire` function).", number-from: 138}
```miniscript
// Fire at the other player's map.
// Print HIT or MISS, and return the name of the ship sunk (if any)
Player.fire = function(pos, otherPlayer)
if otherPlayer.myMap[pos] == EMPTY or otherPlayer.myMap[pos] == MISS then
print "Miss!"
otherPlayer.myMap[pos] = MISS
self.targetMap[pos] = MISS
else
print "HIT!"
wait
otherPlayer.myMap[pos] = HIT
self.targetMap[pos] = HIT
// Check for a ship that's been sunk.
// Every ship has a unique letter, so we know it's sunken if
// that letter is not found anywhere in the map.
for name in otherPlayer.shipsLeft
if otherPlayer.myMap.indexOf(name[0]) == null then
// This ship is not found, so it has been sunk.
otherPlayer.shipsLeft.remove otherPlayer.shipsLeft.indexOf(name)
return name
end if
end for
end if
end function
p = new Player
p.init
p.writeToMap "DDDD", "C4", true, false
p2 = new Player
p2.init
p2.fire "E6", p // should miss
p2.fire "E4", p // should hit
p.print
```
When you run that, it should fire two shots: the first one at E6 misses, but the second one at E4 hits. The result should look like the figure below.
{width:"75%"}
![Output after Listing 8](battle-l8.png)
If you want to test this more thoroughly, try including enough `fire` calls to cover the entire ship. Also, print out the result of the last one; the function should return the name of a ship sunk, or `null` if nothing is sunk.
The last function we need for our Sea Battle game is one to choose the computer's move. This is the AI (artificial intelligence) of the game... though our version is really not very intelligent. It just chooses a move randomly.
{caption: "Listing 9 (Sea Battle AI).", number-from: 163}
```miniscript
// Here is the "AI" of the game, that is, the code that decides
// what to do on the computer's turn.
ai = function()
while true // repeat until we find spot we haven't tried
r = rows[rnd * rows.len]
c = cols[rnd * cols.len]
if comp.targetMap[c + r] == EMPTY then break
end while
print "I fire at " + c + r + "..."