The Next Step: Turning a Clown Car into a Pokemon Battle

It’s my son who asked me to do this. He’s recently taken up Pokemon and has insisted that my wife and I both learn to play so that we can have Pokemon battles with him.

Image

Pika!

I thought: Ok, I have this Clown Car thing all worked out already. Maybe I can just transform it into  what he wants. The basic idea is to transform the clown car into a deck of Pokemon cards. The I also need to have two other decks – one for the player’s hand and one for the computer’s.  At the start of the game, we should have all the Pokemon in the main deck. I thought I would make this main deck viewable and then allow the player and then computer to draw cards, one at a time until they fill up their hands (in my first example, I top out the player and computer decks at two cards each.

What I need to do is be able to move the cards from one deck to another. Apparently, there is a move function for vectors, but I haven’t been able to make this work. However, it shouldn’t be too difficult to just copy a card object from one deck to another and then erase the first instance.

Nevertheless, I have not been able to make this work yet. Here’s the code that controls the player picking and the computer randomly taking cards:

 

 

void drawCards(vector<Pokemon> &draw, vector<Pokemon> &player, vector<Pokemon> &computer){

bool playerChoice = true;

int playerPick= NULL;

int compPick;

int number = 0;

   //start with outline

    printVectorContents2(draw);

    cout<<“**********”<<endl;

    cout<<“Player’s hand”<<endl;

    printVectorContents2(player);

    cout<<“**********”<<endl;

    cout<<“Computer’s hand”<<endl;

    printVectorContents2(computer);

    cout<<“**********”<<endl;

    cout<<“**********”<<endl;

 

while (number < 2) {             //this is copying the pokemon into the new vectors properly, but not erasing them

if (playerChoice == true) {

            cout<<“Player, please choose a Pokemon from the draw pile (enter ID number)”<<endl;

cin>>playerPick;

cout<<“You chose number “<<playerPick<<“, “<<draw[playerPick1].getName()<<endl;

            player[number] = draw[playerPick1];

            //draw.push_back(playerPick-1);

            draw.erase(draw.begin()-playerPick-1);

            //draw.pop_back();

            //move(draw.begin()+playerPick-1, draw.begin()+playerPick-1, player.begin()+number);

            playerChoice = false;

            }

else{

            compPick = rand()%draw.size()+1;

cout<<“Computer chose number “<<compPick<<“, “<<draw[compPick1].getName()<<endl;

           // move(draw.begin()+compPick-1, draw.begin()+compPick-1, computer.begin()+number);

            computer[number] = draw[compPick1];

            //draw.push_back(compPick-1);

            //draw.pop_back();

            draw.erase(draw.begin()-compPick-1);

            number++;

            playerChoice = true;

        }

    }

    //exits when number =3

    cout<<“Finished drawing cards.”<<endl;

    printVectorContents2(draw);

    cout<<“**********”<<endl;

    printVectorContents2(player);

    cout<<“**********”<<endl;

    printVectorContents2(computer);

    cout<<“**********”<<endl;

    cout<<“**********”<<endl;

The problem I’m seeing is that nothing is actually moving. output:

**************

IDNumber = 1, name = Pikachu

IDNumber = 2, name = Froky

IDNumber = 3, name = FurFrou

IDNumber = 4, name = Meowth

IDNumber = 5, name = BunnleBy

IDNumber = 6, name = BeeDrill

IDNumber = 7, name = Gloom

IDNumber = 8, name = Diglett

**********

Player’s hand

IDNumber = 0, name = 

IDNumber = 0, name = 

**********

Computer’s hand

IDNumber = 0, name = 

IDNumber = 0, name = 

**********

**********

Player, please choose a Pokemon from the draw pile (enter ID number)

1

You chose number 1, Pikachu

Computer chose number 6, Gloom

(lldb)

This means that we are getting through the first round of picks, but crashing before second round. Previously, I did have a second round operational, but the player and computer hands remained empty and the Pokemon who had been picked remaining in the drawPile. Perhaps it’s a problem with using pointers?

Wow, Finally smoothed out that clown problem

ImageThis version of the clown application uses a vector (clown car) to handle the objects (clowns).

Each clown has a Name, and ID number, hit points(hps), and three attacks. The way this program works is that it seeds the vector with the clown objects at the beginning (this was part of the vector handling problem), then allows them to fight it out in the clown car – every time a clown’s hps falls to zero, the clown is dead and if thrown from the car (i.e., they no longer populate the vector. This proceeds until only one clown remains. The last clown standing is declared the winner.

As the battle occurs, cout statements indicate who attacks whom and what damage is inflicted. (note: every attack is a ‘hit’). If a clown is killed, this is also announced and then the vector is printed out to show what clowns remain in the car.

I believe there are no bugs remaining in this program. When I was working on it, I included a large number of debugging statements. These have been removed for clarity in the final product. I also removed the dialog asking what clown to eject – and removed the function that handled that query.

Code:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//

#include <iostream>

#include <vector>

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

#include <cstdlib>

#include <ctime>

usingnamespacestd;

using std::vector;

class IdNumbers{     // a place to store a counter for creating idNumbers

public:

    IdNumbers(){

    }

static int getANewNumber(){

counter++;

return counter;

    }

protected:

static int counter;

}; //end IdNumbers

class Clown{   // the clown objects

public:

    Clown(){};

    ~Clown(){};

    Clown(int clownId){

idNum = clownId;

    };

    Clown(string nameOfClown, IdNumbers theNumber, string attackIn1, int attackIn1Damage, string attackIn2, int attackIn2Damage, string attackIn3, int attackIn3Damage ){

clownName = nameOfClown;

idNum = theNumber.getANewNumber();

attack1 = attackIn1;

attack2 = attackIn2;

attack3 = attackIn3;

attack1Damage = attackIn1Damage;

attack2Damage = attackIn2Damage;

attack3Damage = attackIn3Damage;

hitPoints = 100;

dead = false;

    };

int getClownId(void){

return idNum;

    }

string getName(void){

return clownName;

    }

void printClown(void){

cout<<“idNumber = “<<idNum<<endl;

cout<<“name = “<<clownName<<endl;

    };

void printClown2(void){

        cout<<“Clown idNumber = “<<idNum<<“, name = “<<clownName<<endl;

    };

void printClown3(void){

        cout<<“Clown Name = “<<clownName<<“, hp = “<<hitPoints<<endl;

    };

int getNumAttacks(void){

return numAttacks;

    };

string getAttack1(){

return attack1;

    };

string getAttack2(){

return attack2;

    };

string getAttack3(){

return attack3;

    };

int getDamage1(){

        returnattack1Damage;

    };

int getDamage2(){

        returnattack2Damage;

    };

int getDamage3(){

        returnattack3Damage;

    };

int getHP(){

return hitPoints;

    };

void setHP(int damage){

this->hitPoints = this->hitPoints – damage;

if (hitPoints <= 0){

dead = true;

        }

    }

bool getDead(){

return dead;

    }

private:

string clownName;

int idNum;

int numAttacks = 3;

string attack1;

int attack1Damage;

string attack2;

int attack2Damage;

string attack3;

int attack3Damage;

int hitPoints;

bool dead;

};    //end clown

//functions

void printVectorContents(vector<Clown> &);

void printVectorContents2(vector<Clown> &);

void askWhoToPop(vector<Clown> &);

void popClown(vector<Clown> &, int);

void popThisClown(vector<Clown> &car, int popWho);

void directFight(vector<Clown> &car);

int IdNumbers ::counter = 0;

//*********************************************************************************************

int main(int argc, const char * argv[])

{

IdNumbers theCounter;

    srand (time(NULL));                     //<—–should seed random numbers appropriately…

    vector<Clown> clowncar;     //sets up a vector for fish

int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

Clown pennywise(“Pennywise”, theCounter, “bite”, 30, “claw”, 20, “defenestrate”, 1000);

    clowncar.push_back(pennywise);

    Clown jwgacy(“John Wayne Gacy”, theCounter, “creep out”, 30, “molest”, 50, “defenestrate”, 1000);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    Clown bozo(“Bozo”, theCounter, “nose honk”, 5, “flower squirt”, 10, “defenestrate”, 1000);

    clowncar.push_back(bozo);

    Clown krusty(“Krusty”, theCounter, “tease”, 15, “step on foot”, 20, “defenestrate”, 1000);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

    Clown ronald(“Ronald McDonald”, theCounter, “smile”, 30, “arterie clog”, 30, “defenestrate”, 1000);

    clowncar.push_back(ronald);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“**********”<<endl;

    clowncar.shrink_to_fit();

    printVectorContents2(clowncar);

directFight(clowncar);

return 0;

}

//****************************************************************************

//Non-Member functions

void printVectorContents(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown();

    }

}

void printVectorContents2(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown2();

    }

}

void printVectorContents3(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown3();

    }

}

