Quick Death

Started by
-1 comments, last by 5MinuteGaming 18 years, 9 months ago
Small Simple Game For Beginners Well, I just thought it would be nice to give beginners something to work with albeit a graphicless pos but still. I kind of lost the time to do anything with this game so here is the code its a little less than 500 lines and extremely messy so it needs a lot of refactoring but go ahead and play around with it if you want.
//quick death
#include <cstdlib>
#include <list>
#include <iostream>

using namespace std;

typedef struct tagMan
{
       int strength;
       int training;
       int weapons;
       int health;
       bool armed;
       string parent;
       
}MAN,*MANPTR;

typedef struct tagResources
{
       // gold per turn
       int gpt;
                
}RESOURCE,*RESOURCEPTR;

typedef struct tagBarracks
{
       //men per turn
       int mpt;
       
       // gold per turn as maintainence per man
       int gpt;

       // cost per unit
       int cpu;

}BARRACKS,*BARRACKSPTR;

enum AITYPES {AGGRESSIVE,PASSIVE,SPENDTHRIFT,MONEYHOARDER,UNPREDICTABLE};

typedef struct tagUser
{
       string name; 
        
       // number of men total
       int total_men;
       
       // amount of gold total
       int total_gold;
       
       //resources
       tagResources res;
       //barracks
       tagBarracks bar;

       //a list of all the men the user owns
       list<MAN> men;
       
       //is this an AI user or a human player
       bool isHuman;
       
       //here is the automated management type
       AITYPES autotype;
       
}USER,*USERPTR;

typedef struct tagBattleTile
{
       int position;
       int layer;
       list<MANPTR> men;
       
}BTILE,*BTILEPTR;

typedef struct tagBattle
{
       list<BTILEPTR> tiles;
       list<USERPTR> players;
}BATTLE,*BATTLEPTR;

//declares
void initPlayer(USER*);
void recruit(USER&);
static void displayInit();
void displayMenu( string&, USER* );
void endTurn(USER&);
void addMen ( int, USER& );
void train ( USER& );
static void displayMan( MAN* );
static void displayMen( USER* );
void armmen( USER& );
void disband( USER& );
void battle( BATTLE& );
void addbattle( USER& );
void displayBattleMenu();
void battleaction( BATTLE&, USER& );
void retreat( BATTLE&, USER& );
void wait( BATTLE&, USER& );
USER *getNextPlayer();
bool checkWin( BATTLEPTR );
bool moveMen( BTILE, BTILE, BATTLEPTR, USERPTR, int );

//global user list 
list<USERPTR> g_playerlist;

//global battle queue
list<BATTLE> g_battles;

int main(int argc, char *argv[])
{
   
    string inptstr;
    USER* player1 = new USER;
    USER* bob = new USER;
    USER* joe = new USER;
    
    // initialize the players names
    bob->isHuman = false;
    bob->name = "Bob AI";
    joe->name = "Joe AI";
    joe->isHuman = false;
    player1->isHuman = true;
    player1->name = "Unknown Human";
    

    //AI specific initialization
    bob->autotype = AGGRESSIVE;

    initPlayer(bob);    
    initPlayer(joe);
    initPlayer(player1);
    displayInit();
    
    while (inptstr != "Exit" ) 
    {
          USER *playersturn = getNextPlayer();
          if( playersturn->isHuman ) 
          {
              displayMenu(inptstr,playersturn);
              if( inptstr == "EndTurn" ) endTurn(*playersturn);
              if( inptstr == "Recruit" ) recruit(*playersturn);
              if( inptstr == "ShowMen" ) displayMen(playersturn);
              if( inptstr == "Train"   ) train(*playersturn);              
              if( inptstr == "ArmMen"  ) armmen(*playersturn);   
              if( inptstr == "Disband" ) disband(*playersturn);
              if( inptstr == "Battle"  ) addbattle(*playersturn);     //send to the battle queue
          }else{
                printf("\nDoing AI's Turn\n");
                
          }    
          
    }
    
    return EXIT_SUCCESS;
}

void initPlayer(USER *tU )
{
     tU->res.gpt = 150;
     tU->total_gold = 0;
     tU->total_men = 0;
     tU->bar.mpt = 2;
     tU->bar.gpt = 1;
     tU->bar.cpu = 25;
     g_playerlist.push_back(tU);
}

