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……
Recent Comments