void popThisClown(vector<Clown> &car, int popWho){   //how to selectively delete from a vector????

for (int j = 0; j < car.size(); j++)

    {

if(car[j].getClownId() == popWho)

        {

            car.erase (car.begin()+j);

        }

    }// end for

};

void directFight(vector<Clown> &car){      //handles the fighting

bool victor = false;

bool targetIdentified = false;

string winner;

int attackerArrayPosition;

int targetArrayPosition;

int targetID;

int attackerID;

string attackerName;

string targetName;

int attackNum;

string attackType;

int attackDamage;

while (victor == false){

for (int attackerArrayPosition=0;attackerArrayPosition<car.size();attackerArrayPosition++){                             //scrolls through attackers

if(car.size() == 1){

                victor = true;

                winner = car[0].getName();

cout<<“Winner is “<<winner<<endl;

exit(0);

            }

            attackerName = car[attackerArrayPosition].getName();

            attackerID = car[attackerArrayPosition].getClownId();

            targetArrayPosition = rand()%car.size();               // gets an array position

            targetID = car[targetArrayPosition].getClownId();      //translates array position into clown ID number

            targetName = car[targetArrayPosition].getName();

while (targetIdentified == false){

if (targetID == attackerID){

                    targetIdentified = false;

                    targetArrayPosition = rand()%car.size();

                    targetID = car[targetArrayPosition].getClownId();

                }

else if (targetID != attackerID){

                    targetIdentified = true;

for(int i=0;i<car.size();i++){

if (targetID == car[i].getClownId()){

                            targetName = car[i].getName();

                        }

                    }

break;

                }

else{

                    cout<<“this isn’t supposed to happen. AttackerID is “<<attackerArrayPosition<<” target ID is “<<targetID<<endl;

                }

            }

        attackNum = rand()%3 + 1;    //choose attack (gets a random number)

switch ( attackNum ) {

case 1 :

                    attackType = car[attackerArrayPosition].getAttack1();

break;

case 2 :

                    attackType = car[attackerArrayPosition].getAttack2();

break;

case 3 :

                    attackType = car[attackerArrayPosition].getAttack3();

break;

default :

                    cout<<“error in attackNum switch statement”<<endl;

            } //end attackNum Switch

switch ( attackNum ) {

case 1 :

                    attackDamage = car[attackerArrayPosition].getDamage1();

break;

case 2 :

                    attackDamage = car[attackerArrayPosition].getDamage2();

break;

case 3 :

                    attackDamage =  car[attackerArrayPosition].getDamage3();

break;

default :

                    cout<<“error in attackNum switch statement”<<endl;

            }

cout<<attackerName<<” attacks “<<targetName<<” with “<<attackType<<” attack for “<<attackDamage<<” damage.”<<endl;

            //report attacker, attack, target AND score damage

                        car[targetArrayPosition].setHP(attackDamage);       //call to adjust hps according to damage

if (car[targetArrayPosition].getDead() == true){    //thrown dead from the car

cout<<car[targetArrayPosition].getName()<<” is dead.”<<endl;

                            cout<<“<<<<<<<<<<<<<+++++++++>>>>>>>>>>>>>>>>>>>”<<endl;

popThisClown(car, targetID);

                            car.shrink_to_fit();

                            targetIdentified = false;   //reset for next round

break;

                        }

else{

                            cout<<“<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>”<<endl;

                            targetIdentified = false;   //reset for next round

printVectorContents3(car);

                        }

            cout<<“<<<<<<<<<*************>>>>>>>>>>>>>”<<endl;

if(car.size() == 1){

                victor = true;

                winner = car[0].getName();

            }

    }

         printVectorContents3(car);

    }

cout<<“Winner is “<<winner<<endl;

};

