[Kde-games-devel] Re: Kplayer and game logic

Andreas Beckermann b_mann at gmx.de
Mon Sep 1 05:41:28 CEST 2003


On Monday 01 September 2003 03:57, cantabile wrote:
> Hi, Andreas
>
> Here are the lines where I create and instantiate the players.
>
> void KMatchesDoc::initPlayers()
> {
>         MatchesPlayer *player;
>         player=(MatchesPlayer *)createPlayer(1, KGameIO::MouseIO,
> false);
>         player->setUserId(1);
>         player->setName(i18n("You"));
>         addPlayer(player);
>
>         ComputerPlayer *computer;
>         computer=(ComputerPlayer *)createPlayer(2, KGameIO::ComputerIO,
> false);

Isn't ComputerPlayer a KGameIO? Then this won't work, as createPlayer() 
returns a KPlayer*. Replace these lines by 
KPlayer* computer = createPlayer(2, KGameIO::ComputerIO);

But I believe you don't need the rtti of createPlayer() anyway, sou you could 
use any random number instead of 2, but this depends on your implementation. 

>         player->setUserId(2);
>         player->setName(i18n("Computer"));
>         addPlayer(computer);

If ComputerPlayer is not a KPlayer, then this won't work. As described above - 
use a KPlayer* instead of a ComputerPlayer*.

> KPlayer *KMatchesDoc::createPlayer(int rtti,int io,bool isvirtual)
> {
>         KPlayer *player;
>         switch(rtti) {
>         case 1:
>                 player=(KPlayer *)new MatchesPlayer();
>                 break;
>         case 2:
>                 player=(KPlayer *)new ComputerPlayer();
>                 break;
>         }
>         if (!isvirtual) {
>                 createIO(player,(KGameIO::IOMode)io);
>         }
>         return player;
> }

Looks fine for me, assuming ComputerPlayer and MatchesPlayer are both derived 
from KPlayer. But you may probably want to use a single class, instead of two 
different classes for the players.

> void KMatchesDoc::createIO(KPlayer *player,KGameIO::IOMode io)
[...]
Looks fine.

> My players are :
>
> MatchesPlayer(): KPlayer(); //human (public inheritance)
> ComputerPlayer(): KGameIO(); // computer (public inheritance)

I think you got me wrong (please re-read my previous mail - I already told you 
what is going wrong).

A KPlayer should be used to store the data of a player. Think of it as your 
actual player.
A KGameIO should be used to control the input/output of your player only. 
Think of it as the "brain" of your player.

A KPlayer has a name, has the money (assuming your game uses money), ...
A KGameIO does not have a name. It always belongs to exactly one KPlayer 
object, which provides all these.

> Human rtti returns 1 and computer 2.

Usually you won't need the KPlayer::rtti(), as you'll use the same class for 
all players. Only your KGameIO::rtti()'s will differ.

> Is there something wrong in this ?

Yes, you should use
MyPlayer : public KPlayer
instead of MatchesPlayer and ComputerPlayer
and also
ComputerPlayerIO : public KGameIO

You use a KGameMouseIO if you create a human player and you use 
ComputerPlayerIO if you create a computer player.

> BTW, isn't there something strange in "player=(KPlayer *)new
> ComputerPlayer();" since ComputerPlayer inherits from KGameIO ?

As said im my previous mail - it is _definitely_, 100%, *absolutely* wrong. It 
must crash sooner or later.

> Thanks for your patient help, Andeas.
> Cheers.

CU
Andi


More information about the kde-games-devel mailing list