Basically, I have 3 functions
The first and second functions is to check whether ignition is true or false.
The third function basically is to check if ignition is on, the speed cannot be greater than 65, if the speed is greater than 65, it will "Fix" that speed at 65.
Whereas, if the ignition is turn off, the speed will be 0.
However,
in my code, I did a if else statement.
When I print the part where ignition is turn off,
the value I got is 65. It suppose to be 0.
May I know what's wrong with my code?
car.h
#ifndef car_inc_h
#define car_inc_h
#include <iostream>
#include <string>
using namespace std;
class Car {
bool isIgnitionOn;
int speed;
public:
void turnIgnitionOn();
void turnIgnitionOff();
void setSpeed(int);
void showCar();
};
#endif
car.cpp
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
void Car::turnIgnitionOn() {
this->isIgnitionOn = true;
}
void Car::turnIgnitionOff() {
this->isIgnitionOn = false;
};
void Car::setSpeed(int speed) {
if (isIgnitionOn == true) {
if (speed >= 65) {
this->speed = 65;
}
else {
this->speed = speed;
}
}
else if (isIgnitionOn == false){
this->speed = 0;
}
};
void Car::showCar() {
if (isIgnitionOn == true) {
cout << "Ignition is on." << endl;
cout << "Speed is " << speed << endl;
}
else if (isIgnitionOn == false) {
cout << "Ignition is off" << endl;
cout << "Speed is " << speed << endl;
}
};
main.cpp
#include <iostream>
#include <string>
#include "Car.h"
using namespace std;
int main() {
Car myCar;
myCar.turnIgnitionOn();
myCar.setSpeed(35);
myCar.showCar();
myCar.setSpeed(70);
myCar.showCar();
myCar.turnIgnitionOff();
myCar.showCar();
return 0;
}
speed is never reset to 0. You can add this->speed=0 in turnIgnitionOff which is more logical afterall.
Related
So I'm unable to create a substring cut using ranges. I am making an airport program where you feed the program a txt.file and it has to divide the lines I get from it into different strings. For instance, I have the following text data:
CL903 LONDON 41000 14.35 08906 //number of flight, destination, price, etc.
UQ5723 SYDNEY 53090 23.20 12986
IC5984 TORONTO 18030 04.45 03260
AM608 TOKYO 41070 18.45 11315
so the first string will be on the lines of this (variables are in Spanish):
numVuelo[n] = M[n].substr(0,5)
this line will work perfectly, but when I move to the next one (from 7 to 14), it tells me that it's out of range, even though It's between the 0 and 31st values of the length of the string.
M[n] gets all of the strings on the text, I'm using Codeblocks and a class style with header and all. I'll copy the code below...
This is my header Vuelo.h:
#ifndef VUELO_H
#define VUELO_H
#include <iostream>
#include <fstream>
#include <string>
#define NUM_FLIGHTS 10
using namespace std;
class Vuelo
{
public:
Vuelo(int N);
virtual ~Vuelo();
void setM();
void setNumVuelo(string _numVuelo, int n);
void setDestino(string _destino, int n);
void setPrecio(string _precio, int n);
private:
string M[NUM_FLIGHTS];
string numVuelo[NUM_FLIGHTS];
string destino[NUM_FLIGHTS+1]; //somehow "destino" doesn't work without the +1 but everything else does
float precio[NUM_FLIGHTS];
Then, on another code called Vuelo.cpp I have the following
#include "Vuelo.h"
Vuelo::Vuelo(int N)
{
M[N] = { };
numVuelo[N] = { };
destino[N] = { };
precio[N] = { };
}
Vuelo::~Vuelo()
{
//nope
}
void Vuelo::setM()
{
int c = 1;
string s;
ifstream F ("flights.txt");
if(F.is_open())
{
while (!F.eof())
{
getline(F,s);
M[c] = s;
cout << M[c] << endl;
c++;
}
//sets all values
for(c = 0; c < NUM_FLIGHTS; c++)
{
setNumVuelo(M[c],c);
setDestino(M[c],c);
setPrecio(M[c],c);
}
F.close();
}
else
{
cout << "ERROR document wasn't found" << endl;
}
}
void Vuelo::setNumVuelo(string _numVuelo, int n)
{
numVuelo[n]= _numVuelo.substr(0,5); //this works
cout << numVuelo[n] <<endl;
}
void Vuelo::setDestino(string _destino, int n)
{
destino[n] = _destino.substr(7, 13); //PROBLEM HERE
cout << destino[n] << " " << destino[n].length() << endl;
}
void Vuelo::setPrecio(string _precio, int n)
{
string p = _precio.substr(15,19); //PROBLEM HERE
precio[n] = atof(p.c_str());
cout << precio[n] <<endl;
}
And finally my main looks like this:
#include "Vuelo.h"
#include <iostream>
#include <fstream>
#include <string>
#define NUM_FLIGHTS 10
using namespace std;
int main()
{
cout << "Bienvenido, reserva tu vuelo!" << endl;
cout << "-----------------------------------" << endl;
Vuelo* flight = new Vuelo(NUM_FLIGHTS);
flight->setM();
return 0;
}
Thanks :)
I have 3 classes, GameObject, Building which inherits from GameObject, and PokemonCenter which inherits from Building. When I try to call a Building::ShowStatus() function in PokemonCenter I keep get a "invalid operands to binary expression" error.
Building.h
#ifndef BUILDING_H
#define BUILDING_H
#include "Point2D.h"
#include "GameObject.h"
class Building : public GameObject
{
private:
unsigned int pokemon_count;
public:
Building();
Building(char,int, Point2D);
void AddOnePokemon();
void RemoveOnePokemon();
void ShowStatus();
bool ShouldBeVisible();
};
#endif
Building.cpp
#include "Building.h"
#include "GameObject.h"
#include <iostream>
using namespace std;
Building::Building()
{
display_code = 'B';
location;
id_num = ' ';
state = '0';
pokemon_count = 0;
cout << "Building default constructed";
}
Building::Building(char in_code,int in_id, Point2D in_loc)
{
id_num = in_id;
location = in_loc;
display_code = in_code;
state = '0';
pokemon_count = 0;
cout << "Building constructed";
}
void Building::ShowStatus()
{
cout << "\"(" << pokemon_count << "\"pokemon is/are in this building";
}
PokemonCenter.h
#ifndef POKEMONCENTER_H
#define POKEMONCENTER_H
#include "Point2D.h"
#include "Building.h"
class PokemonCenter: public Building
{
private:
unsigned int stamina_capacity;
unsigned int num_stamina_points_remaining;
double dollar_cost_per_stamina_point;
PokemonCenter();
PokemonCenter(int,double,unsigned int, Point2D);
public:
bool HasStaminaPoints();
unsigned int GetNumStaminaPointsRemaining();
bool CanAffordStaminaPoints(unsigned int, Point2D);
double GetDollarCost(unsigned int);
unsigned int DistributeStamina(unsigned int);
bool Update();
void ShowStatus();
};
enum PokemonCenterStates
{
STAMINA_POINTS_AVAILABLE = 0,
NO_STAMINA_POINTS_AVAILABLE = 1
};
#endif
PokemonCenter.cpp
#include "PokemonCenter.h"
#include "Point2D.h"
#include "Building.h"
#include "GameObject.h"
#include <iostream>
using namespace std;
PokemonCenter::PokemonCenter()
{
id_num = ' ';
location;
display_code = 'C';
stamina_capacity = 100;
num_stamina_points_remaining = stamina_capacity;
dollar_cost_per_stamina_point = 5;
state = STAMINA_POINTS_AVAILABLE;
cout << "PokemonCenter default constructed";
}
PokemonCenter::PokemonCenter(int in_id, double stamina_cost, unsigned int stamina_cap, Point2D in_loc)
{
id_num = in_id;
location = in_loc;
dollar_cost_per_stamina_point = stamina_cost;
stamina_capacity = stamina_cap;
num_stamina_points_remaining = stamina_capacity;
state = STAMINA_POINTS_AVAILABLE;
cout << "PokemonCenter constructed";
}
void PokemonCenter::ShowStatus()
{
cout << "Pokemon Center Status: " << Building::ShowStatus() << endl;
}
The problem is that the return type of Building::ShowStatus() is void. operator<< is not defined between std::ostream and void. Hence, you can't use
cout << "Pokemon Center Status: " << Building::ShowStatus() << endl;
Change that to:
cout << "Pokemon Center Status: ";
Building::ShowStatus();
cout << endl;
I'm working on C++, and this is just a very basic program, but I'm still getting an error.
The error message is:
'class secondary' has no member named 'getting'.
Why is this? It works for my void setting, but not for getting? What am I doing wrong here?
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
string getting();
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if(factor < 3){
result = "You have a very low self esteem.";
}elseif(factor > 3){
if(factor > 7){
result = "You have a very high self esteem."
}else{
result = "You have a medium self esteem."
}
}
return result;
}
private factor;
Actually, looking at this again, and deeper, this code has many issues (semicolons missing at key points and the private int definition should have been in the header file, not the cpp file 9t(private is its own section, see below):The problem, from what I can see, s has not yet been instantiated yet, do so and the operation should work correctly.
Please also note that when factor was defined in the cpp file, it was defined at bottom, it should actually be defined before any use of the variable to be defined (in the header file is better meet with common/conventional coding standards).
Please check this tested code:
main.cpp
#include <iostream>
#include <string>
#include "secondary.h"
using namespace std;
int main(){
secondary s;
int scale;
cout << "On a scale of 1-10, how awesome are you?" << endl;
cin >> scale;
cout << endl;
s.setting(scale);
cout << s.getting();
return 0;
}
secondary.h
#ifndef SECONDARY_H
#define SECONDARY_H
#include <string>
class secondary
{
public:
void setting(int x);
std::string getting();
private: // Note: this is how you do private
int factor; // This is the definition with type int, missing in original
};
#endif // SECONDARY_H
secondary.cpp
#include "secondary.h"
#include <iostream>
#include <string>
using namespace std;
void secondary::setting(int x){
factor = x;
}
string secondary::getting(){
string result;
if (factor < 3){
result = "You have a very low self esteem.";
}else if(factor > 3){
if (factor > 7){
result = "You have a very high self esteem.";
}
else{
result = "You have a medium self esteem.";
}
}
return result;
}
First Issue Resolved
As voiced in the answers below , i incorrectly wrote the
#ifndef ICETOWER_H
#define ICETOWER_H
I'm still having some problem with my c++ code.
I implemented a decorator pattern to upgrade a basic tower to an ice tower with 2* the cost. but when i run it , its displaying the same specs for both towers even after it has been decorated . Anyone have any idea what i did wrong ?
Here are the files :
tower.h
#ifndef __TOWER_H__
#define __TOWER_H__
#include <iostream>
#include <string>
using namespace std;
class Tower {
private:
string type;
string effect;
int cost;
int sellTower;
int damage;
int range;
int rate;
public:
string getType() { return type; }
string getEffect() { return effect; }
int getCost() { return cost; }
int getSale() { return sellTower; }
int getDamage() { return damage; }
int getRange() { return range; }
int getROF() { return rate; }
Tower();
virtual ~Tower(){}
};
#endif __TOWER_H__
tower.cpp
#include "Tower.h"
Tower::Tower()
{
// Tower Type
this->type = "Basic";
// Tower Special Effect
this->effect = "None";
// Tower Cost
this->cost = 500;
// Tower Sell Cost
this->sellTower = 300;
// Tower Damage inflicted
this->damage = 50;
// Tower Range (paths)
this->range = 2;
// Tower rate of fire
this->rate = 0.5;
};
TowerDecorator.h
#ifndef __TOWERDECORATOR_H__
#define __TOWERDECORATOR_H__
#include <iostream>
#include <string>
#include "Tower.h"
using namespace std;
class TowerDecorator : public Tower {
private:
Tower *decoratedTower;
public:
TowerDecorator(Tower *decoratedTower)
{
this->decoratedTower = decoratedTower;
}
string type() { return decoratedTower->getType(); }
string effect() { return decoratedTower->getEffect(); }
int getCost() { return decoratedTower->getCost(); }
int getSale() { return decoratedTower->getSale(); }
int getDamage() { return decoratedTower->getDamage(); }
int getRange() { return decoratedTower->getRange(); }
int getROF() { return decoratedTower->getROF(); }
};
#endif __TOWERDECORATOR_H__
iceTower.h
#ifndef ICETOWER_H
#define ICETOWER_H
#include "TowerDecorator.h"
class IceTower : public TowerDecorator {
public:
IceTower(Tower *decoratedTower) : TowerDecorator (decoratedTower){}
int getCost(){return TowerDecorator::getCost() * 2;}
};
#endif __ICETOWER_H__
Driver.cpp
#include "Tower.h"
#include "TowerDecorator.h"
#include "IceTower.h"
void printTowerDetails(Tower* tower)
{
cout << endl << "This is a " << tower->getType() << " Tower" << endl;
cout << "Build Tower : " << tower->getCost() << " Coins" << endl;
cout << "Sell Tower : " << tower->getSale() << " Coins" << endl;
cout << "Tower Range : " << tower->getRange() << " paths" << endl;
cout << "Tower Rate Of Fire : " << tower->getROF() << " p/s" << endl;
cout << "Tower Special Effect : " << tower->getEffect() << "" << endl;
}
int main() {
Tower *t1 = new Tower();
printTowerDetails(t1);
t1 = new IceTower(t1);
printTowerDetails(t1);
}
Output :
Image : http://i.imgur.com/Ws018iV.png
There are at least two serious errors.
The first one is that you check definitions of manifest constants with suffix _H__ but define them without the suffix:
#ifndef __TOWER_H__
#define __TOWER__
and
#ifndef __TOWERDECORATOR_H__
#define __TOWERDECORATOR__
The second one is that you forgot to place a semicolon after class definition of TowerDecorator
class TowerDecorator : public Tower {
//...
}
Well, the first problem that you have is a problem with your include guard. It should be:
#ifndef __TOWER_H__
#define __TOWER_H__
You might have other problems as well.
BTW, you shouldn't use names that begin with two underscores, since they are reserved.
Your header gaurds are inconsistent:-
#ifndef __TOWER_H__
#define __TOWER__
This should be
#ifndef __TOWER_H__
#define __TOWER_H__
Secondly change this
#endif __TOWER_H__
to
#endif
I know this problem is assessed many times on these forums, but they really are unique to their specific cases most times.
This is a project for a class (on C++ no less), and the point of the project was to remake the classic board game Reversi.
I have toiled through code for hours and finally made a program that will work, or so I thought!
The big problem I am having seems to come from my deconstructor as it's giving me this error many of us have seen. My code is posted below and from my own debugging code (using helpful cout messages) I have determined that the program manages to run to the end of the the Game.cpp class. Only, it stumbles on the deconstructor and crashes before finally "ending nicely".
Board.h
#include <iostream>
#include <cstdlib>
#include <vector>
#ifndef BOARD_H
#define BOARD_H
using namespace std;
enum Piece {LIGHT, DARK, EMPTY, BORDER};
typedef int Move;
Move const NullMove = -1;
int const MAX_SQUARES = 100;
enum Direction {N=0, NE=1, E=2, SE=3, S=4, SW=5, W=6, NW=7};
class Board
{
public:
Board();
void reset();
void display();
void makeMove(Piece, Move);
bool isLegal(Piece, Move);
Piece getWinner();
Piece getPlayer();
void genMoves();
int numMoves();
Move getMove(int) const;
bool gameOver;
private:
Piece board[MAX_SQUARES];
int lightPieces;
int darkPieces;
vector<Move> goodMoves;
static Piece currentPlayer;
vector <int> offset;
};
#endif
Board.cpp
#include <iostream>
#include <cstdlib>
#include <vector>
#include "Board.h"
using namespace std;
Board::Board()
{
reset();
for(int i=0;i<MAX_SQUARES;++i)
{
if(i<11 || i>88 || i%10==0 || i%10==9)
board[i]=BORDER;
}
offset.push_back(10);
offset.push_back(11);
offset.push_back(1);
offset.push_back(-9);
offset.push_back(-10);
offset.push_back(-11);
offset.push_back(-1);
offset.push_back(9);
board[44] = LIGHT;
board[45] = DARK;
board[54] = DARK;
board[55] = LIGHT;
gameOver=false;
}
void Board::reset()
{
for(int i=0; i<MAX_SQUARES;++i)
board[i] = EMPTY;
}
void Board::display()
{
for(int i=0;i<MAX_SQUARES;++i)
{
switch(board[i])
{
case LIGHT:
cout << "|LG|";
break;
case DARK:
cout << "|DR|";
break;
case EMPTY:
cout << "| |";
break;
case BORDER:
if(i<9)
cout << "<0" << i << ">";
else if(i==9)
cout << "<09>\n----------------------------------------\n";
else if(i%10==9)
cout << "<$$>\n----------------------------------------\n";
else if(i%10==0)
cout << "<" << i << ">";
else if(i<11 || i>90)
cout << "<$$>";
break;
}
}
}
void Board::makeMove(Piece p, Move m)
{
genMoves(); //generate valid moves
cout << "generated moves\n";
int good = numMoves(); //gets number of moves
if(good>0) //if there are valid moves
{
cout << "more than 0\n";
for(int i=0;i<goodMoves.size();++i) //checking the valid move list
{
if(m==goodMoves[i]) //if our move is in the list
{
cout << "move was in list\n";
board[m]=p; //change square
if(board[m]==DARK)
cout << "ITS DARK\n";
else if(board[m]==LIGHT)
cout << "ITS LIGHT\n";
else if(board[m]==EMPTY)
cout << "ITS EMPTY WTF WTF WTF\n";
for(int i=0;i<8;++i) //checking directions
{
Piece opp =(p==LIGHT)? DARK : LIGHT; //Making an opposite piece
cout << "made opp\n";
int counter=0;
int toCheck = m+offset[i]; //making the next square to check
if(board[toCheck]==opp) //if it's the opposite colour from player
{
cout << "it was the opposite piece\n";
while(board[toCheck]!=BORDER && board[toCheck]!=EMPTY) //while it's a piece
{
cout << "there was a piece to check\n";
if(board[toCheck]==p && counter>0) //if it's player's piece and counter is higher than 0
{
cout << "this should flip stuff\n";
for(int k=m;k!=toCheck;k = k+offset[i])
{
board[k]=p;
cout << k;
}
break;
}
else
{
cout << "found nothing, keep trying..\n";
toCheck += offset[i]; //if not, step in direction
counter++;
}
}
}
}
}
cout << "move wasn't in list\n";
}
}
currentPlayer=(p==LIGHT)? DARK : LIGHT;
}
bool Board::isLegal(Piece p, Move m)
{
Piece opp =(p==LIGHT)? DARK : LIGHT; //Making an opposite piece
if(board[m]==EMPTY) //Checking that the space we're going is empty
{
for(int i=0;i<8;++i) //checking directions
{
int toCheck = m+offset[i]; //making the next square to check
if(board[toCheck]==opp) //if it's the opposite colour from player
{
while(board[toCheck]!=BORDER && board[toCheck]!=EMPTY) //while it's a piece
{
if(board[toCheck]==p) //if it's player's piece
return true; // if move is valid
else
toCheck += offset[i]; //if not, step in direction
}
}
}
return false; // if there's no valid direction moves
}
else // if it's not empty
return false;
}
Piece Board::getWinner()
{
bool gameDone = true;
for(int i=0;i<MAX_SQUARES;++i)
{
if(board[i]==EMPTY)
gameDone = false;
}
if(gameDone==false)
return EMPTY;
else if(lightPieces>darkPieces)
return LIGHT;
else
return DARK;
}
Piece Board::getPlayer()
{
return currentPlayer;
}
void Board::genMoves()
{
goodMoves.clear();
cout << "generating shit\n";
for(int i=0;i<MAX_SQUARES;++i)
{
if(isLegal(currentPlayer, i))
{goodMoves.push_back(i);
cout << i << " twas a good move\n";}
}
if(goodMoves.size()==0)
gameOver=true;
}
int Board::numMoves()
{
return goodMoves.size();
}
Move Board::getMove(int i) const
{
return goodMoves[i];
}
Piece Board::currentPlayer=DARK;
Player.h
#include <iostream>
#include <cstdlib>
#ifndef PLAYER_H
#define PLAYER_H
#include "Board.h"
using namespace std;
class Player
{
public:
Player(const string&, Piece);
Piece getPiece() const;
virtual void makeMove(Board&)=0;
void setName(string&);
string getName();
private:
string name;
Piece color;
};
#endif
Player.cpp
#include <iostream>
#include <cstdlib>
#include "Player.h"
using namespace std;
Player::Player(const string& n, Piece c)
{
name = n;
color = c;
}
Piece Player::getPiece() const
{
return color;
}
void Player::setName(string& n)
{
name = n;
}
string Player::getName()
{
return name;
}
HumanPlayer.h
#include <iostream>
#include <cstdlib>
#include "Player.h"
#ifndef HUMANPLAYER_H
#define HUMANPLAYER_H
using namespace std;
class HumanPlayer: public Player
{
public:
HumanPlayer(const string&, Piece);
void makeMove(Board&);
};
#endif
HumanPlayer.cpp
#include <iostream>
#include <cstdlib>
#include "Player.h"
#include "HumanPlayer.h"
using namespace std;
HumanPlayer::HumanPlayer(const string& n, Piece c): Player(n,c)
{
}
void HumanPlayer::makeMove(Board& b)
{
Move goTo;
cout << "Please enter the number for the square you would like to move: ";
cin >> goTo;
if(!b.gameOver)
b.makeMove(getPiece(),goTo);
}
ComputerPlayer.h
#include <iostream>
#include <cstdlib>
#include "Player.h"
#ifndef COMPUTERPLAYER_H
#define COMPUTERPLAYER_H
using namespace std;
class ComputerPlayer: public Player
{
public:
ComputerPlayer(Piece p);
private:
static int counter;
//string name;
};
#endif
ComputerPlayer.cpp
#include <iostream>
#include <cstdlib>
#include "ComputerPlayer.h"
using namespace std;
ComputerPlayer::ComputerPlayer(Piece p) : Player("", p)
{
string name = "ComputerPlayer" + char(65+counter);
setName(name);
}
int ComputerPlayer::counter=0;
RandomPlayer.h
#include <iostream>
#include <cstdlib>
#include "ComputerPlayer.h"
#ifndef RANDOMPLAYER_H
#define RANDOMPLAYER_H
using namespace std;
class RandomPlayer : public ComputerPlayer
{
public:
RandomPlayer(Piece);
void makeMove(Board&);
};
#endif
RandomPlayer.cpp
#include <iostream>
#include <cstdlib>
#include "RandomPlayer.h"
using namespace std;
RandomPlayer::RandomPlayer(Piece p) : ComputerPlayer(p)
{
}
void RandomPlayer::makeMove(Board& b)
{
cout << "Player 2 making move in stuff\n";
b.genMoves();
int temp1 = b.numMoves();
cout << "This is temp1: " <<temp1 << '\n';
int temp2;
if(temp1>0)
{
temp2 = rand()%temp1;
}
//cout << "This is temp2: " <<temp2 << '\n';
if(!b.gameOver)
b.makeMove(getPiece(),b.getMove(temp2));
}
Game.h
// Name: James St-Germain
// Student #: 0270250
#include <iostream>
#include <cstdlib>
#include "Board.h";
#include "HumanPlayer.h"
#include "RandomPlayer.h"
#ifndef GAME_H
#define GAME_H
using namespace std;
class Game
{
public:
Game();
~Game();
void selectPlayers();
Player* nextPlayer();
void play();
void announceWinner();
private:
Board b;
Player *p1;
Player *p2;
bool isRunning;
};
#endif
Game.cpp
// Name: James St-Germain
// Student #: 0270250
#include "Game.h"
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
Game::Game(): b(), p1(NULL), p2(NULL), isRunning(true){}
Game::~Game()
{
delete &b;
delete &p1;
delete &p2;
}
void Game::selectPlayers()
{
string choice[2];
cout << "Is player 1 a human player or computer player? (H/C): \n";
cin >> choice[0];
cout << "Is player 2 a human player or computer player? (H/C): \n";
cin >> choice[1];
for(int i=0;i<2;++i)
{
if(choice[i]=="H")
{
string n;
char* c;
cout << "What is your name?: \n";
cin >> n;
if(i==0)
p1 = new HumanPlayer(n, LIGHT);
else
p2 = new HumanPlayer(n, DARK);
}
if(choice[i]=="C")
{
if(i==0)
p1 = new RandomPlayer(LIGHT);
else
p2 = new RandomPlayer(DARK);
}
}
cout << "Player 1 is " << p1->getName() << '\n';
cout << "Player 2 is " << p2->getName() << '\n';
}
Player* Game::nextPlayer()
{
if(b.getPlayer()==LIGHT)
return p2;
else
return p1;
}
void Game::play()
{
while(isRunning)
{
b.display();
Piece temp = b.getPlayer();
if(temp==LIGHT)
{
cout << "Player 1 moves!\n";
p1->makeMove(b);
}
else
{
cout << "Player 2 moves!\n";
p2->makeMove(b);
}
if(b.gameOver==true)
break;
}
}
void Game::announceWinner()
{
Piece winner = b.getWinner();
string name = (winner==LIGHT) ? p1->getName() : p2->getName();
cout << "The winner is " << name << "! Congratulations!\n";
}
main.cpp
#include <iostream>
#include <cstdlib>
#include "Game.h"
using namespace std;
int main()
{
Game Reversi = Game();
Reversi.selectPlayers();
Reversi.play();
Reversi.announceWinner();
}
I apologize for the extreme amount of code, but at this point, I don't know what to fix. I know there might also be bad coding habits here, so if you see any, I would love the constructive criticism.
Thank you in advance for all your help!!
This is probably because you have these declarations in the Game class:
Board b;
Player *p1;
Player *p2;
and this code in the destructor:
delete &b;
delete &p1;
delete &p2;
First of all, the Board member b is not a pointer so should not be deleted. Second of all, you're using the address-of operator to get the address of the pointer (and it will be a value of type Player**), which you don't allocate. Drop the &.