is there a warior i can do the following:

lets say i have

string userChoice;

assume i have a Warrior class with move function in it
is it possible to define :

#define WARRIOR "Warrior"

and than use

userChoice=WARRIOR;

and call the function

userChoice.move()

?

void play(char gameWorld[][BOARD_SIZE])
{
	Warrior War(1,1);
	int userChoice;
	userChoice=getUserChoice();
	string currentPlayer=WARRIOR;

	while(userChoice!='q')
	{
		switch(userChoice)
		{
		case(50): ((string)currentPlayer).moveDirection(DOWN);
		}
		userChoice=getUserChoice();
	}
}

it isnt working thanks

is it possible without using a vector? just a simple string C or something simpler? ty

Recommended Answers

All 4 Replies

I'm not sure I entirely understand your question.

You would like to define a string constant, correct? const char *WARRIOR = "Warrior"; -or- const string WARRIOR = "Warrior";

void play(char gameWorld[][BOARD_SIZE])
{
	Warrior war(1,1);
	int userChoice = getUserChoice();
	string currentPlayer = WARRIOR;

	while(userChoice != 'q')
	{
		switch(userChoice)//<--
		{
		case(50): //<-- what?
((string)currentPlayer).moveDirection(DOWN);
		}
		userChoice=getUserChoice();
	}
}

The "switch" is a cleverly disguised specialized if/elseif, and it's fairly primitive. You might find it easier to use if/elseif/else.

lets make it simpler .
lets say i would like to call a class function .
i defined a warrior class :

Warrior newWarr

the class function is :

Warrior::Move();

the same for MAGICIAN
is there any operand i can use that if i save WARRIOR in it or MAGICIAN in it
i could do the following:
oper.Move();
and if oper is warrior it will go to the warrior move function
same if it held the MAGICIAN name

hope its clearer, just wanted to make things more short and less crowded
thanks for trying to understand

Yes you can:

1)
Write the "move" function so that it operates properly using the "WARRIOR" information.
ex:

const std::string WARRIOR = "Warrior";

class Character
{
public:
std::string Type;
void move()
{
   if(this->Type == WARRIOR)
     ;//move
}
};

2)
Redesign using polymorphism with a base class and a Warrior derived from it.
ex:

class CharacterBase
{
public:
virtual void move() = 0;//Pure virtual
};

class Warrior : public CharacterBase
{
public:
virtual void move()
{
   //Move your warrior.
}
};

void MoveCharacter(CharacterBase *myChar)
{
   myChar->move();
}

int main()
{
  Warrior myKnight;
  MoveCharacter(&myKnight);
}

3)
Use a function pointer or functor.
ex)

typedef void (*MoveFunc)(void);// named type for my func. pointer

void MoveWarrior()
{
   //etc.
}

class Warrior //redundant, etc.
{
  MoveFunc moveFunction;
  public:
    void move()
    {
      moveFunction = MoveWarrior;
      moveFunction();
    }
};

Please excuse minor bugs.
Also, there are a large number of other strange things you can do with C++ so you're pretty well-off on finding a way to do it.

abit advanced for me cuz we arent allowed to use some of the things you just said
but i managed to do it in a more LESS efficient way as needed :)
thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.