03.01.2013 Views

Chapter 1

Chapter 1

Chapter 1

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Note<br />

9.2.3 The Controller<br />

As described above, 'my fleet' appears to be a rather odd name in this<br />

context. This is because it's carried over from the full Battleships game in<br />

which the 'my fleet' object really does represent the player's fleet, which, the<br />

opponent, on another phone, is trying to sink. Also, note that the program<br />

refers to a shot at a particular square as a 'hit request'. A square marked as<br />

'hit' is one that has been fired at: it doesn't mean that a ship at that square<br />

has been successfully struck.<br />

In the MVC paradigm, we saw the following:<br />

� The model contains the application data – in Solo Ships, most of the model is<br />

contained in the game engine, though the view's zoom state is also model data.<br />

� The view draws the model in a way that is meaningful to the human user.<br />

� The controller updates the model, and requests view redraws, in response to various<br />

kinds of event – including user interactions.<br />

The CGameController class implements controller functionality. It owns the model<br />

(CGameEngine, and an iZoomFactor member), and ensures that the model state is saved<br />

to file. Finally, the controller handles all events that cause the model (and therefore the view)<br />

to change. In Solo Ships, there are two types of events:<br />

� User interactions from the fleet view – handled by the controller as the implementer of<br />

the MGameViewCmdHandler interface.<br />

� Commands from the app UI – most app UI CmdXxxL() functions are prechecked by<br />

the app UI, and then implemented by some controller function.<br />

For many applications, a separate controller class like this isn't needed. It's OK to put many<br />

of the controller functions inside the app UI or the model, provided that all the persistent data<br />

is inside the model.<br />

9.3 Engine Classes<br />

We've seen about as much of the high-level view as we can realistically take in. Now it's time<br />

to get down to some more detail. Let's start with the engine, which is declared in engine.h<br />

and implemented in engine.cpp.<br />

There are three classes (as shown in Figure 9.3):<br />

Class Description<br />

CGameEngine The engine itself, which includes two fleets(iMyFleet and<br />

iOppFleet), and some utility functions.<br />

TFleet A fleet. The same class is used to represent'my fleet', and to represent<br />

the 'opponent fleet'.<br />

TShip A ship. A ship object contains a data member indicating whether it's a<br />

Battleship (the largest ship), Cruiser, Destroyer, or Frigate (the<br />

smallest).<br />

In UML, that's

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

Saved successfully!

Ooh no, something went wrong!