HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Mass Army Combat Simulator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
masssimulatorarmycombat

Problem

I am currently teaching myself C++ in my spare time. I created the below "game" to simulate combat between an army of humans and an army of skeletons. Is there a simpler or better way of creating this game? I would love for someone to "improve" my code so I can get a better idea of how to use C++.

I think a major improvement would be to allow damage to both armies during the combat. SO you could potentially loose skeletons or humans even during their own individual turns.

ORIGINAL CODE -

#include 
#include 
#include 

int main() {
    int HUMANS, SKELETONS, iSecret1, iSecret2, humanAttackPower, skeletonAttackPower;

    std::cout > HUMANS;
    std::cout > SKELETONS;

    /* initialize random seed: */
    srand (time(NULL));

    while( HUMANS > 0 && SKELETONS > 0) {
        /* generate secret number between 1 and 20: */
        iSecret1 = rand() % 20 + 1;
        humanAttackPower = rand() % HUMANS + 1;
        iSecret2 = rand() % 20 + 1;
        skeletonAttackPower = rand() % SKELETONS + 1;
        if( iSecret1 > iSecret2 ) {
            std::cout  SKELETONS ){ humanAttackPower = SKELETONS;}
            SKELETONS = ( SKELETONS - humanAttackPower );
            if( SKELETONS  HUMANS ){ skeletonAttackPower = HUMANS; }
            HUMANS = ( HUMANS - skeletonAttackPower );
            if( HUMANS <= 0 ){ HUMANS = 0; }
            std::cout << "Skeletons killed " << skeletonAttackPower << " humans!" << std::endl;
        }
        std::cout << HUMANS << " humans remain" << std::endl;
        std::cout << SKELETONS << " skeletons remain" << std::endl;
    }
    if( HUMANS <= 0 ) {
        std::cout << "The humans have been beaten!" << std::endl;
    }
    if( SKELETONS <= 0 ) {
        std::cout << "The skeletons have been crushed!" << std::endl;
    }

    return 0;
}


IMPROVED CODE

```
#include
#include
#include

/*
* Begins the simulation and manages all aspects of the game.
*
* @date 10/25/2015
* @author IWriteThings
* @return bool
*/
int main() {

Solution

Unneeded code

These lines are not necessary:

if( SKELETONS <= 0 ){ SKELETONS = 0; }
        if( HUMANS <= 0 ){ HUMANS = 0; }


because your previous code already limited the casualties so that the number remaining can never fall below zero.

Clarification because of comment:

Suppose humanAttackPower were 100 and SKELETONS were 30:

if( humanAttackPower > SKELETONS ){ humanAttackPower = SKELETONS;}
        SKELETONS = ( SKELETONS - humanAttackPower );
        if( SKELETONS <= 0 ){ SKELETONS = 0; }


After the first line, humanAttackPower would be reduced to 30.

After the second line, SKELETONS would be reduced to 0.

The third line is now unnecessary.
Complicated odds

You have a complicated way of determining which side does damage:

iSecret1 = rand() % 20 + 1;
    iSecret2 = rand() % 20 + 1;
    if( iSecret1 > iSecret2 ) {


What this boils down to is that the humans have a 47.5% chance of doing damage (because ties count towards the skeletons). If that is intentional, I would just do this:

// 19 out of 40 chances for the humans = 47.5%
     if ((rand() % 40) < 19) {


If you meant the chance to be 50%, then I would do this:

if ((rand() & 1) == 0) {

Code Snippets

if( SKELETONS <= 0 ){ SKELETONS = 0; }
        if( HUMANS <= 0 ){ HUMANS = 0; }
if( humanAttackPower > SKELETONS ){ humanAttackPower = SKELETONS;}
        SKELETONS = ( SKELETONS - humanAttackPower );
        if( SKELETONS <= 0 ){ SKELETONS = 0; }
iSecret1 = rand() % 20 + 1;
    iSecret2 = rand() % 20 + 1;
    if( iSecret1 > iSecret2 ) {
// 19 out of 40 chances for the humans = 47.5%
     if ((rand() % 40) < 19) {
if ((rand() & 1) == 0) {

Context

StackExchange Code Review Q#108729, answer score: 4

Revisions (0)

No revisions yet.