I have been programming in Java for about 2.5 years now, and I am trying to teach myself C++. I coded this game in about 2 hours today, plus about 30 minutes to balance the enemies. Can you guys tell me how my coding looks? The game is pretty basic, but I just want to know how to make my coding better.
stdafx.h
//Included libraries
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
class Weapon;
class Armor;
class Hero;
class Monster;
//Weapon Class
class Weapon {
public:
//Default constructor
Weapon();
//Create a weapon with its name and power
Weapon(string weaponName, int attackPower);
//Return functions
const int attackPower();
const string getName();
private:
//Variables for the weapon class
string name;
int attack;
};
//Armor class
class Armor {
public:
//Default Constructor
Armor();
//Create a armor with its defense
Armor(string armorName, int defenseRating);
//Return functions
const int defenseRating();
const string getName();
private:
//Variables for the weapon class
string name;
int defense;
};
class Hero {
public:
//Create a hero with a name
Hero(string heroName);
//Returns methods
const int getLevel();
const int getAttack();
const int getDefense();
const int getMaxHP();
const string getName();
//Level up method
void levelUp();
//Set weapon or armor
void setWep(Weapon wep);
void setArmor(Armor arm);
//Return weapon and armor
Weapon getWep();
Armor getArmor();
private:
//Variables for Hero class
string name;
int attack;
int defense;
int level;
int maxHP;
Weapon currWeapon;
Armor currArmor;
};
//Monster class
class Monster {
public:
//Default constructor
Monster();
//Create a monster with a name, attack, defense and hp
Monster(string monsterType, int monsterAttack,
int monsterDefense, int monsterHP);
//Return methods
const int getAttack();
const int getDefense();
const int getMaxHP();
const string getType();
private:
//Variables for the monster class
string type;
int attack;
int defense;
int maxHP;
};
stdafx.cpp
// stdafx.cpp : source file that includes just the standard includes
// ConRPG.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
//Empty default constructor
Weapon::Weapon() {
}
//Set the name to the string parameter and the attack variable
//to the attackPower parameter
Weapon::Weapon(string weaponName, int attackPower) {
name = weaponName;
attack = attackPower;
}
//Return the attack of the weapon
const int Weapon::attackPower() {
return attack;
}
//Return the name of the weapon
const string Weapon::getName() {
return name;
}
//Empty default constructor
Armor::Armor() {
}
//Create a armor, set the name and defense
Armor::Armor(string armorName, int defenseRating) {
name = armorName;
defense = defenseRating;
}
//Return the defense of the armor
const int Armor::defenseRating() {
return defense;
}
//Return the armor's name
const string Armor::getName() {
return name;
}
//Create a hero with a name and set default values
Hero::Hero(string heroName) {
name = heroName;
attack = 15;
defense = 10;
level = 1;
maxHP = 20;
}
//Return the attack of the hero(attack power + weapon power)
const int Hero::getAttack() {
return attack + Hero::getWep().attackPower();
}
//Return the defense of the hero(defense + armor rating)
const int Hero::getDefense() {
return defense + Hero::getArmor().defenseRating();
}
//Return the hero's level
const int Hero::getLevel() {
return level;
}
//Return the hero's Max HP
const int Hero::getMaxHP() {
return maxHP;
}
//Return the hero's name
const string Hero::getName() {
return name;
}
//Level up the hero, add more attack, defense, hp.
void Hero::levelUp() {
attack += 5;
defense += 2;
maxHP += 5;
level++;
}
//Set the equipped weapon of the hero
void Hero::setWep(Weapon wep) {
currWeapon = wep;
}
//Set the equipped armor of the hero
void Hero::setArmor(Armor arm) {
currArmor = arm;
}
//Return the weapon the hero has
Weapon Hero::getWep() {
return currWeapon;
}
//Return the armor of the hero
Armor Hero::getArmor() {
return currArmor;
}
//Empty Default constructor for the monster
Monster::Monster() {
}
//Create a mosnter with a name, attack, defense and hp
Monster::Monster(string monsterType, int monsterAttack,
int monsterDefense, int monsterHP) {
type = monsterType;
defense = monsterDefense;
attack = monsterAttack;
maxHP = monsterHP;
}
//Get the attack of the monster
const int Monster::getAttack() {
return attack;
}
//Get the defense of the monster
const int Monster::getDefense() {
return defense;
}
//Get the max hp of the monster
const int Monster::getMaxHP() {
return maxHP;
}
//Get monsters type
const string Monster::getType() {
return type;
}
ConRPG.cpp
// ConRPG.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
void initResources();
bool battle(Hero h);
//Used in init resources to keep track of weapons, armor and monsters
Weapon listOfWeps[5];
Armor listOfArmor[5];
Monster listOfMonsters[10];
int main()
{
int gold = 0;
int xp = 0;
int wepNum = 0;
int armorNum = 0;
int currHP = 0;
int choice = 0;
int option = 0;
string heroName;
//Create all the weapons, armor and monsters
initResources();
//Get the name of the hero
cout << "Welcome hero, can you please give us your name?" << endl;
cout << "My name is ";
cin >> heroName;
cout << endl;
//Create a hero using the name recieved with cin
Hero h(heroName);
//Set hero's weapon and armor to the first in each list
h.setWep(listOfWeps[0]);
h.setArmor(listOfArmor[0]);
//Run the game loop
while(true) {
//Display the information about the hero
cout << "Name: " << h.getName()<< endl;
cout << "XP: " << xp << " Gold: " << gold << endl;
cout << "Level: " << h.getLevel() << " Max HP: " << h.getMaxHP() << endl;
cout << "Weapon: " << h.getWep().getName() << " Armor: " << h.getArmor().getName() << endl << endl;
//Option menu
cout << "1. Fight Monster" << endl;
cout << "2. Shop" << endl;
cout << "3. Stats" << endl;
cout << "4. Exit" << endl;
//Get the choice the player wants using cin
cout << "Choice: ";
cin >> choice;
cout << endl;
//If it is not a valid choice, at least a number one
//through 4, then tell the user of their error
//NOTE: A non-integer choice will cause the game to break
if(choice > 0 && choice < 5) {
//Start a battle
if(choice == 1) {
//Create a battle with the hero
bool condition = battle(&h);
//See how the battle went
if (condition) {
//If the hero is level 10,and has fought the final monster
//the game is over
if(h.getLevel() == 10) {
cout << "You killed the evil overlord. You win." << endl;
break;
}
//Give the player 20 gold if he wins
gold += 20;
if(h.getLevel() < 10) {
//If under level 10 also give 50 xp
xp += 50;
}
} else
//If the player lost, end the game loop
break;
//Check for a level up.
if(xp >= 250) {
cout << "Level up!" << endl << endl;
//If the necessary XP was obtained call the hero level up
h.levelUp();
//Remove the 250 XP
xp -= 250;
}
//Go to the shop
} else if(choice == 2) {
//Stay in the shop until 3. Leave Shop is chosen
while(option != 3) {
//Display the shop menu
cout << "Welcome to the shop." << endl;
cout << "Do you wish to upgrade your weapon or armor?" << endl;
cout << "1. Weapon(100 GP)" << endl;
cout << "2. Armor(100 GP)" << endl;
cout << "3. Leave Shop" << endl;
cout << "Option: ";
//Get the option for the shop from cin
cin >> option;
cout << endl;
//Make sure the options are valid
//Note: A non-integer will break the game
if(option > 0 && option < 4) {
//If a new weapon is chosen, make sure a
//weapon is available and the player has
//enough gold.
if(option == 1) {
if(gold >= 100 && wepNum < 4) {
gold -= 100;
h.setWep(listOfWeps[++wepNum]);
cout << "You now have a " << listOfWeps[wepNum].getName() << endl << endl;
} else if(wepNum == 4) {
//If there are no more weapons
cout << "No more weapons left." << endl << endl;
option = 0;
} else if(gold < 100) {
//If gold is the problem
cout << "Not enough gold." << endl << endl;
option = 0;
}
//If a new armor is chosen, make sure a
//armor is available and the player has
//enough gold.
} else if(option == 2) {
if(gold >= 100 && armorNum < 4) {
gold -= 100;
h.setArmor(listOfArmor[++armorNum]);
cout << "You now have a " << listOfArmor[armorNum].getName() << endl << endl;
} else if(armorNum == 4) {
//If there are no more pieces of are to get
cout << "No more armor left." << endl << endl;
option = 0;
} else if(gold < 100) {
//If gold is the problem
cout << "Not enough gold." << endl << endl;
option = 0;
}
}
} else {
//If the number was not valid
cout << "Not a valid option.";
option = 0;
}
}
option = 0;
//Option 3 just re writes the choices
} else if(choice == 4) {
//Exit the loop so the game ends
break;
}
} else {
//Tell the user they made an illegal move
cout << "Not a valid choice. Choose again.\n" << endl;
choice = 0;
}
}
//End the game
cout << "Game over, thank you for playing." << endl;
//Wait for a button to be pressed
system("PAUSE");
return 0;
}
void initResources() {
//Create the array of weapons the player can use
listOfWeps[0] = Weapon("Rusty Sword", 4);
listOfWeps[1] = Weapon("Broad Axe", 7);
listOfWeps[2] = Weapon("Long Sword", 10);
listOfWeps[3] = Weapon("Pike", 12);
listOfWeps[4] = Weapon("Flaming Sword", 15);
//Create the array of armor the player can wear
listOfArmor[0] = Armor("Clothes", 1);
listOfArmor[1] = Armor("Leather Armor", 3);
listOfArmor[2] = Armor("Chain Mail", 5);
listOfArmor[3] = Armor("Plate Mail", 7);
listOfArmor[4] = Armor("God's Suit", 10);
//Create the array of monsters for the player to fight
listOfMonsters[0] = Monster("Rabbit", 17, 9, 15);
listOfMonsters[1] = Monster("Goblin", 22, 11, 25);
listOfMonsters[2] = Monster("Gnoll", 28, 17, 27);
listOfMonsters[3] = Monster("Orc", 33, 19, 35);
listOfMonsters[4] = Monster("Minor Demon", 41, 17, 40);
listOfMonsters[5] = Monster("Demon", 49, 19, 45);
listOfMonsters[6] = Monster("Major Demon", 55, 21, 52);
listOfMonsters[7] = Monster("Vampire", 62, 24, 55);
listOfMonsters[8] = Monster("Overlord Minion", 65, 25, 63);
listOfMonsters[9] = Monster("Evil Overlord", 70, 27, 70);
}
bool battle(Hero h) {
//Set up the random number
srand(time(0));
int randNum = 0;
//Damage taken and dealt
int attackDamage;
int damage;
//Get the necessary info on the monster the player is about to fight
string name = (listOfMonsters[h.getLevel()-1]).getType();
int attack = (listOfMonsters[h.getLevel()-1]).getAttack();
int defense = (listOfMonsters[h.getLevel()-1]).getDefense();
int hp = (listOfMonsters[h.getLevel()-1]).getMaxHP();
//Create the monsters as a pointer, so he can be destoryed when he is done with
Monster *m = new Monster(name, attack, defense, hp);
//Get the max hp of the hero and monster, set it to a int that can be edited
int heroHP = h.getMaxHP();
int monHP = m->getMaxHP();
//Battle to the death
while(heroHP > 0 && monHP > 0) {
//Display both hero and enemy names and hp
cout << h.getName() << "\t\t\t" << m->getType() << endl;
cout << heroHP << "/" << h.getMaxHP() << "\t\t\t" << monHP << "/" << m->getMaxHP() << endl;
cout << "You atttack each other" << endl;
//Wait for a button to be hit to display the round result
system("PAUSE");
//Get a random number
randNum = rand();
//Create a number 0-3 for the hero to attack
randNum = randNum % 3;
//Damage the hero does to monster opponent
attackDamage = ((h.getAttack() + randNum) - m->getDefense()) / 2;
randNum = rand();
//Createa number 0-4 for the monster attack
randNum = randNum % 4;
//Damage the hero takes back
damage = ((m->getAttack()+ randNum) - h.getDefense()) / 2;
//Remove the hp from both characters
heroHP -= damage;
monHP -= attackDamage;
}
//After the battle if the hero had a hp less than 1, the game is over
if(heroHP < 1) {
cout << "You have died." << endl << endl;
//Set the monster to null, then delete them. Return false
//saying the hero lost
m = 0;
delete m;
return false;
} else {
//If the hero had health, show the result of the battle
cout << h.getName() << "\t\t\t" << m->getType() << endl;
cout << heroHP << "/" << h.getMaxHP() << "\t\t\t" << monHP << "/" << m->getMaxHP() << endl;
cout << "Victory" << endl << endl;
//Set the monster pointer to null, then delete him
m = 0;
delete m;
return true;
}
}
Thanks.
edit: source tags -SiCrane
[Edited by - TriSwords on July 28, 2005 12:45:29 AM]