output:

**************

**********

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 3, name = Bozo

Clown idNumber = 4, name = Krusty

Clown idNumber = 5, name = Ronald McDonald

Pennywise attacks Bozo with bite attack for 30 damage.

<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 100

Clown Name = John Wayne Gacy, hp = 100

Clown Name = Bozo, hp = 70

Clown Name = Krusty, hp = 100

Clown Name = Ronald McDonald, hp = 100

<<<<<<<<<*************>>>>>>>>>>>>>

John Wayne Gacy attacks Krusty with defenestrate attack for 1000 damage.

Krusty is dead.

<<<<<<<<<<<<<+++++++++>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 100

Clown Name = John Wayne Gacy, hp = 100

Clown Name = Bozo, hp = 70

Clown Name = Ronald McDonald, hp = 100

Pennywise attacks John Wayne Gacy with bite attack for 30 damage.

<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 100

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Bozo, hp = 70

Clown Name = Ronald McDonald, hp = 100

<<<<<<<<<*************>>>>>>>>>>>>>

John Wayne Gacy attacks Pennywise with molest attack for 50 damage.

<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 50

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Bozo, hp = 70

Clown Name = Ronald McDonald, hp = 100

<<<<<<<<<*************>>>>>>>>>>>>>

Bozo attacks Pennywise with nose honk attack for 5 damage.

<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 45

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Bozo, hp = 70

Clown Name = Ronald McDonald, hp = 100

<<<<<<<<<*************>>>>>>>>>>>>>

Ronald McDonald attacks Bozo with defenestrate attack for 1000 damage.

Bozo is dead.

<<<<<<<<<<<<<+++++++++>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 45

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Ronald McDonald, hp = 100

Pennywise attacks John Wayne Gacy with defenestrate attack for 1000 damage.

John Wayne Gacy is dead.

<<<<<<<<<<<<<+++++++++>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 45

Clown Name = Ronald McDonald, hp = 100

Pennywise attacks Ronald McDonald with bite attack for 30 damage.

<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 45

Clown Name = Ronald McDonald, hp = 70

<<<<<<<<<*************>>>>>>>>>>>>>

Ronald McDonald attacks Pennywise with smile attack for 30 damage.

<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 15

Clown Name = Ronald McDonald, hp = 70

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 15

Clown Name = Ronald McDonald, hp = 70

Pennywise attacks Ronald McDonald with defenestrate attack for 1000 damage.

Ronald McDonald is dead.

<<<<<<<<<<<<<+++++++++>>>>>>>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 15

Winner is Pennywise

Program ended with exit code: 0

 

Collaborative Coding

Bridging the chasm between sandbox problems and real collaborative project-based learning.

collabCodingPage

The next hiccup – making random random and getting organized

I admit that I have a problem.

But first, start here for a little science. Or just go ahead and get straight to the code below.

Claire: What’s wrong with you? Why don’t you like yourself?

Brian: ‘Cause I’m stupid…’cause I’m failing shop. See we had this assignment, to make this ceramic elephant, and um–and we had eight weeks to do it and we’re s’posed ta, and it was like a lamp, and when you pull the trunk the light was s’posed to go on. My light didn’t go on, I got a F on it. Never got a F in my life. When I signed up, you know, for the course I mean. I thought I was playing it real smart, you know. ‘Cause I thought, I’ll take shop, it’ll be such an easy way to maintain my grade point average.

What’s supposed to happen:

1. populate a clown car with five clowns

2. each clown is an object, with a name, an ID number and three attacks, each with unique damage

