Game Struct Reference

List of all members.


Detailed Description

the entire game implementation

Definition at line 425 of file main.cpp.


Public Member Functions

void enemy_turn ()
 the enemy's logic for its turn in battle
 Game (const char *heroname="")
 constructor - creates all actors and stars the game by calling the Game::play() method
void next_enemy ()
 spawns a random enemy
void play ()
 the main game loop logic
void player_turn ()
 the player's logic for its turn in battle
 ~Game ()
 destructor - releases memory used by the actor list

Public Attributes

vector< Actor * > actors_
 the list of actors that are in the game
unsigned int enemy_
 just an index into the actors_ list for the current enemy
bool gameover_
 tells whether or not the game has ended

Constructor & Destructor Documentation

Game::Game ( const char *  heroname = ""  ) 

constructor - creates all actors and stars the game by calling the Game::play() method

Parameters:
heroname - an optional name for the player

Definition at line 440 of file main.cpp.

00441     {
00442         // name the player correctly
00443         string realheroname = "Hero";
00444         if (strlen(heroname))
00445         {
00446             realheroname = "Hero known as ";
00447             realheroname += heroname;
00448         }
00449 
00450         // player is always the first actor
00451         actors_.push_back(
00452             new Actor(
00453                 realheroname.c_str(),
00454                 100,
00455                 1,
00456                 15,
00457                 0,
00458                 0,
00459                 1)
00460             );
00461 
00462         // add the enemies
00463         //                           name           hp      ap      dp      xp      gold    lv
00464         // -------------------------------------------------------------------------------------
00465         actors_.push_back(new Actor("Thief",        20,     5,      5,      0,      0,      1));
00466         actors_.push_back(new Actor("Barbarian",    30,     10,     10,     0,      0,      1));
00467         actors_.push_back(new Actor("Wizard",       40,     20,     20,     0,      0,      1));
00468         actors_.push_back(new Actor("Cleric",       10,     50,     50,     0,      0,      1));
00469 
00470         gameover_ = false;
00471 
00472         play();
00473     }

Here is the call graph for this function:

Game::~Game (  ) 

destructor - releases memory used by the actor list

Definition at line 476 of file main.cpp.

00477     {
00478         for (vector<Actor*>::iterator iter = actors_.begin(); iter != actors_.end(); iter++)
00479         {
00480             delete (*iter);
00481         }
00482     }


Member Function Documentation

void Game::enemy_turn (  ) 

the enemy's logic for its turn in battle

Definition at line 608 of file main.cpp.

00609     {
00610         //if (GameOver == 1) return 1;
00611 
00612         // if the enemy is dead, we just exit
00613         if (actors_.at(enemy_)->hp_ <= 0)
00614         {
00615             return;
00616         }
00617 
00618         int action = roll_die(3) - 1; // pick a random action (0 - 2)
00619 
00620         cout
00621             << HR
00622             << INDENT << "THE ENEMY TAKES IT'S TURN" << BR
00623             << HR;
00624 
00625         switch(action)
00626         {
00627             case 0:
00628             {
00629                 // attack
00630                 if (actors_.at(enemy_)->deal_damage(actors_.at(0)))
00631                 {
00632                     cout << INDENT << "*** A CRITICAL BLOW! ***" << BR;
00633                 }
00634             } break;
00635 
00636             case 1:
00637             {
00638                 // defend
00639                 actors_.at(enemy_)->defense_up();
00640             } break;
00641 
00642             case 2:
00643             {
00644                 // idle or taunt
00645                 /*
00646                 10% chance that the enemy will taunt
00647                 */
00648 
00649                 int tauntchance = roll_die(100);
00650                 if (tauntchance >= 1 && tauntchance <= 10)
00651                 {
00652                     actors_.at(enemy_)->taunt(actors_.at(0));
00653                 }
00654                 else
00655                 {
00656                     cout << INDENT << "The " << actors_.at(enemy_)->name_ << " just sits there..." << BR;
00657                 }
00658             } break;
00659 
00660             default: break;
00661         }
00662 
00663         cout << HR;
00664     }

Here is the call graph for this function:

void Game::next_enemy (  ) 

spawns a random enemy

Definition at line 485 of file main.cpp.