USER* getNextPlayer()
{
      USER *toRet = *g_playerlist.begin();
      g_playerlist.pop_front();
      g_playerlist.push_back(toRet);
      
      //check battle queue 
      if( g_battles.size() > 0 ) 
      {
          //run through all battles
          while ( g_battles.size() > 0 )
          {
               battle( (*g_battles.begin()) );
               g_battles.pop_front();
          }
      }
      
      return toRet;    
}

static void displayInit() 
{
    printf("Hello World!\n");
    printf("My First Text Game with Dev-C++\n");
    printf("\n");
    printf("Quick Death - Strategy Game\n");
    printf("\tUse your resource management skills\n");
    printf("\tto create the biggest army and then\n");
    printf("\tbattle the forces of evil.\n");
}

void displayMenu( string &cmd, USER *tU ) 
{
     cout << "\nMain Menu - " << tU->total_gold << "\\" << tU->total_men << " - " << tU->name << endl;
     printf("- Recruit\n");
     printf("- Train\n");
     printf("- ArmMen\n");
     printf("- EndTurn\n");
     printf("- ShowMen\n");
     printf("- Disband\n");
     printf("- Battle\n");
     printf("- Exit\n");
     printf("ln-input: ");
     cin >> cmd;
}

void train( USER& tU ) 
{
     int cost=0;
     printf("\nThe Minimum amount would be %i gold per training point\n", tU.men.size()*25);
     printf("\nHow much money are you going to spend on training? ");
     cin >> cost;
     printf("\n");
     if( cost > 25 && cost <= tU.total_gold ) 
     {
          int distrib = (int)((cost / 25)/tU.men.size() + .9);
          tU.total_gold -= cost;
     
          for( list<tagMan>::iterator iter = tU.men.begin(); iter != tU.men.end(); iter++ )
          {
               (&*iter)->training += distrib;
          }    
     }
}

void endTurn( USER &tU)
{
     printf("\n");
     tU.total_gold += tU.res.gpt;
     printf("+%i\n", tU.res.gpt);
     tU.total_gold -= tU.bar.gpt * tU.total_men;
     printf("-%i\n", tU.bar.gpt * tU.total_men);
     tU.total_men += tU.bar.mpt;
     addMen(tU.bar.mpt,tU);
     
}
     
void addMen ( int num, USER& tU ) 
{
     tagMan toAdd;
     toAdd.strength = 100;
     toAdd.training = 0;
     toAdd.weapons = 0;
     toAdd.health = 100;
     toAdd.armed = false;
     toAdd.parent = tU.name;
    
     if( num > 0 ) 
     {
         tU.men.insert( tU.men.begin(), toAdd ) ;   
         printf("You have a new man\n");
         addMen ( --num, tU );
     }
}
     
void recruit(USER &tU) 
{
     int num=0;
     printf("Enter number of men to recruit? ");
     cin >> num;
     printf("\n");
     if ( !(num*tU.bar.cpu > tU.total_gold) )
     {
           tU.total_men += num;
           tU.total_gold -= num*tU.bar.cpu;
           printf("\n");
           addMen( num, tU);
     }else{
           printf("--> Not enough Gold! Cost is %i per man!\n", tU.bar.cpu);           
     }
}

void displayMan( MAN *tme)
{
       printf("%i/%i/%i/%i - %i\n",tme->strength,tme->training,tme->weapons,tme->health, tme->armed);
}

void displayMen( USER *tU )
{
     printf("\n");
     for( list<tagMan>::iterator iter = tU->men.begin(); iter != tU->men.end(); iter++ )
     {
          displayMan(&*iter);     
     }
     printf("\n");
}

void armmen( USER& tU )
{
     int num = 0;
     int it = 0;
     int armed = 0;
     
     printf("\nIt will cost %i to arm all your soldiers!\n", tU.men.size() * 225 );
     printf("How many troops would you like to arm? ");
     cin >> num;
     
     list<tagMan>::iterator iter = tU.men.begin();

     if( num <= tU.men.size() ) {
         do
         {
               if( !(&*iter)->armed && tU.total_gold > 224 ) 
               {
                   printf("One of your troops was armed!\n");
                   armed++;
                   (&*iter)->armed = true;
                   tU.total_gold -= 225;
               }
         } 
         while ( (++iter) != tU.men.end() && armed != num );
               
     }
}