3. scroll through the clowns (once through = a turn)

       a. pick a target clown

       b. pick an attack

       c. write out the attack, score damage against the target clown

       d. see if clown is down below 0 ‘hit points’, if so, he’s dead – kick him out of the car

       e. manage the data handling (clown objects are stored in a vector, shrink the vector after kicking out clowns.

       f. report on who is left and how many hit points each clown has left

My code:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//

 

 

//problem calculating damage and reducing hp appropriately.

//problems generating random targets

//problems restricting targets to available clowns (i.e. not dead ones)

 

 

#include <iostream>

#include <vector>

#include <time.h>

#include <stdio.h>

#include <stdlib.h>

#include <cstdlib>

#include <ctime>

 

usingnamespacestd;

using std::vector;

 

 

 

class IdNumbers{     // a place to store a counter for creating idNumbers

public:

    IdNumbers(){

    }

static int getANewNumber(){

counter++;

return counter;

    }

protected:

static int counter;

}; //end IdNumbers

 

 

class Clown{   // the clown objects

public:

    Clown(){};

    ~Clown(){};

    Clown(int clownId){

idNum = clownId;

    };

    Clown(string nameOfClown, IdNumbers theNumber, string attackIn1, int attackIn1Damage, string attackIn2, int attackIn2Damage, string attackIn3, int attackIn3Damage ){

clownName = nameOfClown;

idNum = theNumber.getANewNumber();

attack1 = attackIn1;

attack2 = attackIn2;

attack3 = attackIn3;

attack1Damage = attackIn1Damage;

attack2Damage = attackIn2Damage;

attack3Damage = attackIn3Damage;

hitPoints = 100;

dead = false;

    };

int getClownId(void){

return idNum;

    }

string getName(void){

return clownName;

    }

void printClown(void){

cout<<“idNumber = “<<idNum<<endl;

cout<<“name = “<<clownName<<endl;

    };

void printClown2(void){

        cout<<“Clown idNumber = “<<idNum<<“, name = “<<clownName<<endl;

    };

void printClown3(void){

        cout<<“Clown Name = “<<clownName<<“, hp = “<<hitPoints<<endl;

    };

int getNumAttacks(void){

return numAttacks;

    };

string getAttack1(){

return attack1;

    };

string getAttack2(){

return attack2;

    };

string getAttack3(){

return attack3;

    };

int getDamage1(){

        returnattack1Damage;

    };

int getDamage2(){

        returnattack2Damage;

    };

int getDamage3(){

        returnattack3Damage;

    };

int getHP(){

return hitPoints;

    };

void setHP(int damage){

this->hitPoints = this->hitPoints – damage;

if (this->hitPoints <= 0){

dead = true;

        }

    }

bool getDead(){

return dead;

    }

private:

string clownName;

int idNum;

int numAttacks = 3;

string attack1;

int attack1Damage;

string attack2;

int attack2Damage;

string attack3;

int attack3Damage;

int hitPoints;

bool dead;

};    //end clown

 

//functions

void printVectorContents(vector<Clown> &);

void printVectorContents2(vector<Clown> &);

void askWhoToPop(vector<Clown> &);

void popClown(vector<Clown> &, int);

void popThisClown(vector<Clown> &car, int popWho);

void directFight(vector<Clown> &car);

 

int IdNumbers ::counter = 0;

 

 

//*********************************************************************************************

 

int main(int argc, const char * argv[])

{

IdNumbers theCounter;

    srand (time(NULL));                     //<—–should seed random numbers appropriately…

    vector<Clown> clowncar;     //sets up a vector for fish

int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

Clown pennywise(“Pennywise”, theCounter, “bite”, 30, “claw”, 20, “defenestrate”, 1000);

    clowncar.push_back(pennywise);

    Clown jwgacy(“John Wayne Gacy”, theCounter, “creep out”, 30, “molest”, 50, “defenestrate”, 1000);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    Clown bozo(“Bozo”, theCounter, “nose honk”, 5, “flower squirt”, 10, “defenestrate”, 1000);

    clowncar.push_back(bozo);

    Clown krusty(“Krusty”, theCounter, “tease”, 15, “step on foot”, 20, “defenestrate”, 1000);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

 

 

    Clown ronald(“Ronald McDonald”, theCounter, “smile”, 30, “arterie clog”, 30, “defenestrate”, 1000);

    clowncar.push_back(ronald);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“**********”<<endl;

    clowncar.shrink_to_fit();

    //cout<<“current capacity is: “<<clowncar.capacity()<<endl;

    //cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents2(clowncar);

directFight(clowncar);

return 0;

}

//****************************************************************************

 

 

//Non-Member functions

void printVectorContents(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown();

    }

}

 

void printVectorContents2(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown2();

    }

}

void printVectorContents3(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown3();

    }

}

 

void askWhoToPop(vector<Clown> &car){

int popWho;

char uPick;

    bool picked = false;   // ensures that an appropriate choice was made

while (picked == false) {   //this seems to work

        cout<<“Would you like to (p)ick a clown to Pop, or should we do it (r)andomly?”<<endl;

cin>>uPick;

if ((uPick == ‘p’) || (uPick == ‘P’)) {

            cout<<“Please enter an ID number of a clown to kick out of the car: “;

cin>>popWho;

            picked = true;

break;

        }

if ((uPick == ‘r’) || (uPick == ‘R’)) {

            //generate random number 0 – vector size, identify and kick out – works

            popWho = rand()%car.size();

            cout<<“you picked random”<<endl;

            picked = true;

break;

        }

else{

            picked = false;

            cout<<“Try again – you (p)ick or (r)andom”<<endl;

cin>>uPick;

continue;

        }

    }

popThisClown(car, popWho);

cout<<endl;

}

 

void popThisClown(vector<Clown> &car, int popWho){   //how to selectively delete from a vector????

for (int j = 0; j < car.size(); j++)

    {

if(car[j].getClownId() == popWho)

        {

            car.erase (car.begin()+j);

        }

    }// end for

};

 

 

