I have a program that checks me some tokens, the first time it checks the one with the least HP, the other times it takes one randomly. I thought about transforming this code using the strategy pattern. The code works, but there is a problem: the tokens are cleared
Token
class Token{
private:
string name;
int hp;
public:
Token(string N, int H){
name=N;
hp=H;
}
string GetName(){
return name;
}
int GetHp(){
return hp;
}
}
Strategy.h
#ifndef MAIN_CPP_STRATEGY_H
#define MAIN_CPP_STRATEGY_H
#include "Token.h"
class EnemyStrategy{
protected:
std::unique_ptr<Token> token[3];
public:
virtual int Start() = 0;
};
class FirstCase : public EnemyStrategy{
public:
FirstCase(std::unique_ptr<Token> pieces[3]){
for(int i=0; i<3; i ){
token[i] = std::move(pieces[i]);
std::cout<<token[i]->GetName()<<"\n";
}
}
~FirstAttack(){}
int Start() override{
int min=100;
int attacked;
for (int i = 0; i < 3; i ) {
if (token[i]->GetHp() <= min){
min = token[i]->GetHp();
attacked = i;
}
}
return attacked;
}
};
class SecondCase: public EnemyStrategy{
public:
int pawns;
SecondCase(std::unique_ptr<Token> pieces[3]){
for(int i=0; i<3; i )
token[i] = std::move(pieces[i]);
}
~SecondCase(){}
int Start() override {
int i = rand() % 3;
return i;
}
};
//Controller
class Controller{
EnemyStrategy* strategy;
public:
Controller(EnemyStrategy* tm): strategy(tm){
}
~Controller(){
}
int WhoToAttack(){
return strategy->Start();
}
};
#endif //MAIN_CPP_STRATEGY_H
MainCode
#include "Strategy.h"
#include "Token.h"
int main(){
unique_ptr<Token> token[3];
token[0] = make_unique<Token>("Knight", 20);
token[1] = make_unique<Token>("Mage", 5);
token[2] = make_unique<Token>("Fighter", 10);
Controller* controller;
EnemyStrategy* strategy;
if (control == 0) {
strategy = new FirstAttack(std::move(token));
} else {
strategy = new WarStrategy(std::move(token));
}
controller = new Controller(strategy);
attacked = controller->WhoToAttack();
cout<< "\n the token:" <<attacked; //WORKS
cout<<token[attacked]->GetName(); //DON'T WORKS
delete strategy;
delete controller;
}
My fear is that pointers go out of scope once called in the function, and for this the Strategy pattern works, but the program crashes at: token[attacked]->GetName()
I don't know how to do it, ideas?
At first I left the unique_ptr smart pointers but then I tried to replace them with shared_ptr, but nothing has changed
CodePudding user response:
Here:
if (control == 0) {
strategy = new FirstAttack(std::move(token));
} else {
strategy = new WarStrategy(std::move(token));
}
std::move
doesn't do anything, because arrays are passed by pointer.
The move occurs in the strategy constructor for each element:
FirstCase(std::unique_ptr<Token> pieces[3]){
for(int i=0; i<3; i ){
token[i] = std::move(pieces[i]);
std::cout<<token[i]->GetName()<<"\n";
}
}
After that, you can't use the tokens in main
.
This also happens when using shared_ptr
, but shared_ptr
is copyable and you are not forced to use move
. To use shared_ptr
s, remove the move from the strategy constructor:
FirstCase(std::shared_ptr<Token> pieces[3]){
for(int i=0; i<3; i ){
token[i] = pieces[i];
std::cout<<token[i]->GetName()<<"\n";
}
}