void disband( USER& tU ) 
{
     int num;
     printf("So you want to get ride of some recruits!\n");
     printf("How many? ");
     cin >> num;
     if( num <= tU.men.size() )
     {
         for (int i=0; i < num; i++) 
         {
             tU.men.pop_front();
             tU.total_men--;
         }
     }
}

void addbattle( USER &tU )
{
     int number = 1;
     int choice = 1;
     USER *ai = 0;
     printf("Choose who you want to battle!\n");
     for( list<USERPTR>::iterator iter = g_playerlist.begin(); iter != g_playerlist.end(); iter++ ) 
     {
          //if not tU then add the users name to list with sequence number     
          if( tU.name != (*iter)->name ) 
          {
                  cout << number++ << ": " << (*iter)->name << endl;
          }
     }
     
     cin >> choice;
     
     if( choice > 0 && choice < g_playerlist.size() ) 
     {
         //check is the choice is actually tU 
         if( tU.name == (*g_playerlist.begin())->name ) 
         {
             //choose the next one         
             choice++;
         }
         
         //set the ai as this player
         list<USERPTR>::iterator iter = g_playerlist.begin();
         for( int i = 1; i < choice; i++) { iter++;}
         ai = *iter;
         cout << "You chose to battle " << ai->name << endl;
     }
     
     if(ai != 0 ) {
            BATTLE b;
            b.players.push_back(&tU);
            b.players.push_back(ai);
            g_battles.push_back(b); // copies the battle into the list
     }
     
}

void battle( BATTLE& b )  
{
     char format = 0;
     bool over = false;
     string inptstr;
     
     //initialize the battle field
     
     for( int pos=0,lay=0; ((lay*16) + pos) < 16 * 16; pos++ )
     {
          if( pos >=16 ) {
              pos=0;
              lay++;
          }else{
                BTILE bt;
                bt.position = pos;
                bt.layer = lay;
                b.tiles.push_back(&bt);
          }          
     }
     
     printf("Would you like to organize your troops? (Y/N)");
     cin >> format;
     
     if( format == 'Y' || format == 'y' )
     {
           printf("Your troops are all at position! %i, %i\n",0,0);
           int pos = 0;
           
           do {
              printf("Please enter a posistion from 1 to 16? ");               
              cin >> pos;
           }while( pos < 1 || pos > 16 );

           int men = 0;
           printf("How many men would you like to position there? ");
           cin >> men;

           // move the men to their posistions
           BTILE t;
           BTILE f;
           if( !moveMen(t,f,&b,0,men) )
           {
               printf("The Enemy Is Near\n");
           }    

     }else{
           printf("Skipping setup phase!\n");
     }
     
     while ( !over ) 
     {
           displayBattleMenu();
           cin >> inptstr;
           
           USER *player = *b.players.begin();
           b.players.pop_front();
           
           if( inptstr == "Retreat" ) over = true;
           if( inptstr == "Do Action")battleaction(b,*player);
           if( inptstr == "Retreat" )retreat(b,*player);
           if( inptstr == "Wait" )wait(b,*player);
                      
           over = checkWin(&b);
     }
}


bool checkWin( BATTLEPTR b ) 
{
     bool toRet = false;
     
     //make sure neither of the players has no more men
     
     for( list<USERPTR>::iterator iter = b->players.begin(); iter != b->players.end(); iter++ )
     {
          if( !((*iter)->total_men) ) 
          {
              printf("The Battle is Over");
              toRet = true;
          }
     }
          
     return toRet;
}

void displayBattleMenu() 
{
     printf("\nBattle Menu:\n");
     printf("-Retreat -Do Action -Wait\n");
     
}

void battleaction( BATTLE& b, USER& tU )
{
     
}

void retreat( BATTLE& b, USER& tU)
{
     
}

void wait( BATTLE& b, USER& tU)
{
     
}

bool moveMen( BTILE to, BTILE from, BATTLEPTR b, USERPTR tU, int nmen)
{
     bool toRet = false;
     //check the tile we're moving to if the current user isn't there then
     //the tile belongs to the enemy return false if the tile can't be moved 
     //to
     MANPTR mptr = *to.men.begin();
     if( to.men.size() > 0 && mptr->parent == tU->name ) 
     {
         //this is our army
         //combine the soldiers
         toRet = true;
         for( int i=0; i < nmen; i++ )
         {
              to.men.push_back(*from.men.begin());
              from.men.pop_front();
         }
     }
     
     return toRet;
}

This topic is closed to new replies.

Advertisement