void directFight(vector<Clown> &car){      //handles the fighting

bool victor = false;

bool targetIdentified = false;

string winner;

int attackerID;

int targetID;

string attackerName;

string targetName;

int attackNum;

string attackType;

int attackDamage;

while (victor == false){

for (int j=0;j<car.size();j++){                             //scrolls through attackers

        attackerName = car[j].getName();

            attackerID = car[j].getClownId();

            //cout<<“Attacker “<<attackerID<<” is “<<attackerName<<endl;     // verification step

            targetID = rand()%car.size();               // (5 + 1);    //<——problem with this randomizer

            //cout<<“Target “<<targetID<<” is “<<car[targetID].getName()<<endl;

while (targetIdentified == false){

                //targetID = rand()%5+1;        //car.size();    //<—–problem    <replace with number now>

                //cout<<“In while”<<endl;

if (targetID == attackerID){

                    targetIdentified = false;

                    targetID = rand()%5+1;

                    cout<<“Identified self – trying again”<<endl;

                }

else if (targetID != attackerID){

                    targetIdentified = true;

for(int i=0;i<car.size();i++){

if (targetID == car[i].getClownId()){

                            targetName = car[targetID].getName();

cout<<“Target of “<<attackerName<<” is “<<targetName<<endl;

                            targetID = i;

                        }

                    }

//temp verification step

break;

                }

else{

                    cout<<“this isn’t supposed to happen. AttackerID is “<<attackerID<<” target ID is “<<targetID<<endl;

                }

            }

        attackNum = rand()%3 + 1;    //choose attack (gets a random number)

for (int i=0;i<car[attackerID].getNumAttacks(); i++){   //loop through attackers attacks

            //cout<<“numAttacks is “<<car[attackerID].getNumAttacks();

            //cout<<“attackNum is “<<attackNum<<endl;

switch ( attackNum ) {

case 1 :

                    attackType = car[attackerID].getAttack1();

                    //cout<<“returning “<<car[attackerID].getAttack1()<<endl;

break;

case 2 :

                    attackType = car[attackerID].getAttack2();

                    //cout<<“returning “<<attackType<<endl;

break;

case 3 :

                    attackType = car[attackerID].getAttack3();

                    //cout<<“returning “<<attackType<<endl;

break;

default :

                    cout<<“error in attackNum switch statement”<<endl;

            }

switch ( attackNum ) {

case 1 :

                    attackDamage = car[attackerID].getDamage1();

break;

case 2 :

                    attackDamage = car[attackerID].getDamage2();

break;

case 3 :

                    attackDamage =  car[attackerID].getDamage3();

break;

default :

                    cout<<“error in attackNum switch statement”<<endl;

            }

        }

cout<<“AttackDamage is :”<<attackDamage<<endl;

    //choose target

cout<<attackerName<<” attacks “<<targetName<<” with “<<attackType<<” attack “<<” for “<<attackDamage<<” damage.”<<endl;

            //report attacker, attack, target AND score damage

            car[targetID].setHP(attackDamage);       //call to adjust hps according to damage

if (car[targetID].getDead() == true){    //thrown dead from the car

popThisClown(car, targetID);

                car.shrink_to_fit();

            }

            cout<<“<<<<<<<<<*************>>>>>>>>>>>>>”<<endl;

            printVectorContents3(car);

if(car.size() == 1){

                victor = true;

                winner = car[0].getName();

            }

    //printVector3 – clown name, hp

    //isWinner, i.e. is there only one clown left in the car?

    }

    }

cout<<“Winner is “<<winner<<endl;

};

 

 

My Output:

**************

**********

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 3, name = Bozo

Clown idNumber = 4, name = Krusty

Clown idNumber = 5, name = Ronald McDonald

AttackDamage is :30

Pennywise attacks  with creep out attack  for 30 damage.

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 70

Clown Name = John Wayne Gacy, hp = 100

Clown Name = Bozo, hp = 100

Clown Name = Krusty, hp = 100

Clown Name = Ronald McDonald, hp = 100

AttackDamage is :5

John Wayne Gacy attacks  with nose honk attack  for 5 damage.

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 70

Clown Name = John Wayne Gacy, hp = 100

Clown Name = Bozo, hp = 100

Clown Name = Krusty, hp = 95

Clown Name = Ronald McDonald, hp = 100

AttackDamage is :20

Bozo attacks  with step on foot attack  for 20 damage.

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 70

Clown Name = John Wayne Gacy, hp = 100

Clown Name = Bozo, hp = 80

Clown Name = Krusty, hp = 95

Clown Name = Ronald McDonald, hp = 100

AttackDamage is :30

Krusty attacks  with arterie clog attack  for 30 damage.

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 70

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Bozo, hp = 80

Clown Name = Krusty, hp = 95

Clown Name = Ronald McDonald, hp = 100

AttackDamage is :1000

Ronald McDonald attacks  with defenestrate attack  for 1000 damage.

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 70

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Krusty, hp = -905

Clown Name = Ronald McDonald, hp = 100

AttackDamage is :30

Pennywise attacks  with creep out attack  for 30 damage.

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 70

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Krusty, hp = -905

Clown Name = Ronald McDonald, hp = 70

AttackDamage is :20

John Wayne Gacy attacks  with step on foot attack  for 20 damage.

<<<<<<<<<*************>>>>>>>>>>>>>

Clown Name = Pennywise, hp = 70

Clown Name = John Wayne Gacy, hp = 70

Clown Name = Krusty, hp = -905

Clown Name = Ronald McDonald, hp = 50

etc., etc……

adding a few more layers

Image

Nighty-night.

With the clown car program working fairly well, I want to add a layer of complexity…

Well, I’d rather not add complexity at all, but I do want my code to do something. Since my son has recently discovered Pokemon (and taught me how to play), I thought I would set up this clown car as something of a cage match for the clowns.  Here’s the basic plan:

Vector practice – clown car CAGE MATCH!

add some element to the clown object that can be changed

  • how about hit points(hp)?
  • make them fight, damage is subtracted from hp

