This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 13th, 2012  |  syntax: C++  |  size: 8.32 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //  Created by Michael Gorman on 11/9/12.
  2. //  Copyright (c) 2012 Michael Gorman. All rights reserved.
  3. //
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <windows.h>
  8. #include <process.h>
  9. #include <conio.h>
  10. #include <time.h>
  11.  
  12. using namespace std;
  13.  
  14. int input;
  15. char askYesNo(string question);
  16.  
  17. class SpaceShip               //class definition -- defines a new type
  18. {
  19. public:
  20.     SpaceShip(int power = 0, string sname = "", string cname = "");
  21.     string shipName;
  22.     string m_computerName;
  23.         int m_power;
  24.     int m_apower;
  25.     int m_speed;
  26.     int m_engines;
  27.     int m_shields;
  28.     int m_weapons;
  29.         int m_x;
  30.         int m_y;
  31.         int m_z; //3d for the future.
  32.         int m_cx;
  33.         int m_cy;
  34.  
  35.     void LaunchPrep();           // member function prototype
  36.         void Flight();
  37.         void Charts();
  38.         void Movement(string moveCommand);
  39.         void doMovement();
  40. };
  41.  
  42. SpaceShip::SpaceShip(int power, string sname, string cname):
  43.     m_apower(power),
  44.     m_power(power),
  45.     m_computerName(cname),
  46.         m_speed(0),
  47.     m_engines(0),
  48.     m_shields(0),
  49.     m_weapons(0)
  50. {}
  51.  
  52.  
  53. void SpaceShip::LaunchPrep()       // member function definition
  54. {
  55.     string shipControl = "";
  56.    
  57.     while (shipControl != "q")
  58.     {
  59.             cout << "\nHello! I'm the Ship's Computer. You may call me " << m_computerName << "\n\n";
  60.  
  61.             while (true) {
  62.                 char startShip = askYesNo("Shall I start all the automated ship systems?");
  63.                 if (startShip == 'y')
  64.                     break;
  65.             }
  66.            
  67.             Sleep(500);
  68.             cout << "\n.";
  69.             Sleep(500);
  70.             cout << "\n..";
  71.             Sleep(500);
  72.             cout << "\n...";
  73.             Sleep(500);
  74.             cout << "\n....";
  75.             Sleep(500);
  76.             cout << "\n.....";
  77.             Sleep(500);
  78.             cout << "\n......" << endl;
  79.             Sleep(500);
  80.             cout << m_computerName << ": Okay, we're ready for launch preperation." << endl;
  81.             Sleep(500);
  82.  
  83.         while(true)
  84.         {
  85.             cout << m_computerName << ": \n\tShip's Status:\n\n";
  86.             cout << "\t\tTotal power: " << m_power << " Units | Available Power: " << m_apower << " Units" << endl;
  87.             cout << "\t\tPower Unit Assignment: Engines: " << m_engines << " Shields: " << m_shields << " Weapons: " << m_weapons << endl;
  88.  
  89.             cout << m_computerName << ": How would you like to assign power?\n\t(E for engine, S for Shields and W for weapons): ";
  90.             cin >> shipControl;
  91.        
  92.             if (shipControl == "e" || shipControl == "E") {
  93.                 m_engines += 1;
  94.                 m_apower -= 1;
  95.                 cout << m_computerName << ": Engines powered to: " << m_engines << endl; }
  96.             if (shipControl == "s" || shipControl == "S") {
  97.                 m_shields += 1;
  98.                 m_apower -= 1;
  99.                 cout << m_computerName << ": Shields powered to: " << m_shields << endl;}
  100.             if (shipControl == "w" || shipControl == "W") {
  101.                 m_weapons += 1;
  102.                 m_apower -= 1;
  103.                 cout << m_computerName << ": Weapons powered to: " << m_weapons << endl; }
  104.             if (m_apower == 0)
  105.                 break;
  106.             else if (shipControl != "w" && shipControl != "e" && shipControl != "s")
  107.                 cout << m_computerName << ": Sorry, that's not a valid system" << endl;
  108.  
  109.         }
  110.        
  111.         cout << m_computerName << ": The power levels have been assigned. Ship is ready for launch: ";
  112.         cin >> shipControl;
  113.         if (shipControl != "launch")
  114.             cout << "Huh?";
  115.         else
  116.             cout << "\nVROOOOOOM!" << endl;
  117.                         m_x = 12;
  118.                         m_y = 25;
  119.                         m_cx = m_x;
  120.                         m_cy = m_y;
  121.         shipControl = "q";
  122.     }
  123.    
  124. }
  125.  
  126. void SpaceShip::Flight()
  127. {
  128.     string flightCommand;
  129.         time_t starttime, curtime;
  130.         int done = 0;
  131.  
  132.         cout << m_computerName << ": Before you get too crazy, let me explain how flight works, it consits of these commands";
  133.         cout << "\n\tThrottle\tCourse\t\tJump";
  134.         cout << "\n\tCharts\t\tComms\t\t";
  135.         cout << "\n\tLock\t\tFire\t\tAutoFire";
  136.         cout << "\n\tLand\t\tPower\t\tEject";
  137.  
  138.         char flightHelp = askYesNo("\n\nDo you need an explaination of each function? ");
  139.         if (flightHelp == 'y')
  140.         {
  141.                 cout << endl << m_computerName << ": Throttle allows input as a percentage, this makes piloting more \n\tsimple, I will handle the rest for the time being.\n\tWhen you're more comfortable we can go over using individual thrusters.";
  142.                 cout << endl << m_computerName << ": Course is for plotting your destination. You'll do it with \n\tx, y and z coordinates. I handle the computation and\n\tspecifics for you.";
  143.                 cout << endl << m_computerName << ": Jump allows you activate your Ether Drive for faster travel.\n\tYou'll want to manually chart jumps around \n\tproblem areas.";
  144.                 cout << endl << m_computerName << ": Charts displays your Nav Computer.";
  145.                 cout << endl << m_computerName << ": Comms brings up the communication array";
  146.                 cout << endl << m_computerName << ": Lock, Fire and Autofire allow you to target and attack.";
  147.                 cout << endl << m_computerName << ": Land allows me to compute trajectory and beging the landing sequence.";
  148.                 cout << endl << m_computerName << ": Power lets you tweak your previously set power distribution.";
  149.                 cout << endl << m_computerName << ": Eject, well just be sure you're ready for the consequences of\n\tbeing thrown into space...";
  150.  
  151.         }
  152.        
  153.         cout << endl << m_computerName << ": That should be everything you need to get started.";
  154.  
  155.         while(true)
  156.         {
  157.                 cout << "\n<(Speed: " << m_speed << " Coordinates: " << m_x << "x " << m_y << "y)> ";
  158.        
  159.         time(&starttime);
  160.         do
  161.         {
  162.         time(&curtime);
  163.                 if(_kbhit())
  164.                 {
  165.                         cin >> flightCommand;
  166.  
  167.                         if (flightCommand == "charts")
  168.                                 SpaceShip::Charts();
  169.                         if (flightCommand == "throttle")
  170.                                 SpaceShip::Movement("throttle");
  171.                         if (flightCommand == "course")
  172.                                 SpaceShip::Movement("course");
  173.                 }
  174.  
  175.         } while((curtime - starttime) < 5);
  176.         if (m_speed > 0)
  177.         SpaceShip::doMovement();
  178.         }
  179. }
  180.  
  181. void SpaceShip::Charts()
  182. {
  183.         char space[150][150];
  184.        
  185.         for(int i = (m_x -10);i < (m_x + 10);i++) //row by 80.
  186.         {
  187.                 for(int j = (m_y - 20);j < (m_y + 20);j++) //columns by 100.
  188.                 {
  189.                         space[i][j]='.';
  190.                         space[m_x][m_y]='^';
  191.                         space[10][20]='*';
  192.                         space[12][24]='O';
  193.                         cout <<  space[i][j];
  194.                 }
  195.                 cout << endl;
  196.         }
  197.         return 0;
  198. }
  199.  
  200. void SpaceShip::doMovement()
  201. {
  202.                 if (m_cx > m_x)
  203.                         m_x += 1;
  204.                 if (m_cx < m_x)
  205.                         m_x -= 1;
  206.                 if (m_cy > m_y)
  207.                         m_y += 1;
  208.                 if (m_cy < m_y)
  209.                         m_y -= 1;
  210. }
  211.  
  212. void SpaceShip::Movement(string moveCommand)
  213. {
  214.         int throttle;
  215.         int cx, cy;
  216.  
  217.         if (moveCommand == "throttle")
  218.         {
  219.                 cout << "\nWhat percentage of throttle would you like to apply? ";
  220.                 cin >> throttle;
  221.                 m_speed = throttle;
  222.                 return;
  223.         }
  224.  
  225.         if (moveCommand == "course")
  226.         {
  227.                 cout << "\nWhat X coordinate would you like to set? ";
  228.                 cin >> cx;
  229.                 m_cx = cx;
  230.                 cout << "\nWhat Y coordinate would you like to set? ";
  231.                 cin >> cy;
  232.                 m_cy = cy;
  233.                 return;
  234.         }
  235. }
  236.  
  237.  
  238. char askYesNo(string question)
  239. {
  240.         char response;
  241.         do
  242.         {
  243.                 cout << question << " (y/n); ";
  244.                 cin >> response;
  245.         } while (response != 'y' && response != 'n');
  246.    
  247.         return response;
  248. }
  249.  
  250. int main()
  251. {
  252.     SpaceShip XGP(9, "The XGP", "Gillium");
  253.     SpaceShip XWING(4, "X-Wing", "R2D2");
  254.     SpaceShip ENTERPRISE(10, "The Enterprise", "Data");
  255.     SpaceShip DEATHSTAR(25, "The Death Star", "EVIL");
  256.  
  257.     cout << "Welcome to the Gorman Space Simulator. The following ships are available for test flights:" << endl;
  258.     cout << "\n\t1. The XGP";
  259.     cout << "\n\t2. An X-Wing Fighter";
  260.     cout << "\n\t3. The USS Enterprise";
  261.     cout << "\n\t4. The Death Star";
  262.     cout << "\nPlease input the number of the desired ship class: ";
  263.     cin >> input;
  264.    
  265.     switch (input) {
  266.         case 1:
  267.             XGP.LaunchPrep();
  268.                         XGP.Flight();
  269.             break;
  270.         case 2:
  271.             XWING.LaunchPrep();
  272.                         XWING.Flight();
  273.             break;
  274.         case 3:
  275.             ENTERPRISE.LaunchPrep();
  276.                         ENTERPRISE.Flight();
  277.                         break;
  278.         case 4:
  279.             DEATHSTAR.LaunchPrep();
  280.                         DEATHSTAR.Flight();
  281.                         break;
  282.         default:
  283.             cout << "Something went wrong...";
  284.             break;
  285.     }
  286.    
  287.        
  288.     cout << "\nTakeoff complete!";
  289.    
  290.    
  291.    
  292.     return 0;
  293. }
clone this paste RAW Paste Data