00486     {
00487         // pick a random enemy
00488         enemy_ = 1 + rand() % (actors_.size() - 1);
00489 
00490         // randomize the rewards
00491 
00492         // 1 to 5000 possible gold
00493         actors_.at(enemy_)->gold_ =
00494             roll_die(1000) +
00495             roll_die(1000) +
00496             roll_die(1000) +
00497             roll_die(1000) +
00498             roll_die(1000);
00499 
00500         // 10 to 1500 xp possible
00501         actors_.at(enemy_)->xp_ = 10 +
00502             roll_die(10) +
00503             roll_die(20) +
00504             roll_die(360) +
00505             roll_die(1100);
00506 
00507         // player level - 1
00508         long playerlevel = actors_.at(0)->level_;
00509 
00510         if (playerlevel > 5 )
00511         {
00512             // enemy level can be anywhere from the player's level - 1 to player's level + 2
00513             actors_.at(enemy_)->level_ = (playerlevel - 1) + roll_die(playerlevel + 2);
00514         }
00515 
00516         // pick a random direction for the enemy to attack from
00517         int atkdir = rand() % 3;
00518 
00519         clear_screen();
00520         cout
00521             << HR
00522             << INDENT << "WARNING! WARNING! WARNING! WARNING! WARNING!" << BR
00523             << HR
00524             << INDENT << "A " << actors_.at(enemy_)->name_ << " is attacking from the " << direction[atkdir] << "!" << BR
00525             << INDENT << "Prepare for battle!" << BR;
00526 
00527         // player can sense the level of enemies once they are >= level 10
00528         // player can sense the ap/dp of enemies once they are >= level 15
00529         // player can sense the hp of enemies once they are >= level 20
00530         if (playerlevel >= 10)
00531         {
00532             cout
00533                 << HR
00534                 << INDENT << "YOUR [SENSE] ABILITY TELLS YOU" << BR
00535                 << HR
00536                 << INDENT << "* The " << actors_.at(enemy_)->name_ << " has a level of " << actors_.at(enemy_)->level_ << BR;
00537             if (playerlevel >= 15)
00538             {
00539                 cout
00540                     << INDENT << "* The " << actors_.at(enemy_)->name_
00541                     << " has an attack/defense of "
00542                     << actors_.at(enemy_)->ap_ << "/"
00543                     << actors_.at(enemy_)->dp_ << BR;
00544             }
00545 
00546             if (playerlevel >= 20)
00547             {
00548                 cout
00549                     << INDENT << "* The " << actors_.at(enemy_)->name_ << " has " << actors_.at(enemy_)->hp_ << " health" << BR
00550                     << HR;
00551             }
00552         }
00553     }

Here is the call graph for this function:

void Game::play (  ) 

the main game loop logic

Definition at line 667 of file main.cpp.

00668     {
00669         srand(time(0));
00670 
00671         // spawn the first enemy
00672         next_enemy();
00673 
00674         while(!gameover_)
00675         {
00676             // game ends if player is out of hp
00677             if (actors_.at(0)->hp_ <= 0)
00678             {
00679                 gameover_ = true;
00680                 cout << "*** You have been defeated! ***\n";
00681                 get_user_input_string("Press Enter to Continue.");
00682             }
00683             else
00684             {
00685                 player_turn();
00686 
00687                 // because the player can choose to end the game at any time its their turn,
00688                 // we need to see if we can still continue
00689                 if (!gameover_)
00690                 {
00691                     // the enemy cannot get a turn if it was killed
00692                     if (actors_.at(enemy_)->hp_ > 0)
00693                     {
00694                         enemy_turn();
00695                     }
00696 
00697                     // if the enemy was killed
00698                     if (actors_.at(enemy_)->hp_ <= 0)
00699                     {
00700                         cout
00701                             << HR
00702                             << "The " << actors_.at(enemy_)->name_ << " collapses to the ground, lifeless and bloodied." << BR
00703                             << HR;
00704                         // gain the rewards
00705                         actors_.at(0)->get_rewards_from(actors_.at(enemy_));
00706 
00707                         // spawn another enemy
00708                         next_enemy();
00709                     }
00710                 }
00711             }
00712         }
00713 
00714         cout
00715             << INDENT << "*** THANKS FOR PLAYING! ***" << BR;
00716     }

Here is the call graph for this function:

void Game::player_turn (  ) 

the player's logic for its turn in battle

Definition at line 556 of file main.cpp.

00557     {
00558         char action = '?';
00559 
00560         cout
00561             << HR
00562             << INDENT << "SELECT AN ACTION" << BR
00563             << HR
00564             << INDENT << "(F)ight" << BR
00565             << INDENT << "(D)efend" << BR
00566             << INDENT << "(V)iew Status" << BR
00567             << INDENT << "(Q)uit" << BR
00568             << HR;
00569             cout << INDENT << ": ";
00570 
00571         action = get_user_input_char("");
00572 
00573         switch(action)
00574         {
00575             case 'f':
00576             case 'F':
00577             {
00578                 if (actors_.at(0)->deal_damage(actors_.at(enemy_)))
00579                 {
00580                     cout << INDENT << "*** A CRITICAL BLOW! ***" << BR;
00581                 }
00582             } break;
00583 
00584             case 'd':
00585             case 'D':
00586             {
00587                 actors_.at(0)->defense_up();
00588             } break;
00589 
00590             case 'v':
00591             case 'V':
00592             {
00593                 actors_.at(0)->show_status();
00594                 return;
00595             } break;
00596 
00597             case 'q':
00598             case 'Q':
00599             {
00600                 gameover_ = true;
00601                 return;
00602             } break;
00603             default: break;
00604         }
00605     }

Here is the call graph for this function:


Member Data Documentation

vector<Actor*> Game::actors_

the list of actors that are in the game

Definition at line 428 of file main.cpp.

unsigned int Game::enemy_

just an index into the actors_ list for the current enemy

Definition at line 434 of file main.cpp.

tells whether or not the game has ended

Definition at line 431 of file main.cpp.


The documentation for this struct was generated from the following file:

Generated on Sat Aug 29 17:55:33 2009 for Simple Battle by  doxygen 1.5.5