(can I make the whole fight a separate document to #include?)

Fight will proceed in turns

Basic Turn Outline

o   scroll through clowns remaining in the car (i.e. each clown gets a chance to attack each turn – unless they’re killed)

o   randomize attack (each has 50% chance of hit) – have ~3 attacks available per clown

o   randomize target among clowns remaining in the car (clown cannot attack itself)

o   report attacker, attack, target and damage (and if Kill) as a cout

  • throw slain targets from the car

o   end of round: update each clown’s hp

  • update and output damage each round – pause before advancing to next round (also number the rounds)

 Table of Clowns with hot points, attacks and respective damage:

Name Hp Attacks Damage
Pennywise 100 Bite/Claw/defenestration 30/20/auto kill
Jw Gacy 75 Creep out/Molest / defenestration 30/50/auto kill
Bozo 50 Honk/squirting flower defenestration 5/10/auto kill
Krusty 75 Laugh/step on foot defenestration 5/20/auto kill
Ronald McD 50 Smile/clog arteries defenestration 10/50/auto kill

 

 

 

A debt of gratitude

Thank you very much to CTphpnwb on </ dream.in.code> for helping me to gain a better understanding of how to work with references – and why this is important.

The basic problem I was facing was that I was passing the whole factor of objects into my functions. When you do this, it actually creates a copy of the vector and manipulates that. What I wanted to have happen was to pass a reference to my vector (i.e. the vector’s address in the computer) so that no copy was made, and all changes were done to the original vector.

I’ve tried playing with shrink_to_fit, but I think I may be exploring territory that isn’t going to actually help me at this point.

the successful code:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//  Copyright (c) 2014 Jack Treml. All rights reserved.

//

 

 

 

/*

 1. empty car (0)

 2. pick up Pennywise and John Wayne Gacy (2)

 3. Pick up Bozo and Krusty (4)

 4. Pick up Ronald McDonald (5)

 5. print out list of clowns

 6. As for a name of a clown (or ID number) to kick out of the car

 7. Kick only that individual out, then re-print list

 */

 

#include <iostream>

#include <vector>

usingnamespacestd;

using std::vector;

 

class IdNumbers{     // a place to store a counter for creating idNumbers

public:

    IdNumbers(){

    }

static int getANewNumber(){

counter++;

return counter;

    }

protected:

static int counter;

};

 

class Clown{   // the clown objects

public:

    Clown(){};

    ~Clown(){};

    Clown(int clownId){

idNum = clownId;

    };

    Clown(string nameOfClown, IdNumbers theNumber){

clownName = nameOfClown;

idNum = theNumber.getANewNumber();

    };

int getClownId(void){

return idNum;

    }

void printClown(void){

cout<<“idNumber = “<<idNum<<endl;

cout<<“name = “<<clownName<<endl;

    };

void printClown2(void){

        cout<<“Clown idNumber = “<<idNum<<“, name = “<<clownName<<endl;

    };

private:

string clownName;

int idNum;

};

 

//functions

void printVectorContents(vector<Clown> &);

void printVectorContents2(vector<Clown> &);

void askWhoToPop(vector<Clown> &);

void popClown(vector<Clown> &, int);

void popThisClown(vector<Clown> &car, int popWho);

 

int IdNumbers ::counter = 0;

 

int main(int argc, const char * argv[])

{

IdNumbers theCounter;

    vector<Clown> clowncar;     //sets up a vector for fish

int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

Clown pennywise(“Pennywise”, theCounter);

    clowncar.push_back(pennywise);

Clown jwgacy(“John Wayne Gacy”, theCounter);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    //printVectorContents(clowncar);

    printVectorContents2(clowncar);

Clown bozo(“Bozo”, theCounter);

    clowncar.push_back(bozo);

Clown krusty(“Krusty”, theCounter);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    //printVectorContents(clowncar);

    printVectorContents2(clowncar);

Clown ronald(“Ronald McDonald”, theCounter);

    clowncar.push_back(ronald);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“current capacity is: “<<clowncar.capacity()<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    //printVectorContents(clowncar);

    //list clowns with ID numbers

    printVectorContents2(clowncar);

    //ask for an ID number of a clown to evict

askWhoToPop(clowncar);

    cout<<“**********”<<endl;

    clowncar.shrink_to_fit();

    cout<<“current capacity is: “<<clowncar.capacity()<<endl;

cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents2(clowncar);

return 0;

}

 

 

//Non-Member functions

void printVectorContents(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown();

    }

}

 

void printVectorContents2(vector<Clown> &car){

for(int i=0; i < car.size(); i++){

        car[i].printClown2();

    }

}

 

void askWhoToPop(vector<Clown> &car){

int popWho;

    cout<<“Please enter an ID number of a clown to kick out of the car: “;

cin>>popWho;

popThisClown(car, popWho);

cout<<endl;

}

 

void popThisClown(vector<Clown> &car, int popWho){   //how to selectively delete from a vector????

for (int j = 0; j < car.size(); j++)

    {

        cout<<“is “<<j<<” the right guy to bump?”<<endl;

if(car[j].getClownId() == popWho)

        {

            cout<<“yeah, I’m here lookin’ at erasing clown “<<j<<endl;

            //car.erase(j);   //tried this – didn’t work

            car.erase (car.begin()+j);

        }

    }// end for

};

 

output:

**************

vector is size: 0

**************

vector is size: 2

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

**************

vector is size: 4

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 3, name = Bozo

Clown idNumber = 4, name = Krusty

**************

current capacity is: 8

vector is size: 5

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 3, name = Bozo

Clown idNumber = 4, name = Krusty

