![]() |
|
VC++ Game Class problem - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Game Programming Fundamentals (/forum-7.html) +--- Thread: VC++ Game Class problem (/thread-7189.html) |
VC++ Game Class problem - Martial_Law - Mar 29, 2003 05:10 PM Hi everyone, I am creating a chess game. I have a game class that contains a game tree two players (can be human or computer) what I am trying to do is: In the constructor of for game, I want to call the constructor for PlayerA and PlayerB telling it if it is a human or computer player. I am having problems with the constructor creating the players Any help would be greatly appreciated ***Code Snippet*** Player Class //abstract class { .... public: virtual bool Play(int Colour, GameTree* MyGameTree) = 0; //pure virtual function } Class Human : public Player //sub-class of player { ..... public: bool Play(int Colour, GameTree* MyGameTree); } Class Computer : public Player //sub-class of player { ..... public: bool Play(int Colour, GameTree* MyGameTree); } Class Game { private: Player PlayerA Player PlayerB public: Game::Game(); //default constructor Game::Game(bool PlayerTypeA, bool PlayerTypeB); //constructor for creating } Game::Game(bool PlayerTypeA, bool PlayerTypeB) { ....... if (PlayerTypeA) //if human player Player1 = Human(); //create human player else Player1 = Computer(); //create computer player if (PlayerTypeB) //if human player Player2 = Human(); //create human player else player2 = Computer(); //create computer player ....... } void Main(void) {... Game MyGame(true, false); //create a game where playerA = human, playerB = computer ... } ***end snippet*** is this how you would create two players inside a class or how would one do it? I can get it to work if I do it like this: Class Game { Human Player1; Computer Player2; } void Main(void) { Game MyGame(true, false); //create a game where playerA = human, playerB = computer ... } this will work, but I don't want to hard code whether the players are human or computer. Thanks for any help Martial Law VC++ Game Class problem - Patrick - Mar 29, 2003 05:33 PM be careful, this probably incorrect ( I think... ): [sourcecode] class A { public: virtual int f(int) = 0; } class B : public A { public: int f(int); // should be: "virtual int f(int);" } [/sourcecode] declaring a pure virtual makes no sense if you then never add the virtual function to define it, heh. And this wouldn't work right: [sourcecode] int main( void ) { A* foobar = new B; foobar->f(0); } [/sourcecode] this should work: [sourcecode] // this is a bit better than passing bools, imo ![]() typedef int PlayerType; enum { Human = 0, Computer }; class Player { ... public: virtual bool Play( int Colour, GameTree* MyGameTree ) = 0; }; class Human : public Player { ... public: virtual bool Play( int Colour, GameTree* MyGameTree ); }; class Computer : public Player { ... public: virtual bool Play( int Colour, GameTree* MyGameTree ); }; class Game { private: Player* a; Player* b; public: Game(); Game(PlayerType A, PlayerType B); int Run( void ); }; Game::Game(PlayerType A, PlayerType B) { if( A == Human ) a = new Human; else a = new Computer; if( B == Human ) a = new Human; else b = new Computer; } int Game::Run( void ) { ... } int main( void ) { try { Game* MyGame = new Game( Human, Computer ); } catch (std::bad_alloc) { return -1; } return MyGame->Run(); } [/sourcecode] VC++ Game Class problem - Mars_999 - Mar 29, 2003 07:21 PM Actually you don't have to use the keyword virtual everytime you redefine the function after you declare the function virtual and that goes for pure virtual functions also. It's a matter of style I guess and their is nothing wrong with it. I myself use the keyword virtual on all my virtual functions to just to remind myself they are!!
VC++ Game Class problem - Patrick - Mar 29, 2003 07:47 PM you're right. never seen that before, heh.
VC++ Game Class problem - Martial_Law - Mar 30, 2003 07:01 AM Cheers guys Yeah I declared the virtual function in human.cpp and computer.cpp sorry for the confusion. Yeah I suppose I should call them virtual in human and computer classes,... By changing the game declaration to class Game { ... Player* A; Player* B; ... }; and passing in the enumerated data type "player type" it worked like a charm I spent about 3 hours yesterday looking for any information about it...its a bit relief. Thanks again, Martial |