03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TInt iLength; // Length - determined from type<br />

TInt iRemaining; // Remaining unit squares<br />

// Calculated members<br />

TInt iStartX, iStartY; // Start position<br />

TInt iDx, iDy; // Orientation vector<br />

};<br />

A ship's first attribute is its type, iType, which may be battleship, cruiser, destroyer, or<br />

frigate. The type governs the length – 4, 3, 2, or 1 squares, respectively.<br />

The relationship between type and length is encapsulated in the<br />

TShip::TShip(TShipType) constructor: you specify a valid ship type, and the<br />

constructor sets the length. There's also a default constructor, which is needed so that<br />

arrays of TShips can be constructed in TFleet: the default constructor sets the ship type<br />

to unknown and the length to zero.<br />

A ship has a starting square and a direction vector, for example, a battleship starting at (3, 4)<br />

with a direction vector of (0, 1) will cover squares (3, 4), (3, 5), (3, 6), and (3, 7). Clearly, the<br />

same battleship could also be described by a starting square of (3, 7) and a direction vector<br />

or (0, −1) – it doesn't matter.<br />

The above information about ships is used during hit detection to work out whether a hit<br />

request affects a particular ship (in iMyFleet). When a hit is registered, I decrement the<br />

iRemaining count, which is initially set to the length of the ship. I can then tell when the<br />

ship has been entirely sunk.<br />

The ship-type enumeration is worth a second look. TShip::TShipType is passed to and<br />

returned from many functions in the engine API. The ship type is an enumerated type,<br />

including constants not only for the four real ship types, but artificial values for unknown and<br />

sea. These are there to ensure that the ship-type enumeration is compatible with the square<br />

states in TFleet.<br />

Note<br />

TShip::TShipType is represented in the engine's APIs and is used<br />

throughout the higher-level code that plays Solo Ships and, indeed,<br />

Battleships. The TFleet square states are private to TFleet. The<br />

requirement to keep TFleet's square states in sync with TShipType is a<br />

little awkward: initially I judged it tolerable in this small design, but on<br />

reflection I'm guilty of microoptimization here – see the comment below.<br />

Another small-time design practice is that I don't hide the data members of<br />

TShip terribly well: in some contexts, I would have taken more trouble to<br />

make data members private, and to provide public accessors. Again, that<br />

didn't seem worth the trouble here.<br />

There's nothing too difficult about TShip's implementation: check out engine.cpp for<br />

details.<br />

9.3.2 The Fleet Class<br />

A fleet is an 8 × 8 grid of squares containing up to 10 ships. Here's the declaration:<br />

class TFleet

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!