Clown idNumber = 5, name = Ronald McDonald

Please enter an ID number of a clown to kick out of the car: 3

is 0 the right guy to bump?

is 1 the right guy to bump?

is 2 the right guy to bump?

yeah, I’m here lookin’ at erasing clown 2

is 3 the right guy to bump?

 

**********

current capacity is: 4

vector is size: 5

Clown idNumber = 1, name = Pennywise

Clown idNumber = 2, name = John Wayne Gacy

Clown idNumber = 4, name = Krusty

Clown idNumber = 5, name = Ronald McDonald

Program ended with exit code: 0

in a rut

I’m stuck. I’ve been trying to re-jigger my code to add a dialog asking for an ID number to delete from the vector and then doing it, but so far, I’ve gotten nowhere. Sure, I can create the dialog and get the info from the user, but I have not yet been able to use that info to selectively delete a clown from the car.

Recall, the purpose of this exercise is to ensure that I can handle a vector of objects, adding and deleting objects at will. A secondary objective is to be sure that I am assigning unique ID numbers to each object so they can be tracked accurately.

my function asking who to delete:

void askWhoToPop(vector<Clown> car){

    int popWho;

    cout<<“Please enter an ID number of a clown to kick out of the car: “;

    cin>>popWho;

    Clown::popThisClown(car, popWho);

    cout<<endl;

}

 

my function for deleting an object:

void popThisClown(vector<Clown> car, int popWho){   //how to selectively delete from a vector????

for (int j = 0; j < car.size(); j++)

        {

if(car[j].idNum == popWho)

            {

delete car[j];

                car.pop_back();

            }

        }// end for

    };

suggestions????

 

Success!

Successfully added object counter as a static variable inside an object. Used to generate unique, sequential ID numbers for a vector of other objects.

Image

As easy as 00 01 11 …

This means, that after an afternoon of work and reflection, I was finally able to count up past “1”.

I need to come up with a way to build this project into something larger to ensure that I can manipulate the objects in my vector at will.

Perhaps the best thing to add is a dialog asking the user for the name of a clown to kick out, then ice out that clown, kick them out of the vector and report on everyone’s ID number (should remain unchanged) and the size of the vector (it should shrink by one).

Here’s the completed project:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//  Copyright (c) 2014 Jack Treml. All rights reserved.

//

/*

 1. empty car (0)

 2. pick up Pennywise and John Wayne Gacy (2)

 3. Pick up Bozo and Krusty (4)

 4. Pick up Ronald McDonald (5)

 5. Ronald McDonald gets kicked out (4)

 6. All the rest stop for a drink at the Alehouse together (0)

 */

#include <iostream>

#include <vector>

usingnamespacestd;

using std::vector;

class IdNumbers{     // a place to store a counter for creating idNumbers

public:

    IdNumbers(){

    }

    static int getANewNumber(){

        counter++;

        return counter;

    }

protected:

     static int counter;

};

class Clown{   // the clown objects

public:

    Clown(){};

    ~Clown(){};

    Clown(int clownId){

        idNum = clownId;

    };

    Clown(string nameOfClown, IdNumbers theNumber){

        clownName = nameOfClown;

        idNum = theNumber.getANewNumber();

    };

    int getClownId(void){

        return idNum;

    }

    void printClown(void){

        cout<<“idNumber = “<<idNum<<endl;

        cout<<“name = “<<clownName<<endl;

    };

private:

    string clownName;

    int idNum;

};

//functions

void printVectorContents(vector<Clown>);

int IdNumbers ::counter = 0;

int main(int argc, const char * argv[])

{

    IdNumbers theCounter;

    vector<Clown> clowncar;     //sets up a vector for fish

    int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    Clown pennywise(“Pennywise”, theCounter);

    clowncar.push_back(pennywise);

    Clown jwgacy(“John Wayne Gacy”, theCounter);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    Clown bozo(“Bozo”, theCounter);

    clowncar.push_back(bozo);

    Clown krusty(“Krusty”, theCounter);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    Clown ronald(“Ronald McDonald”, theCounter);

    clowncar.push_back(ronald);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    clowncar.pop_back();

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    while(clowncar.size() > 0){

        clowncar.pop_back();

    }

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    return 0;

}

//functions

void printVectorContents(vector<Clown> car){

    for(int i=0; i < car.size(); i++){

        car[i].printClown();

    }

}

/*

 Output:

 **************

 vector is size: 0

 **************

 vector is size: 2

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 idNumber = 3

 name = Bozo

 idNumber = 4

 name = Krusty

 **************

 vector is size: 5

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 idNumber = 3

 name = Bozo

 idNumber = 4

 name = Krusty

 idNumber = 5

 name = Ronald McDonald

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 2

 name = John Wayne Gacy

 idNumber = 3

 name = Bozo

 idNumber = 4

 name = Krusty

 **************

 vector is size: 0

 Program ended with exit code: 0

 */

 

 

unable to add 1

Grrrr.

I know I have set this problem up oddly. However, the point is to handle objects correctly, not to be the cleanest code. i.e. I’m trying to do this in a kind of round-about way just to ensure that I understand how objects work – and even more importantly, how a vector of objects works. 

