KDEGames

kgamesequence.cpp
1 /*
2  This file is part of the KDE games library
3  SPDX-FileCopyrightText: 2003 Andreas Beckermann <[email protected]>
4  SPDX-FileCopyrightText: 2003 Martin Heni <kde at heni-online.de>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "kgamesequence.h"
10 
11 // own
12 #include "kplayer.h"
13 #include "kgame.h"
14 
15 class KGameSequencePrivate
16 {
17  public:
18  KGameSequencePrivate()
19  : mGame(nullptr), mCurrentPlayer(nullptr)
20  {
21  }
22 
23  KGame* mGame;
24  KPlayer* mCurrentPlayer;
25 };
26 
27 KGameSequence::KGameSequence()
28  : QObject(), d(new KGameSequencePrivate)
29 {
30 }
31 
32 KGameSequence::~KGameSequence() = default;
33 
35 {
36  d->mGame = game;
37 }
38 
40 {
41  return d->mGame;
42 }
43 
44 KPlayer* KGameSequence::currentPlayer() const
45 {
46  return d->mCurrentPlayer;
47 }
48 
49 void KGameSequence::setCurrentPlayer(KPlayer* player)
50 {
51  d->mCurrentPlayer = player;
52 }
53 
55 {
56  qCDebug(GAMES_PRIVATE_KGAME) << "=================== NEXT PLAYER ==========================";
57  if (!game())
58  {
59  qCCritical(GAMES_PRIVATE_KGAME) << "NULL game object";
60  return nullptr;
61  }
62  unsigned int minId,nextId,lastId;
63  KPlayer *nextplayer, *minplayer;
64  if (last)
65  {
66  lastId = last->id();
67  }
68  else
69  {
70  lastId = 0;
71  }
72 
73  qCDebug(GAMES_PRIVATE_KGAME) << "nextPlayer: lastId="<<lastId;
74 
75  // remove when this has been checked
76  minId = 0x7fff; // we just need a very large number...properly MAX_UINT or so would be ok...
77  nextId = minId;
78  nextplayer = nullptr;
79  minplayer = nullptr;
80 
81  QList<KPlayer*>::iterator it = game()->playerList()->begin();
82  for (;it != game()->playerList()->end(); it++ )
83  {
84  KPlayer* player = *it;
85  // Find the first player for a cycle
86  if (player->id() < minId)
87  {
88  minId=player->id();
89  minplayer=player;
90  }
91  if (player==last)
92  {
93  continue;
94  }
95  // Find the next player which is bigger than the current one
96  if (player->id() > lastId && player->id() < nextId)
97  {
98  nextId=player->id();
99  nextplayer=player;
100  }
101  }
102 
103  // Cycle to the beginning
104  if (!nextplayer)
105  {
106  nextplayer=minplayer;
107  }
108 
109  qCDebug(GAMES_PRIVATE_KGAME) << " ##### lastId=" << lastId << "exclusive="
110  << exclusive << " minId=" << minId << "nextid=" << nextId
111  << "count=" << game()->playerList()->count();
112  if (nextplayer)
113  {
114  nextplayer->setTurn(true,exclusive);
115  }
116  else
117  {
118  return nullptr;
119  }
120  return nextplayer;
121 }
122 
123 // Per default we do not do anything
125 {
126  return 0;
127 }
128 /*
129  * vim: et sw=2
130  */
quint32 id() const
Returns the id of the player.
Definition: kplayer.cpp:235
void setGame(KGame *game)
Set the KGame object for this sequence.
KGame * game() const
bool setTurn(bool b, bool exclusive=true)
Sets whether this player is the next to turn.
Definition: kplayer.cpp:330
virtual int checkGameOver(KPlayer *player)
Check whether the game is over.
virtual KPlayer * nextPlayer(KPlayer *last, bool exclusive=true)
Select the next player in a turn based game.
Base class for a game player.
Definition: kplayer.h:60
QList::iterator end()
The main KDE game object.
Definition: kgame.h:57
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Dec 7 2021 22:34:15 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.