1.7.1 Game Stats 3.0程序简介
Game Stats 3.0程序使用常量表示值。首先,程序计算玩家的得分,然后计算策略游戏中单位升级所需的花费。图1-8显示了程序结果。
从Course Technology网站(www.courseptr.com/downloads)或本书合作网站(http://www. tupwk.com.cn/downpage)上可以下载到该程序的代码。程序位于Chapter 1文件夹中,文件名为game_stats3.cpp。
|
| 图1-8 每次计算都使用了常量,可以使代码含义更加清晰易懂 |
- // 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;
- }