I have created a semi-medium program (in size) to simulate any battle for all of your battling needs!
I would like some feedback on how it is (not to great at programming in c++) or you can just put your name in there and use it however you would like!
Warning, there are a lot of comments, so you can practically read this like a story.
There are two files, one called main.cpp and the other called dependencies.h I will put the header file first.
dependencies.h
#ifndef __dependencies_H_INCLUDED__
#define __dependencies_H_INCLUDED__
using namespace std;
//roll the dice
int rollDice(int diceType) {
//var declaration
int diceSide = 0;
//all the dice types
if (diceType == 4) diceSide = round(rand() % (4));
if (diceType == 6) diceSide = round(rand() % (6));
if (diceType == 8) diceSide = round(rand() % (8));
if (diceType == 10) diceSide = round(rand() % (10));
if (diceType == 12) diceSide = round(rand() % (12));
if (diceType == 20) diceSide = round(rand() % (20));
//return the side the dice landed on
return diceSide;
}
//player class
class Player {
public:
//variable declaration
string name;
string classType;
int strength, endurance;
int id, DT = 20; //DT is dice type by the way
//get players health
int getHP() {
return (10 + ((strength + endurance) * 2));
}
//get players hit
int getHit() {
return rollDice(DT);
} //get damage
};
//Enemies
class Enemy {
public:
//var declaration
string name;
double AC; //armor class ablity to resist hits
int DT; //dice used to attack
int eid; //id for Enemies (Enemy id)
int HP = round(HP*(1+AC)); //should I have done this with the player class? Eh to late now
int getHit() {
return rollDice(DT);
} //get damage
};
//Get context of the situation for enemy fight
string enemyContxt(int option) {
//make random numbers so the fight stuff seems 'random'
int randNum1 = round(rand() % (9) + 1);
int randNum2 = round(rand() % (9) + 1);
int randNum3 = round(rand() % (9) + 1);
int randNum4 = round(rand() % (9) + 1);
int randNum5 = round(rand() % (9) + 1);
//names for enemies
string name1 = "\nBruneor the ";
string name2 = "\nRichard the ";
string name3 = "\nFilbert the ";
string name4 = "\nLodric the ";
string name5 = "\nRuuker the ";
string name6 = "\nKruger the ";
string name7 = "\nCharles the ";
string name8 = "\nAaarl the " ;
string name9 = "\nVasiilk the ";
string name10 = "\nGubl the ";
//introduction of situation strings
string intro1 = " hits you with a blunt slinky ";
string intro2 = " whacks you with a feather ";
string intro3 = " pushes you into Tiny Tim ";
string intro4 = " stabs you with a lamp ";
string intro5 = " shoots you with an M16 catapult ";
string intro6 = " summons a spirit to pester you ";
string intro7 = " uses a rune-stothe enchantment ";
string intro8 = " tries to curse you but explodes an unfourtunate chicken, due to a terrible mispronuncation of your name ";
string intro9 = " simply does nothing ";
string intro10 = " burps up a gnerm (a miniature knome) ";
//strings to help transition to next statement
string trans1 = "and says 'Die, filthy swine!' ";
string trans2 = "then trips on a gruubliyth. ";
string trans3 = "and then, snarls! ";
string trans4 = "and then begins to mutter an ancient curse! ";
string trans5 = "then yells 'You hit like a Kerbb hehe!' ";
string trans6 = "and says 'Die, filthy swine!' ";
string trans7 = "then trips on a gruubliyth. ";
string trans8 = "and then, snarls! ";
string trans9 = "and then begins to mutter an ancient curse! ";
string trans10 = "then yells 'You hit like a Uerbb hehe!' ";
//check if user selected an option then use random number var to decide string and return that string
if (option == 1) {
if (randNum1 == 1) return name1;
if (randNum1 == 2) return name2;
if (randNum1 == 3) return name3;
if (randNum1 == 4) return name4;
if (randNum1 == 5) return name5;
if (randNum1 == 6) return name6;
if (randNum1 == 7) return name7;
if (randNum1 == 8) return name8;
if (randNum1 == 9) return name9;
if (randNum1 == 10) return name10;
}
if (option == 2) {
if (randNum2 == 1) return intro1;
if (randNum2 == 2) return intro2;
if (randNum2 == 3) return intro3;
if (randNum2 == 4) return intro4;
if (randNum2 == 5) return intro5;
if (randNum2 == 6) return intro6;
if (randNum2 == 7) return intro7;
if (randNum2 == 8) return intro8;
if (randNum2 == 9) return intro9;
if (randNum2 == 10) return intro10;
}
if (option == 3) {
if (randNum3 == 1) return trans1;
if (randNum3 == 2) return trans2;
if (randNum3 == 3) return trans3;
if (randNum3 == 4) return trans4;
if (randNum3 == 5) return trans5;
if (randNum3 == 6) return trans6;
if (randNum3 == 7) return trans7;
if (randNum3 == 8) return trans8;
if (randNum3 == 9) return trans9;
if (randNum3 == 10) return trans10;
}
}
//Get context of the situation
string playerContxt(Player &player) {
int randNum = round(rand() % (19) + 1);
//pretty much the same thing with the finction above
string name = player.name;
if (randNum == 1) return "\n" + name + " strikes with an evil Urrgleumbeck ";
if (randNum == 2) return "\n" + name + " hits, but epicly fails and hits a wall causing a rupture in time itself ";
if (randNum == 3) return "\n" + name + " trips on an explosive turtle ";
if (randNum == 4) return "\n" + name + " lunges at his enemy ";
if (randNum == 5) return "\n" + name + " sneezes violently causing a worldwide pandemic ";
if (randNum == 6) return "\n" + name + " swiftly hacks at his enemy using a knerm ";
if (randNum == 7) return "\n" + name + " summons the almighty mega-knerm ";
if (randNum == 8) return "\n" + name + " summons a crude writhe-golem ";
if (randNum == 9) return "\n" + name + " casts an ancient curse";
if (randNum == 10) return "\n" + name + " yells 'AVADA CADABRA!' ";
if (randNum == 11) return "\n" + name + " falls painfully ";
if (randNum == 12) return "\n" + name + " throws a strauug gas grenade ";
if (randNum == 13) return "\n" + name + " fires a portable villkreek mortar ";
if (randNum == 14) return "\n" + name + " strikes with a pirated knerm sword ";
if (randNum == 15) return "\n" + name + " drinks a super-enchantment giving him the ability to eat apples 10 times faster than normal ";
if (randNum == 16) return "\n" + name + " summons Tiny Tim who calls upon his liege ";
if (randNum == 17) return "\n" + name + " calls upon a skeleton to do his bidding";
if (randNum == 18) return "\n" + name + " strikes with a molten axe";
if (randNum == 19) return "\n" + name + " hits a tree with his head causing it to fall ";
if (randNum == 20) return "\n" + name + " calls upon the ancient curse of Ugaar ";
}
//fight an Enemy (option 1)
int fightEnemy(Player &player, Enemy &enemy) {
//declare all of these variables
int eHit = enemy.getHit();
int pHit = player.getHit();
int eHP = enemy.HP;
int pHP = player.getHP();
int playerLastRoll = pHit;
int enemyLastRoll = eHit;
int counter = 0;
string name = enemyContxt(1);
//welcome the user to the arena!
cout << "\n->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->\n";
cout << "Welcome to the Arena!\n";
cout << "Starting HP: \n" << player.name << "'s HP: " << pHP << "\n" << enemy.name << "'s HP: " << eHP << "\n";
cout << "Begin Battle!\n";
//for loop to fight until the death
for(;;) {
//as long as both the hits are not the same and it isnt the first round we should be ok
while(playerLastRoll == pHit || enemyLastRoll == eHit && counter != 0){
//if it is keep looping this until these variables are different
eHit = enemy.getHit();
pHit = player.getHit();
}
//pause for a second, give the user some xor_eqconst_cast
sleep(1);
//use contxt function and print out message of hit
cout << name << enemy.name << enemyContxt(2) << enemyContxt(3) << " Dealing " << eHit << " Damage!\n";\
//subtract damage from health
pHP = pHP - eHit;
//is the player dead if statement
if (pHP <= 0) {
//if they are tell the user and break the loop
cout << "\n" << player.name << " is Dead!\n";
cout << enemy.name << "'s HP left: " << eHP << "\n";
cout << player.name << "'s HP left: " << pHP << "\n";
break;
} else {
//else keep going
sleep(1);
//the same as above just switched around really
cout << "\n" << playerContxt(player) << " Dealing " << pHit << " Damage!\n";
eHP = eHP - pHit;
if (eHP <= 0) {
cout << "\n" << enemy.name << " is Dead!\n";
cout << enemy.name << "'s HP left: " << eHP << "\n";
cout << player.name << "'s HP left: " << pHP << "\n";
break;
}
}
//make the last roll be the last roll so the while loop can know in the future
playerLastRoll = pHit;
enemyLastRoll = eHit;
counter++;
}
//return function presumably after someone has died (hopefully)
return 0;
}
//fight a Player (option 2)
int fightPlayer(Player &player1, Player &player2) {
//variable declaration
int pHit1 = player1.getHit();
int pHit2 = player2.getHit();
int pHP1 = player1.getHP();
int pHP2 = player2.getHP();
int playerLastRoll1 = pHit1;
int playerLastRoll2 = pHit2;
int counter = 0;
//same thing as the function above just with two players
cout << "\n->->->->->->->->->->->->->->->->->->->->->->->->->->->->->->\n";
cout << "Welcome to the Arena!\n";
cout << "Starting HP: \n" << player1.name << "'s HP: " << pHP1 << "\n" << player2.name << "'s HP: " << pHP2 << "\n";
cout << "Begin Battle!\n";
for(;;) {
while(playerLastRoll1 == pHit1 || playerLastRoll2 == pHit2 && counter != 0){
pHit1 = player1.getHit();
pHit2 = player2.getHit();
}
sleep(1);
cout << "\n" << playerContxt(player1) << " Dealing " << pHit1 << " Damage!\n";
pHP2 = pHP2 - pHit1;
if (pHP2 <= 0) {
cout << "\n" << player2.name << " is Dead!\n";
cout << player1.name << "'s HP left: " << pHP1 << "\n";
cout << player2.name << "'s HP left: " << pHP2 << "\n";
break;
} else {
sleep(1);
cout << "\n" << playerContxt(player2) << " Dealing " << pHit2 << " Damage!\n";
pHP1 = pHP1 - pHit2;
if (pHP1 <= 0) {
cout << "\n" << player1.name << " is Dead!\n";
cout << player1.name << "'s HP left: " << pHP1 << "\n";
cout << player2.name << "'s HP left: " << pHP2 << "\n";
break;
}
}
playerLastRoll1 = pHit1;
playerLastRoll2 = pHit2;
counter++;
}
//kill the function before it becomes sentient!
return 0;
}
//check for every possible combo in each of the vectors
int enemyComboCheck(int id1, int id2, vector<Player> &allPlayers, vector<Enemy> &allEnemies){
//declare iterators and booleans
vector<Player>::iterator player_iter = allPlayers.begin();
vector<Enemy>::iterator enemy_iter;
bool found = false;
// try to find player
while (player_iter != allPlayers.end() && !found) {
if (player_iter->id == id1)
found = true;
else
player_iter++;
}
// when player has been found, try to find enemy
if (found) {
found = false;
enemy_iter = allEnemies.begin();
while (enemy_iter != allEnemies.end() && !found) {
if (enemy_iter->eid == id2)
found = true;
else
enemy_iter++;
}
// if both have been found call function fightEnemy
if (found)
fightEnemy(*player_iter, *enemy_iter);
return 0;
}
return 0;
}
//same as the function above just for two players though
int playerComboCheck(int id1, int id2, vector<Player> &allPlayers){
vector<Player>::iterator player_iter1 = allPlayers.begin();
vector<Player>::iterator player_iter2;
bool found = false;
// try to find player 1
while (player_iter1 != allPlayers.end() && !found) {
if (player_iter1->id == id1)
found = true;
else
player_iter1++;
}
// when player has been found, try to find player 2
if (found) {
found = false;
player_iter2 = allPlayers.begin();
while (player_iter2 != allPlayers.end() && !found) {
if (player_iter2->id == id2)
found = true;
else
player_iter2++;
}
// if both have been found call function fightPlayer
if (found)
fightPlayer(*player_iter1, *player_iter2);
return 0;
}
return 0;
}
//end of header file!
#endif
main.cpp
//all the libraries
#include <iostream>
#include <string>
#include <time.h>
//#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include <vector>
#include "dependencies.h"
//so I dont have to type a million std prefixes
using namespace std;
//main function
int main() {
//set a random seed based on the time
srand(time(NULL));
//vector to add all the players and enemies for scanning later on
vector<Player> allPlayers;
vector<Enemy> allEnemies;
//player declaration below
//yourname1
Player yourname1;
yourname1.name = "yourname1";
yourname1.classType = "Scribe";
yourname1.strength = 3;
yourname1.endurance = 6;
yourname1.id = 1;
//push back this player to the vector
allPlayers.push_back(yourname1);
//------------------------------------------
//yourname2
Player yourname2;
yourname2.name = "yourname2";
yourname2.classType = "Cursed";
yourname2.strength = 3;
yourname2.endurance = 3;
yourname2.id = 2;
//push back this player to the vector
allPlayers.push_back(yourname2);
//------------------------------------------
//yourname3
Player yourname3;
yourname3.name = "yourname3";
yourname3.classType = "Thief";
yourname3.strength = 3;
yourname3.endurance = 1;
yourname3.id = 3;
//push back this player to the vector
allPlayers.push_back(yourname3);
//------------------------------------------
//yourname4
Player yourname4;
yourname4.name = "yourname4";
yourname4.classType = "Paladin";
yourname4.strength = 9;
yourname4.endurance = 5;
yourname4.id = 4;
//push back this player to the vector
allPlayers.push_back(yourname4);
//------------------------------------------
//yourname5
Player yourname5;
yourname5.name = "yourname5";
yourname5.classType = "Hobo";
yourname5.strength = 3;
yourname5.endurance = 7;
yourname5.id = 5;
//push back this player to the vector
allPlayers.push_back(yourname5);
//------------------------------------------
//Enemy declaration below
//AmatuerGuard
Enemy AmatuerGuard;
AmatuerGuard.name = "Amatuer Guard";
AmatuerGuard.HP = 10;
AmatuerGuard.AC = 0.05;
AmatuerGuard.DT = 4;
AmatuerGuard.eid = 1;
//push back this enemy to the vector
allEnemies.push_back(AmatuerGuard);
//MediocreGuard
Enemy MediocreGuard;
MediocreGuard.name = "Mediocre Guard";
MediocreGuard.HP = 15;
MediocreGuard.AC = 0.1;
MediocreGuard.DT = 8;
MediocreGuard.eid = 2;
//push back this enemy to the vector
allEnemies.push_back(MediocreGuard);
//Knight
Enemy Knight;
Knight.name = "Knight";
Knight.HP = 20;
Knight.AC = 0.2;
Knight.DT = 12;
Knight.eid = 3;
//push back this enemy to the vector
allEnemies.push_back(Knight);
//OkachiRaider
Enemy OkachiRaider;
OkachiRaider.name = "Okachi Raider";
OkachiRaider.HP = 10;
OkachiRaider.AC = 0.15;
OkachiRaider.DT = 6;
OkachiRaider.eid = 4;
//push back this enemy to the vector
allEnemies.push_back(OkachiRaider);
//OkachiPaladin
Enemy OkachiPaladin;
OkachiPaladin.name = "Okachi Paladin";
OkachiPaladin.HP = 25;
OkachiPaladin.AC = 0.1;
OkachiPaladin.DT = 10;
OkachiPaladin.eid = 5;
//push back this enemy to the vector
allEnemies.push_back(OkachiPaladin);
//OkachiMage
Enemy OkachiMage;
OkachiMage.name = "Okachi Mage";
OkachiMage.HP = 15;
OkachiMage.AC = 0;
OkachiMage.DT = 20;
OkachiMage.eid = 6;
//push back this enemy to the vector
allEnemies.push_back(OkachiMage);
//FrostGiant
Enemy FrostGiant;
FrostGiant.name = "Frost Giant";
FrostGiant.HP = 30;
FrostGiant.AC = 0.2;
FrostGiant.DT = 10;
FrostGiant.eid = 7;
//push back this enemy to the vector
allEnemies.push_back(FrostGiant);
//DuneViper
Enemy DuneViper;
DuneViper.name = "Dune Viper";
DuneViper.HP = 10;
DuneViper.AC = 0.15;
DuneViper.DT = 10;
DuneViper.eid = 8;
//push back this enemy to the vector
allEnemies.push_back(DuneViper);
//Burrowurm
Enemy Burrowurm;
Burrowurm.name = "Burrowurm";
Burrowurm.HP = 10;
Burrowurm.AC = 0.25;
Burrowurm.DT = 8;
Burrowurm.eid = 9;
//push back this enemy to the vector
allEnemies.push_back(Burrowurm);
//Behemoth
Enemy Behemoth;
Behemoth.name = "Behemoth";
Behemoth.HP = 65;
Behemoth.AC = 0.25;
Behemoth.DT = 6;
Behemoth.eid = 10;
//push back this enemy to the vector
allEnemies.push_back(Behemoth);
//RabidSquirrel
Enemy RabidSquirrel;
RabidSquirrel.name = "Rabid Squirrel";
RabidSquirrel.HP = 10;
RabidSquirrel.AC = 0;
RabidSquirrel.DT = 20;
RabidSquirrel.eid = 11;
//push back this enemy to the vector;
allEnemies.push_back(RabidSquirrel);
//Wolf
Enemy Wolf;
Wolf.name = "Wolf";
Wolf.HP = 15;
Wolf.AC = 0.2;
Wolf.DT = 8;
Wolf.eid = 12;
//push back this enemy to the vector
allEnemies.push_back(Wolf);
//DireWolf
Enemy DireWolf;
DireWolf.name = "Dire Wolf";
DireWolf.HP = 20;
DireWolf.AC = 0.2;
DireWolf.DT = 12;
DireWolf.eid = 13;
//push back this enemy to the vector
allEnemies.push_back(DireWolf);
//InsaneHobo
Enemy InsaneHobo;
InsaneHobo.name = "Insane Hobo";
InsaneHobo.HP = 10;
InsaneHobo.AC = 0.05;
InsaneHobo.DT = 12;
InsaneHobo.eid = 14;
//push back this enemy to the vector
allEnemies.push_back(InsaneHobo);
//ForestArcher
Enemy ForestArcher;
ForestArcher.name = "Forest Archer";
ForestArcher.HP = 15;
ForestArcher.AC = 0.15;
ForestArcher.DT = 10;
ForestArcher.eid = 15;
//push back this enemy to the vector
allEnemies.push_back(ForestArcher);
//ForestKnight
Enemy ForestKnight;
ForestKnight.name = "Forest Knight";
ForestKnight.HP = 20;
ForestKnight.AC = 0.1;
ForestKnight.DT = 8;
ForestKnight.eid = 16;
//push back this enemy to the vector
allEnemies.push_back(ForestKnight);
//ForestKnight
Enemy ForestMage;
ForestMage.name = "Forest Mage";
ForestMage.HP = 10;
ForestMage.AC = 0.15;
ForestMage.DT = 10;
ForestMage.eid = 17;
//push back this enemy to the vector
allEnemies.push_back(ForestMage);
//MurderOfCrows
Enemy MurderOfCrows;
MurderOfCrows.name = "Murder Of Crows";
MurderOfCrows.HP = 10;
MurderOfCrows.AC = 0.1;
MurderOfCrows.DT = 10;
MurderOfCrows.eid = 18;
//push back this enemy to the vector
allEnemies.push_back(MurderOfCrows);
//core
//variable declaration
int choice = 0;
//ask user to select function
cout << "Please select a function: \n";
cout << "1) Fight Against an Enemy\n";
cout << "2) Fight Against Another Player\n";
cout << "Response: ";
cin >> choice; //take in the response to the choice variable
//not sure why I didn't just use an if statement for these
switch (choice) {
//check if choice is 1
case 1:
//uid stands for user input id
int uid1;
int uid2;
//ask for player id
cout << "\nEnter your Id (1-5): ";
cin >> uid1; //take it in to uid1
//ask for enemy id
cout << "\nEnter your Enemies Id (1-18): ";
cin >> uid2; //take it in to uid2
//call the ingenious function to check all possiblilities of enemies and players
enemyComboCheck(uid1, uid2, allPlayers, allEnemies);
break;
case 2:
//same thing as above but just with another player instead of an enemy
cout << "\nEnter Player 1's Id (1-5): ";
cin >> uid1;
cout << "\nEnter Player 2's Id (1-5): ";
cin >> uid2;
//different function too
playerComboCheck(uid1, uid2, allPlayers);
break;
//if you didn't do anything right resort to this
default:
cout << "Input invalid. Bye!\n";
return 0;
break;
}
//ask user if they want to run the program again
string endornot;
sleep(1);
cout << "\nRun again y/n: ";
cin >> endornot;
if (endornot == "y" || endornot == "Y") return main();
if (endornot == "n" || endornot == "N") cout << "Shutting Down...\n"; return 0;
}
1 Answer 1
Summary
A lot of work still needs to be done on this.
Fix all the suggestions below then resubmit for further review.
Dice Rolling.
if (diceType == 4) diceSide = round(rand() % (4));
if (diceType == 6) diceSide = round(rand() % (6));
...
Why not:
diceSide = rand() % diceType; // note `round()` not needed `%` is an int operation
Random Number.
Using random like that does not give you equal probability.
The function rand()
returns a number in the range [0..RAND_MAX]
. If RAND_MAX
is not an exact divisor of the number you use after the %
operator then you get an uneven distribution.
Example:
Say RAND_MAX is 32768
Say you are rolling a 6 sided dye.
Then probability of a:
0: 5462/32768 // Notice 5462 (not 5461)
1: 5462/32768 // Notice 5462 (not 5461)
2: 5461/32768
3: 5461/32768
4: 5461/32768
5: 5461/32768
To do this so you get an even distribution you need to throw away those extra two values.
int randMax = RAND_MAX / diceType * diceType;
int randVal;
while((randVal = rand()) > randMax) {
// Do Nothing
}
diceSide =randVal % diceType;
Modern Rand
The rand()
and srand()
are old ways of generating random numbers inherited from C. Modern C++ has its own (actually good) random number generators.
See <Random>
// Do this bit once.
std::default_random_engine generator;
// Do this bit when you want to get random number in a specific range.
std::uniform_int_distribution<int> distribution(1, diceType);
int dice_roll = distribution(generator);
Class Design
Hmmmm.
class Player {
public:
//variable declaration
string name;
string classType;
int strength, endurance;
int id, DT = 20; //DT is dice type by the way
//get players health
int getHP() {
return (10 + ((strength + endurance) * 2));
}
//get players hit
int getHit() {
return rollDice(DT);
} //get damage
};
Some basics.
- Member Variables should be private
- Don't use geter/seter methods (they break encapsulation).
- One variable declaration per line.
- Member variables should be initialized in the constructor.
- Member methods should be
verbs
(ie actions).
The action is something you can do to the object. - Don't write comments that say the same thing as the code.
Static Const
If you have some variables used as const. Make them static members.
string name1 = "\nBruneor the ";
string name2 = "\nRichard the ";
string name3 = "\nFilbert the ";
Looks like these should be static const. They are valid for all members. You don't want to initialize them more than once and you don't look like you are going to modify them.
static const string name1 = "\nBruneor the ";
static const string name2 = "\nRichard the ";
static const string name3 = "\nFilbert the ";
Switch
If you have a bunch of if
statements that depend on the value of a single variable then a switch
statement is probably better.
if (randNum1 == 1) return name1;
if (randNum1 == 2) return name2;
if (randNum1 == 3) return name3;
if (randNum1 == 4) return name4;
This would look better as:
switch(randNum1)
{
case 1: return name1;
case 2: return name2;
case 3: return name3;
case 4: return name4;
But an even better technique is to use an array.
static const std::string names[] = {"\nBruneor the ",
"\nRichard the ",
"\nFilbert the ",
.... etc
};
return names[randNum1 - 1];
Member Methods With Action
Going back to the member method that is an action:
int fightEnemy(Player &player, Enemy &enemy) {
//declare all of these variables
int eHit = enemy.getHit();
int pHit = player.getHit();
int eHP = enemy.HP;
int pHP = player.getHP();
int playerLastRoll = pHit;
int enemyLastRoll = eHit;
In this function you get information from the object. You do some calculation then I would expect you to put the result back. But you never update the object (which is weird). OK ignoring the weird part.
You can have a method that manipulates the state of the object when two players are fighting.
int fightEnemy(Player &player, Enemy &enemy)
{
while(player.alive() && enemy.alive()) {
player.attack(enemy);
enemy.attack(player);
}
}
void Attacker::attack(Attacker& victim)
{
getPrimaryWeapon().swingAt(victim);
}
Indentation
Fix the indentation. ITs important; people reading it need this so we can quickly spot the logical scope of operations. When the indentation is broken it makes the code hard to read.
Using std
Stop doing this:
//so I dont have to type a million std prefixes
using namespace std;
Sure you have to add the prefix std::
. But that's better than haing broken code. Read: Why is "using namespace std" considered bad practice?
The reason it is std
and not standard
is so that it is not a big burden to use the prefix.
Add some constructors
This is way to verbose:
Player yourname1;
yourname1.name = "yourname1";
yourname1.classType = "Scribe";
yourname1.strength = 3;
yourname1.endurance = 6;
yourname1.id = 1;
allPlayers.push_back(yourname1);
If you add a constructor this becomes:
allPlayers.push_back(Player("yourname1", "Scribe", 3, 6)); // Id should be generated.
As long as you are not using an ancient compiler (a C++03) then you can use emplace rather than push.
allPlayers.emplace_back("yourname1", "Scribe", 3, 6);
-
\$\begingroup\$ Thank you for all this! I have tried to take all of your suggestions and got most to work, but others didn't. The constructor part was key and I have updated my code above! \$\endgroup\$moskaut– moskaut2017年05月24日 05:18:36 +00:00Commented May 24, 2017 at 5:18