Game Development Reference
In-Depth Information
P UTTING I TIN C ODE
Although this may seem obvious, I may as well drop it in. If you were to put the
entire AI for Matching Pennies or the above hypothetical fighting game into code,
this is what it might look like.
typedef enum { MV_HEADS = 0,
MV_TAILS = 1} PENNIES_MOVE;
PENNIES_MOVE MyGame::SelectPenniesMove()
{
PENNIES_MOVE ThisMove = PENNIES_MOVE(rand() % 2);
return ThisMove;
}
It seems kind of pointless, doesn't it? We have one single function that returns
0 or 1, in this case, representing heads or tails. Incidentally, if you make the func-
tion return three choices instead of two, you have the AI for Rock-Paper-Scissors.
typedef enum { MV_ROCK = 0,
MV_PAPER = 1,
MV_SCISSORS = 2} RPS_MOVE;
RPS_MOVE MyGame::SelectRPSMove()
{
RPS_MOVE ThisMove = RPS_MOVE(rand() % 3);
return ThisMove;
}
As I suggested in Chapter 1, games such as these only have the appearance of
playing against another person. If your best bet is to play randomly, the only choice
is whether to do so or not. If you choose to play randomly, and your opponent does
so as well, then we do not have a game that meets the condition of “interesting
choices.� It also becomes tiring rather quickly. (I do not believe this is a coincidence.)
Search WWH ::




Custom Search