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 "kgame.h"
13 #include "kplayer.h"
14 
15 class KGameSequencePrivate
16 {
17 public:
18  KGameSequencePrivate()
19  : mGame(nullptr)
20  , mCurrentPlayer(nullptr)
21  {
22  }
23 
24  KGame *mGame;
25  KPlayer *mCurrentPlayer;
26 };
27 
28 KGameSequence::KGameSequence()
29  : QObject()
30  , d(new KGameSequencePrivate)
31 {
32 }
33 
34 KGameSequence::~KGameSequence() = default;
35 
37 {
38  d->mGame = game;
39 }
40 
42 {
43  return d->mGame;
44 }
45 
46 KPlayer *KGameSequence::currentPlayer() const
47 {
48  return d->mCurrentPlayer;
49 }
50 
51 void KGameSequence::setCurrentPlayer(KPlayer *player)
52 {
53  d->mCurrentPlayer = player;
54 }
55 
56 KPlayer *KGameSequence::nextPlayer(KPlayer *last, bool exclusive)
57 {
58  qCDebug(GAMES_PRIVATE_KGAME) << "=================== NEXT PLAYER ==========================";
59  if (!game()) {
60  qCCritical(GAMES_PRIVATE_KGAME) << "NULL game object";
61  return nullptr;
62  }
63  unsigned int minId, nextId, lastId;
64  KPlayer *nextplayer, *minplayer;
65  if (last) {
66  lastId = last->id();
67  } else {
68  lastId = 0;
69  }
70 
71  qCDebug(GAMES_PRIVATE_KGAME) << "nextPlayer: lastId=" << lastId;
72 
73  // remove when this has been checked
74  minId = 0x7fff; // we just need a very large number...properly MAX_UINT or so would be ok...
75  nextId = minId;
76  nextplayer = nullptr;
77  minplayer = nullptr;
78 
80  for (; it != game()->playerList()->end(); it++) {
81  KPlayer *player = *it;
82  // Find the first player for a cycle
83  if (player->id() < minId) {
84  minId = player->id();
85  minplayer = player;
86  }
87  if (player == last) {
88  continue;
89  }
90  // Find the next player which is bigger than the current one
91  if (player->id() > lastId && player->id() < nextId) {
92  nextId = player->id();
93  nextplayer = player;
94  }
95  }
96 
97  // Cycle to the beginning
98  if (!nextplayer) {
99  nextplayer = minplayer;
100  }
101 
102  qCDebug(GAMES_PRIVATE_KGAME) << " ##### lastId=" << lastId << "exclusive=" << exclusive << " minId=" << minId << "nextid=" << nextId
103  << "count=" << game()->playerList()->count();
104  if (nextplayer) {
105  nextplayer->setTurn(true, exclusive);
106  } else {
107  return nullptr;
108  }
109  return nextplayer;
110 }
111 
112 // Per default we do not do anything
114 {
115  return 0;
116 }
117 /*
118  * vim: et sw=2
119  */
virtual int checkGameOver(KPlayer *player)
Check whether the game is over.
KGame * game() const
int count(const T &value) const const
KGamePlayerList * playerList()
Returns a list of all active players.
Definition: kgame.cpp:681
bool setTurn(bool b, bool exclusive=true)
Sets whether this player is the next to turn.
Definition: kplayer.cpp:335
The main KDE game object.
Definition: kgame.h:55
Base class for a game player.
Definition: kplayer.h:59
quint32 id() const
Returns the id of the player.
Definition: kplayer.cpp:233
void setGame(KGame *game)
Set the KGame object for this sequence.
virtual KPlayer * nextPlayer(KPlayer *last, bool exclusive=true)
Select the next player in a turn based game.
QList::iterator begin()
QList::iterator end()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Mar 23 2023 04:15:34 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.