I feel like I am getting the hard part to work fine (creating objects on the fly, storing them in a vector and calling up details from the objects. However, I have stored a counter integer in a vector and I can’t seem to get it to work correctly. Why store a counter in an object? I want to have the counter in a safe place – away from the other objects, but not as a global variable. 

It looks like it is resetting to 0 each time I reach in to get a unique ID number.

Does anyone out there see what I’m doing wrong?

 

code:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//  Copyright (c) 2014 Jack Treml. All rights reserved.

//

 

/*

 1. empty car (0)

 

 2. pick up Pennywise and John Wayne Gacy (2)

 

 3. Pick up Bozo and Krusty (4)

 

 4. Pick up Ronald McDonald (5)

 

 5. Ronald McDonald gets kicked out (4)

 

 6. All the rest stop for a drink at the Alehouse together (0)

 

 */

 

#include <iostream>

#include <vector>

usingnamespacestd;

using std::vector;

 

class IdNumbers{     // a place to store a counter for creating idNumbers

public:

    IdNumbers(int x){

        absoluteNumber = x;

    }

    int getANewNumber(void){

        cout<<“number is “<<absoluteNumber<<endl;

        absoluteNumber = advanceNumber();

        cout<<“number is now “<<absoluteNumber<<endl;

        returnabsoluteNumber;

    }

    int advanceNumber(void){

        absoluteNumber ++;

        returnabsoluteNumber;

    }

private:

    int absoluteNumber;

};

 

class Clown{   // the clown objects

public:

    Clown(){};

    Clown(int clownId){

        idNum = clownId;

    };

    Clown(string nameOfClown, IdNumbers theNumber){

        clownName = nameOfClown;

        idNum = theNumber.getANewNumber();

        

    };

    int getClownId(void){

        return idNum;

    }

    void printClown(void){

        cout<<“idNumber = “<<idNum<<endl;

        cout<<“name = “<<clownName<<endl;

        

    };

    

    

private:

    string clownName;

    int idNum;

    

};

 

//functions

void printVectorContents(vector<Clown>);

 

int main(int argc, const char * argv[])

{

    

    IdNumbers *absoluteNumbers = newIdNumbers(0);   //initializes a running id Number

    

    

    vector<Clown> clowncar;     //sets up a vector for fish

    int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    Clown pennywise(“Pennywise”, *absoluteNumbers);

    clowncar.push_back(pennywise);

    Clown jwgacy(“John Wayne Gacy”, *absoluteNumbers);

    clowncar.push_back(jwgacy);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    Clown bozo(“Bozo”, *absoluteNumbers);

    clowncar.push_back(bozo);

    Clown krusty(“Krusty”, *absoluteNumbers);

    clowncar.push_back(krusty);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    Clown ronald(“Ronald McDonald”, *absoluteNumbers);

    clowncar.push_back(ronald);

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.pop_back();

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    while(clowncar.size() > 0){

        clowncar.pop_back();

    }

    vectorSize = clowncar.size(); //get array size

    cout<<“**************”<<endl;

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

 

    

    

    return 0;

}

 

 

//functions

void printVectorContents(vector<Clown> car){

    for(int i=0; i < car.size(); i++){

        car[i].printClown();

        

    }

}

 

 

/*  output:

 

 **************

 vector is size: 0

 number is 0

 number is now 1

 number is 0

 number is now 1

 **************

 vector is size: 2

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 number is 0

 number is now 1

 number is 0

 number is now 1

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 idNumber = 1

 name = Bozo

 idNumber = 1

 name = Krusty

 number is 0

 number is now 1

 **************

 vector is size: 5

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 idNumber = 1

 name = Bozo

 idNumber = 1

 name = Krusty

 idNumber = 1

 name = Ronald McDonald

 **************

 vector is size: 4

 idNumber = 1

 name = Pennywise

 idNumber = 1

 name = John Wayne Gacy

 idNumber = 1

 name = Bozo

 idNumber = 1

 name = Krusty

 **************

 vector is size: 0

 Program ended with exit code: 0

 

*/

clowncar vector solution

Amazingly, this solution came fast an easily:

//

//  main.cpp

//  clowncar

//

//  Created by Jack on 5/4/14.

//

 

/*

 1. empty car (0)

 2. pick up Pennywise and John Wayne Gacy (2)

 3. Pick up Bozo and Krusty (4) 

 4. Pick up Ronald McDonald (5)

 5. Ronald McDonald gets kicked out (4)

 6. All the rest stop for a drink at the Alehouse together (0)

 */

 

#include <iostream>

#include <vector>

usingnamespacestd;

using std::vector;

 

//functions

void printVectorContents(vector<string>);

 

int main(int argc, const char * argv[])

{

    vector<string> clowncar;     //sets up a vector for fish

    int vectorSize = 0;

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    clowncar.push_back(“Pennywise”);

    clowncar.push_back(“John Wayne Gacy”);

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.push_back(“Bozo”);

    clowncar.push_back(“Krusty”);

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.push_back(“Ronald McDonald”);

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    clowncar.pop_back();

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

    

    while(clowncar.size() > 0){

        clowncar.pop_back();

    }

    vectorSize = clowncar.size(); //get array size

    cout<<“vector is size: “<<vectorSize<<endl;

    printVectorContents(clowncar);

 

    return 0;

}

 

 

//functions

void printVectorContents(vector<string> car){

    for(int i=0; i < car.size(); i++){

        cout<<“clown #”<<i<<“: “<<car[i]<<endl;

    }

}

 

 

/*  output – surprisingly, this worked right first try.

    the timing of the cout statements for vector size is a bit kooky, but it’s working the way I wanted

 

 vector is size: 0

 vector is size: 2

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 vector is size: 4

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 clown #2: Bozo

 clown #3: Krusty

 vector is size: 5

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 clown #2: Bozo

 clown #3: Krusty

 clown #4: Ronald McDonald

 vector is size: 4

 clown #0: Pennywise

 clown #1: John Wayne Gacy

 clown #2: Bozo

 clown #3: Krusty

 vector is size: 0

 Program ended with exit code: 0

 

*/

Previous Older Entries