I'm having this issue with code evaluation. I want to use a function in one class, and use a function in another but I can't seem to get a prototype working for the function in the class.
class Pride
{
public:
std::string firstMinion = "Pride";
int prideAttackPlayer()
{
int damage = (rand() % 20 + 1) * attack;
return damage;
}
int damagePride()
{
health = health - play.attackEnemy();
}
int outputHealth()
{
return health;
}
private:
int health = 20;
int attack = 0;
int defence = 0;
int money = 0;
}pride;
class Player
{
public:
int attackEnemy()
{
int damage = (rand() % 20 + 1) * attack;
return damage;
}
void userAttack()
{
getline(std::cin, userInput);
if (userInput == "1")
{
std::cout << pride.damagePride();
}
}
private:
int health = 100;
int attack = 0;
int defence = 0;
std::string userInput = "";
}play;
I want to use play.attackEnemy in the Pride class but it won't work due to evaluation errors. I think the fix is to use a prototype but I've tried with int Player::attackEnemy() {}; and class Player but it doesn't do anything.
Related
I'm working on a textual game, I wish to assign a weapon to a player or monster.
I have created a 2d array with min & max damage so I can randomize every hits between min/max.
But I'm stuck for now.
Should I integrate the 2d array in the weapon class ?
How to affect the weapon to each player ?
thanks for any help :)
#include <iostream>
class player{
public:
std::string playerName;
int health;
int maxmana;
int minDegatWeapon;
int maxDegatWeapon;
};
class weapon{
public:
std::string weaponName;
int maxDegatWeapon;
int minDegatWeapon;
};
void createPlayer(player *p, weapon *w){
int weaponSelection = 0;
std::cout << "Player Name ?\n";
std::getline (std::cin,p->playerName);
p->health = 20;
p->maxmana = 80;
std::cout << "Choose your Weapon : 1-Dagger / 2-Sword / 3-Axe ? \n";
std::cin >> weaponSelection;
};
int main(){
player human = {" ", 0, 0, 0, 0};
weapon humanWeapon = {" ", 0, 0};
int weapons[3][2] = {
{3,4}, //dagger
{1,6}, //sword
{0,7} //axe
};
createPlayer(&human, &humanWeapon);
return 0;
}
You can always have class Weapon be an abstract class. There is no "generic weapon" so it would not be appropriate to instantiate an object of type weapon. Instead, have multiple classes inherit from the weapon class, and adjust their min/max value as needed. Further more, you can assign this weapon to the player by adding a weapon to the Player class.
#include <random>
#include <time.h>
class Weapon
{
public:
virtual std::string getWeaponType() = 0;
virtual int generateDamage() = 0;
int minDmg, maxDmg;
};
class Sword : public Weapon
{
public:
Sword()
{
minDmg = 1;
maxDmg = 6;
}
std::string getWeaponType()
{
return "SWORD";
}
int generateDamage()
{
return ( rand() % maxDmg + minDmg );
}
};
class Player
{
public:
Player(int weapon)
{
if(weapon == 1)
{
w = new Sword();
}
//else if(weapon == 2).....
//..................
}
Weapon* getWeapon()
{
return w;
}
private:
Weapon* w;
};
int main()
{
srand(time(NULL)); //random seed
Player p1 = Player(1);
std::cout << p1.getWeapon().generateDamage() << '\n'; //see if it works
}
I have this class
#include "Room.h"
#include "Book.h"
#include <cstdlib>
#include <iostream>
static int book_counter = 100;
//Constructors
Book::Book() {
for(int i = 0; i < 13; i++) {
this->customer_name += 97 + rand() % 26;
}
this->arrival = rand() % 30;
this->duration = 1 + (rand() % 10);
this->ppl = 1 + rand() % 5;
this->room = nullptr;
this->book_code = book_counter++;
}
Book::Book(std::string nm, int arr, int dur, int ppl) {
this->customer_name = nm;
this->arrival = arr;
this->duration = dur;
this->ppl = ppl;
this->room = nullptr;
this->book_code = book_counter++;
}
//Methods
int Book::getArr() {
return arrival;
}
int Book::getDur() {
return duration;
}
int Book::getPpl() {
return ppl;
}
void Book::anathesi(Room* x) {
this->room = x;
}
int Book::getBookCode() {
return book_code;
}
Room* Book::getRoom() {
return room;
}
std::string Book::getCustomerName() {
return customer_name;
}
which includes a string getter method getCustomerName().
When I call this method on main, via an instance created via the first constructor, everything works fine. On the other hand, if the instance is created via the second constructor, the method will cause a segmentation fault.
It seems like customer_name has an infinite length, thus causing a segmentation fault when I try to return it.
The method is called in main with this line of code:
cout << hotel.getBooks(i)->getBookCode() << " " << hotel.getBooks(i)->getCustomerName()
<< " " << hotel.getBooks(i)->getRoom()->getRoomCode() << "\n";
I am new into C++, so please elaborate my fault as much as needed.
The header file for class Book:
#ifndef PROJECT_BOOK_H
#define PROJECT_BOOK_H
#include <string>
class Room;
class Book {
protected:
std::string customer_name;
int book_code;
int arrival;
int duration;
int ppl;
Room* room;
public:
Book();
Book(std::string nm, int arr, int dur, int ppl);
void anathesi(Room* x);
int getArr();
int getDur();
int getPpl();
int getBookCode();
std::string getCustomerName();
Room* getRoom();
};
#endif //PROJECT_BOOK_H
In Hotel.h:
private: std::vector<Book*> books;
public: Book* getBooks(int i);
In Hotel.cpp:
Book* Hotel::getBooks(int i) {
return books[i];
}
Found the problem and fixed it.
I was creating instances with this piece of code:
Book obj(onoma, afiksh - 1, meres, arithmos);
which caused the segmentation I said.
I changed this to:
Book *obj = new Book(onoma, afiksh - 1, meres, arithmos);
and fixed the problem. Thanks for your time and help.
Unfortunately, I don't really know why this works, but it does, everything functions properly. Therefore I guess I'll have to look at new documentation.
I have written a snippet that tries to use an object from a class as a function parameter, and I keep getting the error
In function 'int main()':
75:23: error: expected primary-expression before 'test'
75:35: error: expected primary-expression before 'Knight'
I am not sure how to fix this, as I am quite new to C++.
Some example code is down below:
// Example program
#include <iostream>
#include <string>
using namespace std;
//player class
class Player {
public:
//variable declaration
string name;
string classType;
int strength, perception, endurance, charisma, intelligence, agility, luck;
int id, cubes; // currency etc.
bool authority;
int inventory[20] = {};
int health = 100 + ((strength * endurance) * 2);
//sub stat functions
double getPick() {
return ((intelligence + luck) * (agility / 4)) * .01;
}
double getSneak() {
return (25 + (agility * 5)) * 0.01;
}
double getIntimidation() {
return (charisma * 10) * 0.01;
}
double getBarter() {
return getIntimidation();
}
double getScience() {
return ((intelligence * 5) / 3);
}
};
//enemys
class enemy {
public:
//var declaration
string name;
int HP;
double AC; //armor class ablity to resist hits
int DT; //dice used to attack
int eid; //id for enemys (enemy id)
int gear[2] = {}; //gear
//is the enemy alive?
int alive() {
if (HP <= 0) cout << "\nThe " << name << " is dead! ";
return false;
}
};
//fight an enemy (option 1)
int fightEnemy(Player player1, enemy enemy1) {
cout << "\n" << player1.name << " and a " << enemy1.name << "\n";
return 0;
}
int main() {
//test
Player test;
test.name = "test";
test.classType = "test";
test.strength = 3;
test.perception = 3;
test.endurance = 6;
test.charisma = 2;
test.intelligence = 6;
test.agility = 3;
test.luck = 5;
test.id = 1;
test.authority = true;
test.cubes = 500;
enemy Knight;
Knight.name = "Knight";
Knight.HP = 20;
Knight.AC = 0.2;
Knight.DT = 12;
Knight.eid = 3;
fightEnemy(Player test, enemy Knight);
return 0;
}
fightEnemy(Player test, enemy Knight);
Syntax is wrong here. You just pass the variables to the function, you are essentially declaring them again.
It should be
fightEnemy(test, Knight);
fightEnemy(Player test, enemy Knight);
to
fightEnemy(test, Knight);
The variables are already initialized.
I am making a game that includes orcish enemies. I have one problem, how do initialize their HP, MP, Attack, defense, and speed without writing different code for each like this:
int orcishHP = 50;
int orcishMP = 5;
int orcishAttack = 15;
int orcishDef = 10;
int orcishSpeed = 20;
Isn't there some way to initialize all that when I refer to the orc, like this:
int orcishStats(){
int HP = 50
etc...
}
So instead of calling orcish MP HP and all that stuff, I have all of it in one one place.
If this made sense, please help. If this didn't make sense, don't help.
Here's the correct way to do this:
You define your attributes in a class/struct, and when that struct is initialized, the constructor(OrcishAttributes()) is called:
struct OrcishAttributes
{
int m_orcishHP;
int m_orcishMP;
int m_orcishAttack;
int m_orcishDef;
int m_orcishSpeed;
OrcishAttributes(int orcishHP, int orcishMP, int orcishAttack, int orcishDef, int orcishSpeed)
{
m_orcishHP=orcishHP;
m_orcishMP = orcishMP;
m_orcishAttack = orcishAttack
m_orcishDef = orcishDef;
m_orcishSpeed = orcishSpeed;
}
}
class Orc
{
public:
Orc(OrcishAttributes oa)
{
m_orcAttributes = oa;
}
private:
OrcishAttributes m_orcAttributes;
}
int main()
{
OrcishAttributes oa(50,5,15,10,20);
Orc* o = new Orc(oa);
}
That's why we have OOP.
You can define a base class, lets say Unit. It can store common members like fields or methods:
class Unit
{
protected:
int HP;
int MP;
int Attack;
int Def;
int Speed;
public:
int getHPplusMP()
{
return HP + MP;
}
};
Then, you can create inherited classes and initialize these values in its constructors:
class OrcUnit : public Unit
{
public:
OrcUnit()
{
HP = 20;
MP = 0;
Attack = 13;
Def = 4;
Speed = 5;
}
};
class ElfUnit : public Unit
{
public:
ElfUnit()
{
HP = 25;
MP = 15;
Attack = 15;
Def = 1;
Speed = 8;
}
};
Then, you will be able to work with it:
ElfUnit elf;
OrcUnit orc;
Unit u = elf;
cout << u.getHPplusMP();
Unit u = orc;
cout << u.getHPplusMP();
In general, it is definitely a good idea to learn OOP very well before trying to implement games.
This is for a poker game and I have class PokerTable defined in PokerTable.h
#include <iostream>
using namespace std;
class PokerTable
{
private:
int numPlayers;
int numPlaying;
int dealerPos;
int bigBlind;
int potSize;
int betSize;
bool flop;
bool turn;
bool river;
public:
//constructors
PokerTable();
PokerTable(int,int,int,int,int,bool,bool,bool);
//getters
int getNumPlayers(){return numPlayers;};
int getDealerPos(){return dealerPos;};
int getBigBlind(){return bigBlind;};
int getNumPlaying(){return numPlaying;};
int getPotSize(){return potSize;};
int getBetSize(){return betSize;};
bool getFlop(){return flop;};
bool getTurn(){return turn;};
bool getRiver(){return river;};
//void buttonShow(int);
//setters
void setBetSize(int inBetSize){betSize = inBetSize;};
void setBigBlind(int inBigBlind){bigBlind = inBigBlind;};
void setNumPlaying(int inNumPlaying){numPlaying = inNumPlaying;};
void setPotSize(int inPotSize){potSize = inPotSize;};
void setFlop(bool inFlop){flop = inFlop;};
void setTurn(bool inTurn){turn = inTurn;};
void setRiver(bool inRiver){river = inRiver;};
void setNumPlayers(int inPlayers){numPlayers = inPlayers;};
void setDealerPos(int inDealerPos){dealerPos = inDealerPos;};
};
PokerTable::PokerTable()
{
numPlayers = 9;
numPlaying = 9;
dealerPos = 1;
bigBlind = 20;
flop = false;
turn = false;
river = false;
}
PokerTable::PokerTable(int playerNum, int playingCount, int posDealer, int blindBig,int inPotSize, bool inFlop,bool inTurn,bool inRiver)
{
numPlayers = playerNum;
numPlaying = playingCount;
dealerPos = posDealer;
potSize = inPotSize;
bigBlind = blindBig;
flop = inFlop;
turn = inTurn;
river = inRiver;
}
In my watch list pokerTable.numPlayers has a random value up to 4 million before I even execute this next line of code.
PokerTable aPokerTable(9,9,1,20,30,false,false,false);
and afterwards here is pokerTable in my watch list:
- aPokerTable { numPlayers=2990892 numPlaying=9 dealerPos=9 ...} PokerTable
betSize 30 int
bigBlind 1 int
dealerPos 9 int
flop false bool
numPlayers 2990892 int
numPlaying 9 int
potSize 20 int
river false bool
turn false bool
Can anyone tell me why all the values are not what I declared them to be??!?!!
And how I can fix this?
This is Form1.h
#pragma once
#include "PokerTable.h"
#include "Card.h"
#include <time.h>
#include "PokerPlayer.h"
#include <fstream>
#include <string>
#include <sstream>
//global variables
//TODO make players start from 0
int firstPlayer;
int deck[52];
int nextCard=0;
PokerTable aPokerTable(9,9,1,20,30,false,false,false);
PokerPlayer players[9]; //however many players
ofstream gameLog;
/*
void setTable()
{
aPokerTable.setNumPlayers(9);
aPokerTable.setNumPlaying(9);
aPokerTable.setDealerPos(1);
aPokerTable.setBigBlind(20);
aPokerTable.setPotSize(30);
aPokerTable.setBetSize(20);
aPokerTable.setFlop(false);
aPokerTable.setTurn(false);
aPokerTable.setRiver(false);
}
*/
string convertInt(int number) //convert to string
{
stringstream ss;//create a stringstream
ss << number;//add number to the stream
return ss.str();//return a string with the contents of the stream
}
void createPlayers()
{
// aPokerTable.setNumPlayers(9);
for(int x=0;x<=(aPokerTable.getNumPlayers()-1);x++)
{
players[x] = *(new PokerPlayer(1000,(aPokerTable.getDealerPos())+1,false,0,1));//1000 chips, position i+1, not folded
}
}
void playRound()
{
int action;
for(int playerTurn = firstPlayer; playerTurn <= aPokerTable.getNumPlayers()+firstPlayer; playerTurn++)
{
if(players[playerTurn].getFold() == false)
{
if(aPokerTable.getNumPlaying() == 1)
{
players[playerTurn].setChipStack(players[playerTurn].getChipStack() + aPokerTable.getPotSize()); //player wins pot
}
else //there is more than one person playing
{
action = players[playerTurn].action(); //0 is check/fold, value is call/bet/raise,
if(action > aPokerTable.getBetSize())
{
aPokerTable.setBetSize(action);
aPokerTable.setPotSize(aPokerTable.getPotSize() + action);
playerTurn = playerTurn - aPokerTable.getNumPlayers();
}
else if (action == aPokerTable.getBetSize()) //call
{
aPokerTable.setPotSize(aPokerTable.getPotSize() + action);
}
else //action < aPokerTable.betSize
{
players[playerTurn].setFold(true);
aPokerTable.setNumPlaying(aPokerTable.getNumPlaying()-1); //removes player from playing tally
}
}
}
}
}
void randomDeck()
{
int random_integer;
int tempCard;
//srand((unsigned)time(0));
for(int j=0;j<=51;j++)
{
deck[j] = j;
}
for(int i=51; i>=1; i--)
{
random_integer = rand()%(i); //a random number between 0 and i
tempCard = deck[i];
deck[i] = deck[random_integer]; //put the random card from unshuffled deck into slot i of the deck
deck[random_integer] = tempCard; //put whatever was at slot i into the random slot
}
}
void dealCards()
{
for(int j=1;j<=aPokerTable.getNumPlayers();j++)
{
players[j].setCard1(deck[nextCard]);
nextCard++;
players[j].setCard2(deck[nextCard]);
nextCard++;
}
}
void playPreFlop()
{
aPokerTable.setBetSize(aPokerTable.getBigBlind());
aPokerTable.setFlop(false); //it is before the flop
aPokerTable.setTurn(false);
aPokerTable.setRiver(false);
randomDeck(); //shuffle cards
dealCards();
firstPlayer = (aPokerTable.getDealerPos() + 3)%(aPokerTable.getNumPlayers()); // first player is left of blinds between 0 and numplayers
playRound();
}
void playFlop()
{
aPokerTable.setFlop(true);
firstPlayer = (aPokerTable.getDealerPos())%aPokerTable.getNumPlayers(); // first player is left of dealer between 0 and numplayers
aPokerTable.setBetSize(0);
playRound();
}
void playTurn()
{
aPokerTable.setTurn(true);
firstPlayer = (aPokerTable.getDealerPos())%aPokerTable.getNumPlayers(); // first player is left of dealer between 0 and numplayers
aPokerTable.setBetSize(0);
playRound();
}
void playRiver()
{
aPokerTable.setRiver(true);
firstPlayer = (aPokerTable.getDealerPos())%(aPokerTable.getNumPlayers()); // first player is left of dealer between 0 and numplayers
aPokerTable.setBetSize(0);
playRound();
if(aPokerTable.getNumPlaying() >=2)
{
//showDown();
}
}
/*
void showDown()
{
}
*/
This is pokerPlayer.h
using namespace std;
class PokerPlayer
{
private:
int chipStack,position;
bool fold;
int card1,card2;
public:
//constructors
PokerPlayer();
PokerPlayer(int,int,bool,int,int);
//getters
int getChipStack() {return chipStack;}
int getPosition() {return position;}
int getCard1(){return card1;}
int getCard2(){return card2;}
bool getFold(){return fold;}
//setters
void setChipStack(int inChips){chipStack = inChips;}
void setPosition(int inPos){position = inPos;}
void setCard1(int inCard1){card1 = inCard1;}
void setCard2(int inCard2){card2 = inCard2;}
void setFold(bool inFold){fold = inFold;}
int action();
};
PokerPlayer::PokerPlayer()
{
chipStack = 1000;
position = 0;
fold=false;
card1 = 0;
card2 = 1;
}
PokerPlayer::PokerPlayer(int inChipStack,int inPos, bool inFold, int inCard1, int inCard2)
{
chipStack = inChipStack;
position = inPos;
fold = inFold;
card1 = inCard1;
card2 = inCard2;
}
int PokerPlayer::action()
{
return 0;
}
aPokerTable { numPlayers=2990892 numPlaying=9 dealerPos=9 ...}
Note that dealerPos got assigned the value 9, that's wrong as well. If you look closely, you'll see that everything is shifted by 4 bytes.
Two possible reasons. The debugger could have picked the wrong address for aPokerTable, the actual address minus 4. That's unlikely. Or there's a mismatch between the definition of the PokerTable class as seen by pokertable.cpp and the other .cpp files that #include the pokertable.h include file. Where pokertable.cpp saw an extra member before the numPlayers member. Maybe you edited the header and deleted that member but ended up not recompiling pokertable.cpp for some mysterious reason. Build + Rebuild to fix. Do panic a bit if this actually works.
It's because in C++ before the constructor is called, variable uses the value that it already contains in its memory location that is a "random" value
I cannot reconstruct it because i dont have the full code. However, a random value near 4 million sounds like a pointer. When you store or retrieve a member variable maybe you did not de-reference the pointer. Please post the rest of the code so we can check if that's the case.
players[x] = *(new PokerPlayer(...));
That is a memory leak. What you probably want is:
players[x] = PokerPlayer(1000,(aPokerTable.getDealerPos())+1,false,0,1);