Object Oriented Programming Review

A Little More Information Than You Asked For

I wrote this to improve my programming skills. There are a few things in C++ that I hadn't used since college so I thought I should really work on that. Hopefully it will be helpful to someone else. An image of serial output has been included near the bottom. Down at the very bottom are ideas for expanding the program should you choose to do so.


I will not do a step by step breakdown as the documentation and inline commenting pretty much does this already. The program should be fairly easy to read through.

This is a fairly basic demo of:

  • class inheritance
  • polymorphism
  • Also (partially) included is a template function.

If you are very new to C++ this may very well be a little over your head, but if you have played around with it a bit and would like to learn about these topics I hope this will be very easy for you to follow and understand.

This is a fairly simple game. It is nothing special but it is intended to be something you can work forward from and make into something better. Hook up your mbed, use TeraTerm (or some terminal program) to communicate and you should see the game (after compile). It is a very simple Action RPG (lol). Everything happens automatically in this initial version and there is no user input at all.

Program Flow

The code works (in general) like this:

  • Spawn player (set up all player variables)
  • Roll (choose between) for basic enemy or elite enemy
  • Delete currently spawned enemy
  • Spawn enemy (basic or elite)
  • Communicate enemy info to player class

Combat begins:

  • Display round number
  • Player rolls for damage (crit roll then damage roll)
  • Current enemy takes damage (after dodge roll and if elite enemy, armour takes damage before health)
  • Get updated enemy health info
  • Send updated enemy health info to player class
  • Enemy rolls for damage (crit roll then damage roll)
  • User takes damage (one third of roll)
  • Check to see if player has been killed
  • If player is dead exit combat, otherwise increment round count

If player is dead:

  • Make fun of player and pretend to do complicated bioengineering.
  • Delete spawned player
  • Spawn new player
  • Reset round count

If user is still alive:

  • Add experience points. This also adds bonus "rest" health.
  • Check to see if the player has enough experience points to level up (and level up if there are enough points).
  • Reset round count
  • Roll for basic or elite enemy
  • Delete current enemy
  • Spawn new enemy
  • Start combat again with new enemy

The Libraries

The following libraries are fully documented. See API documentations for explanation of class functions.

This is the base class that all living things (well, pretend living anyway) are derived from:

Import librarylife_entity

Life entity (base class). Written for OOP Review.

The player class:

Import libraryplayer

Player class. Written for OOP Review. Derived from life_entity.

The basic enemy class:

Import libraryenemy

Enemy class. Written for OOP Review. Derived from life_entity.

The elite enemy class:

Import libraryarmoured_vehicle

Armoured vehicle class. For OOP Review. Derived from life_entity.

Example Program

Import programOOP_Review

Example program for Object Oriented Programing Review cookbook page.

Import program

