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

Creatures :: DynObjCreation

By: frankV on Nov 13th, 2012  |  syntax: C++  |  size: 4.40 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*  Creature Feature  */
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <algorithm>        // for forcing input to lowercase
  6. #include <vector>           // for creating/storing dynamic sub-class objects
  7. #include <fstream>          // for file read
  8. #include <unistd.h>         // for using system commands and invoking delays
  9.  
  10. #include "balrog.h"
  11. #include "cyberdemon.h"
  12. #include "elf.h"
  13. #include "human.h"
  14.  
  15. #include "functions.h"
  16.  
  17. using namespace std;
  18.  
  19. typedef Creature* CreaturePtr;
  20. void loadPlayers (ifstream& is, vector<CreaturePtr>& GoodGuys,
  21.                   vector<CreaturePtr>& BadGuys);
  22. void battle (vector<CreaturePtr>& attacks, vector<CreaturePtr>& defends);
  23.  
  24.  
  25.  
  26. // main delegator
  27. int main ()
  28. {
  29.     string playerName, fileName, move;
  30.     ifstream fileIn;
  31.    
  32.     vector<CreaturePtr> GoodGuys;
  33.     vector<CreaturePtr> BadGuys;
  34.    
  35.     title();
  36.    
  37.     cout << setw(37) << "Enter your name: ";
  38.     cin >> playerName;
  39.     cout << endl;
  40.     cout << setw(23) << playerName << ", prepare for battle!\n" << endl;
  41.    
  42.     /*
  43.      
  44.     chooseCharacter();
  45.    
  46.     sleep(5);
  47.     system("clear");
  48.      
  49.     */
  50.    
  51.     cout << "Enter a filename, " << playerName << " the "
  52.          << characterType(playersCharacter) << "... (include .txt)" << endl;
  53.    
  54.     cout << "Filename: ";
  55.     cin >> fileName;
  56.    
  57.     while (!validateFileExtension(fileName)) {
  58.        
  59.         cout << "File type is invalid. TXT files only." << endl;
  60.         cout << "Enter a filename to open (include .txt), or be slain! : ";
  61.         cin >> fileName;
  62.        
  63.         fileIn.open( fileName.c_str() );
  64.     }
  65.    
  66.     if (fileIn.fail() ) {
  67.         cout << "Failed to open input file." << endl;
  68.         cout << "Enter a filename to open (include .txt), or be slain! : ";
  69.         cin >> fileName;
  70.        
  71.         fileIn.open( fileName.c_str() );
  72.     }
  73.    
  74.     cout << "file opened" << endl; // DELETE ME!!!
  75.    
  76.     loadPlayers (fileIn, GoodGuys, BadGuys);
  77.    
  78.     cin >> move;
  79.    
  80.     fileIn.close();
  81.    
  82.     return (0);
  83. }
  84.  
  85.  
  86.  
  87. void loadPlayers (ifstream& is, vector<CreaturePtr>& GoodGuys,
  88.                   vector<CreaturePtr>& BadGuys) {
  89.    
  90.     int species, strength, hitpoints;
  91.     int count = 0;
  92.     string line;
  93.    
  94.     if( is.good() ){
  95.        
  96.         for (int i = 0; i < 4; i++) {
  97.            
  98.             cout << "in good guy while loop... I should read data!" << endl; // DELETE ME!!!
  99.        
  100.             is >> species >> strength >> hitpoints;
  101.             cout << species << strength << hitpoints;
  102.            
  103.             // if human type
  104.             if(species == 0)
  105.             {
  106.                 cout << "did I make it here? ";
  107.                 GoodGuys.push_back(new Human(strength, hitpoints));
  108.                 count ++;
  109.                 cout << "Good Human " << i << endl;
  110.             }
  111.             // if cyberdemon type
  112.             else if(species == 1)
  113.             {
  114.                 GoodGuys.push_back(new CyberDemon(strength, hitpoints));
  115.                 count ++;
  116.                 cout << "Good CyberDemon " << i << endl;
  117.             }
  118.             // if balrog type
  119.             else if(species == 2)
  120.             {
  121.                 GoodGuys.push_back(new Balrog(strength, hitpoints));
  122.                 count ++;
  123.                 cout << "Good Balrog " << i << endl;
  124.             }
  125.             // if elf type
  126.             else if(species == 3)
  127.             {
  128.                 GoodGuys.push_back(new Elf(strength, hitpoints));
  129.                 count ++;
  130.                 cout << "Good Elf " << i << endl;
  131.             }
  132.             // if data is invalid
  133.             else {
  134.                 cout << is << " ";
  135.                 cout << "Error in file. Invalid creature. " << endl;
  136.                 return;
  137.             }// end else
  138.         }
  139.            
  140.         if (count >= 4) {
  141.            
  142.             for (int i = 0; i < 4; i++) {
  143.                
  144.                 cout << "in bad guy loop... I should read data!" << endl; // DELETE ME!!!
  145.                
  146.                 is >> species >> strength >> hitpoints;
  147.                
  148.                 if (species > 0) {
  149.                     cout << "bad species greater than zero" << endl;
  150.                 }
  151.                
  152.                 else {
  153.                     cout << "Error in file. Invalid creature. " << endl;
  154.                     return;
  155.                 }// end else
  156.             }
  157.         }
  158.     }
  159. }
clone this paste RAW Paste Data