The lecturer made an OO Zombie game in C++, I was following it along fine in class. However I decided to take a look at the code when I got home as I wanted to mess around with it, but for whatever reason the code doesn't work. There are errors. I don't know if the lecturer modified the program or what. Anyhow I'd appreciate it if anyone would know how to fix it. Thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
//main.cpp
#include <iostream>
#include <stdlib.h>
#include "Character.h"
#include "StoryEventHandler.h"
struct zombie
{
int hunger;
int speed;
void setHunger(int a)
{
hunger = a;
setSpeed(hunger * 2);
}
int getHunger()
{
return hunger;
}
void setSpeed(int b)
{
speed = b;
}
int getSpeed()
{
return speed;
}
void print()
{
switch (hunger)
{
case 1:
case 2:
std::cout << "belly is full.... it shuffles slowly" << std::endl;
break;
case 3:
case 4:
std::cout << "his tummy rumbles.... it meanders threateningly" << std::endl;
break;
case 5:
case 6:
std::cout << "hunger consumes the zombie.... it breaks into a crazed run" << std::endl;
break;
case 7:
case 8:
std::cout << "ravenous would describe this accurately.... it begins to leap and run awkwardly" << std::endl;
break;
default:
std::cout << "run...." << std::endl;
}
std::cout << "Speed is " << getSpeed();
}
};
/*int main()
{
zombie ted;
int randhunger = rand() % 10 + 1;
ted.setHunger(randhunger);
//ted.print();
//give the player some options
std::cout << "\nThere is a zombie in front of you.... \n" << std::endl;
ted.print();
std::cout << "\n1. Run \n 2. FIGHT!!!!!!" << std::endl;
int userInput = -1;
userInput = std::cin.get();
if (userInput == 1)
{
if (ted.getHunger() >= 5){
std::cout << "You feel the flesh being torn from your body. CONGRADULATIONS!" << std::endl;
}
else{
std::cout << "You escape the zombie. He gives you a puppy dog look... awww" << std::endl;
}
}
else
{
int randomno = rand() % 10 + 1;
if (3 + randomno > ted.getHunger())
{
std::cout << "You beat the zombie with the nearest blunt object.... I hope you're happy..." << std::endl;
}
else
{
std::cout << "If you can't beat 'em join 'em... You're a zombie HARRY!!!" << std::endl;
}
}
int wait = std::cin.get();
}*/
int main()
{
Character* playerOne = new Character();
Character* playerTwo = new Character();
playerOne->setHealth(10);
playerTwo->setHealth(10);
playerOne->setStrength(20);
playerTwo->setStrength(16);
playerOne->setDefense(8);
playerTwo->setDefense(14);
StoryEventHandler storyHandler;
std::cout << "Let's Fight!";
bool swapAttackDefend = false;
while (playerOne->state == CharacterState::ALIVE && playerTwo->state == CharacterState::ALIVE)
{
if (!swapAttackDefend)
{
storyHandler.Fight(playerOne, playerTwo);
swapAttackDefend = true;
}
else
{
storyHandler.Fight(playerTwo, playerOne);
swapAttackDefend = false;
}
}
if (playerOne->state == CharacterState::DEAD)
{
std::cout << "Player Two wins!!!";
}
if (playerTwo->state == CharacterState::DEAD)
{
std::cout << "Player One wins!!";
}
int wait = std::cin.get();
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
//StoryEventHandler.h
/*
* This class will handle the following story events:
* FIGHT: this takes in two characters and sets the state of those characters (ALIVE, DEAD) and health
* CHOICE: this asks the user to choose between two option and sets characters health based on the choices
* the major method will be getNextStoryPoint() which will give a random story point to the user
*/
#include "Character.h"
class StoryEventHandler
{
public:
//constructor
StoryEventHandler();
//deconstructor
~StoryEventHandler();
void Fight(Character* attacker, Character* defender);
//calls a random next choice
void getNextStoryPoint();
private:
//private method fight
//private method choice
void Choice();
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
//Character.h
enum CharacterState {ALIVE, DEAD};
class Character
{
public:
Character();
~Character(){};
//getters
int getHealth(){return health;}
int getStrength(){return strength;}
int getDefense(){return defense;}
//setters
void setHealth(int newhealth);
void setStrength(int newstrength){strength = newstrength;}
void setDefense(int newdefense){defense = newdefense;}
CharacterState state;
//private:
//private variables
int health, strength, defense;
};
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
//Character.cpp
#include "Character.h"
Character::Character()
{
health = 0;
strength = 0;
defense = 0;
state = CharacterState::ALIVE;
}
void Character::setHealth(int newhealth)
{
health = newhealth;
if (health <= 0)
{
state = CharacterState::DEAD;
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
//StoryEventHanndler.cpp
#include "StoryEventHandler.h"
#include <iostream>
StoryEventHandler::StoryEventHandler()
{
//initialize variables here
}
/*StoryEventHandler::StoryEventHandler()
{
}
*/
void StoryEventHandler::getNextStoryPoint()
{
}
void StoryEventHandler::Fight(Character* attacker, Character* defender)
{
std::cout << "Beginning Attack \n";
if (attacker->getStrength() < -defender->getDefense()) //if pointer use ->getHealth instead of .getHealth
{
int temphealth = defender->getHealth();
int damage = attacker->getStrength() - defender->getDefense();
temphealth = temphealth - damage;
defender->setHealth(temphealth);
std::cout << "Attacker hits dealing" << damage << " DAMAGE!! \n";
}
else
{
std::cout << "Attacker Misses!! \n";
}
}
void StoryEventHandler::Choice()
{
}
|
Here are the errors I'm getting:
Error C2027 use of undefined type 'CharacterState' main.cp 158
Error C2011 'CharacterState': 'enum' type redefinition Character.h 1
Error C2011 'Character': 'class' type redefinition Character.h 4
Error C2027 use of undefined type 'Character' main.cpp 125
Error C2227 left of '->{ctor}' must point to class/struct/union/generic type main.cpp 125
Error C2027 use of undefined type 'Character' main.cpp 126
Error C2227 left of '->{ctor}' must point to class/struct/union/generic type main.cpp 126
Error C2027 use of undefined type 'Character' main.cpp 127
Error C2227 left of '->setHealth' must point to class/struct/union/generic type main.cpp 127
Error C2027 use of undefined type 'Character' main.cpp 128
Error C2227 left of '->setHealth' must point to class/struct/union/generic type main.cpp 128