1.7.1 Game Stats 3.0程序简介

2013-10-07 14:42:27 · 作者: · 浏览: 371

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  每次计算都使用了常量,可以使代码含义更加清晰易懂

  1. // Game Stats 3.0   
  2. // Demonstrates constants  
  3. #include <iostream>   
  4. using namespace std;   
  5. int main()   
  6. {   
  7.          const int ALIEN_POINTS = 150;   
  8.          int aliensKilled = 10;   
  9.          int score = aliensKilled * ALIEN_POINTS;   
  10.          cout << "score: " << score << endl;   
  11.          enum difficulty {NOVICE, EASY, NORMAL, HARD, UNBEATABLE};   
  12.          difficulty myDifficulty = EASY;   
  13. enum shipCost {FIGHTER_COST = 25, BOMBER_COST, CRUISER_COST = 50};   
  14.          shipCost myShipCost = BOMBER_COST;   
  15.          cout << "\nTo upgrade my ship to a Cruiser will cost "   
  16. << (CRUISER_COST - myShipCost) << " Resource Points.\n";   
  17. return 0;   
  18. }