< Previous | Contents | Next >
Introducing the Game Stats 3.0
The Game Stats 3.0 program uses constants to represent values. First the program calculates a player’s score, and then it calculates the upgrade cost of a unit in a strategy game. Figure 1.8 shows the results.
You can download the code for this program from the Course Technology website (www.courseptr.com/downloads). The program is in the Chapter 1 folder; the filename is game_stats3.cpp.
30 Chapter 1 n Types, Variables, and Standard I/O: Lost Fortune
Figure 1.8
Each calculation involves a constant, making the code behind the scenes clearer.
// Game Stats 3.0
// Demonstrates constants
#include <iostream> using namespace std;
int main()
{
const int ALIEN_POINTS = 150; int aliensKilled = 10;
int score = aliensKilled * ALIEN_POINTS; cout << "score: " << score << endl;
enum difficulty {NOVICE, EASY, NORMAL, HARD, UNBEATABLE};
difficulty myDifficulty = EASY;
enum shipCost {FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50};
shipCost myShipCost = BOMBER_COST;
cout << "\nTo upgrade my ship to a Cruiser will cost "
<< (CRUISER_COST - myShipCost) << " Resource Points.\n";
return 0;
}