00001 /*
00002     Object Oriented Programing Review
00004     Refreshing my memory on things I
00005     haven't touched since college.
00007     This version is intended for public use.
00008     You may use this in any way you choose, free of charge.
00009     No permission is required (written or verbal), and you
00010     do not HAVE to attribute.  It would however, be very
00011     nice if you would mention me in any product or project
00012     you may create with this.
00014     Aaron Goselin 2011
00015 */
00017 #include "mbed.h"
00018 #include "life_entity.h"
00019 #include "player.h"
00020 #include "enemy.h"
00021 #include "armoured_vehicle.h"
00023 Serial PC(USBTX, USBRX);
00025 // Pointer to player's class
00026 // Does not change to any other class
00027 // Only resets on death (delete then new)
00028 player *user = new player();
00030 // Pointer to the current enemy.
00031 // Can change from enemy to armoured_vehicle (or the other way around)
00032 // Also resets on death (delete and new)
00033 // Pointer to user class is always passed to the current enemy's
00034 // constructor.
00035 life_entity *currentEnemy;
00037 int main() 
00038 {
00039     PC.baud(230400);
00041     // Setting RTC for rand()
00042     set_time(1256729737);
00044     int enemyHealth = 0;
00045     char enemyLevel = 0x00;
00046     int roll = 0;  // Damage roll
00047     int roundCount = 1;  // Round counter
00048     char thereIsCake = 0x00;
00050     while(1) 
00051     {
00052         srand ( time(NULL) );
00053         // Spawn elite enemy
00054         if( (rand() % 1000) > 200 )
00055         {
00056             delete currentEnemy;
00057             currentEnemy = new armoured_vehicle(user);
00058         }
00059         // Spawn normal enemy
00060         else
00061         {
00062             delete currentEnemy;
00063             currentEnemy = new enemy(user);
00064         }
00066         enemyHealth = currentEnemy->getHealth();
00067         enemyLevel = currentEnemy->getLevel();
00069         // This isn't really used in the version of code
00070         // being added to the site but you can use it
00071         // however you like.
00072         user->setCurrentEnemy(enemyHealth, enemyLevel);
00074         // Keep going through the rounds until someone is dead
00075         while(currentEnemy->getHealth() > 0)
00076         {
00077             printf("ROUND #%i\n", roundCount);
00078             printf("----------\n");
00079             // User rolls for damage
00080             roll = user->rollDamage();
00081             // Current enemy takes the hit
00082             currentEnemy->takeDamage(roll);
00083             // Get current enemy health
00084             enemyHealth = currentEnemy->getHealth();
00085             // Send update on enemy health status to user
00086             user->setCurrentEnemy(enemyHealth);
00087             // Enemy rolls for damage
00088             roll = currentEnemy->rollDamage();
00089             // User takes the hit (one third of it anyway)
00090             user->takeDamage(roll / 3);
00091             // Check to see if the user is dead
00092             thereIsCake = user->isDead();
00093             // Exit if user is dead (they promised cake though)
00094             if(thereIsCake) break;
00095             wait(.5);
00097             roundCount++;
00098         }
00100         // If the user dies restart everything
00101         if(thereIsCake)
00102         {
00103             delete user;
00104             user = new player();
00105             roundCount = 1;
00106         }
00107         // User is still alive.  Crap, no cake yet.
00108         else
00109         {        
00110             // User won, so add xp (also adds a bit of health)
00111             user->addExperience();
00112             // Check to see if user leveled up
00113             user->isLevelUp();
00114             // Reset round counter
00115             roundCount = 1;
00116         }
00118     }
00119 }



What's Next?

So, you've read through this junk gagging through my idiot sense of humor all the while and now you want to modify the program and add to it until it is the most awesome game ever to be on the mbed? Cool! I think you should go for it.

If that's true but you aren't quite sure where to go from here then keep reading for an extra few seconds (or minutes depending on how slow/fast you read).

Ideas for expansion of this program:

  • Really, playing a game in terminal isn't ideal. Consider any sort of external display/interface device (LCD, OLED, LED matrix, motorcycle engines and pulleys hooked up to punch you in the face when you lose, whatever you can think of).
  • Health bars are nicer than numbers (although you can draw numbers over or by the bars)
  • Same with experience bars
  • Items! Create a new base class for items, then create derived classes for your items (health packs, guns(damage modifiers?), armour vests, etc.)
  • Events! Create yet another base class for events, and again create derived classes this time for your events. There could be traps (roll for traps), bonus events (meaning good things like health refills or extra crit chance), etc.
  • More than one enemy at a time. Final Fantasy style combat anyone?
  • Again with the FF gameplay, AI teammates.
  • Improvements to xp and health/armour calculations (lets be honest, I didn't put a lot of effort into them).
  • More xp for enemy's higher than your level, and less for enemy's lower than your level.
  • User input! Obviously that's a no brainer.
  • Dialog options (chat with the enemy...annoy the enemy).
  • Anything
  • Seriously, anything. If you can think it up, go ahead and throw it in.