KDEGames

kgamesequence.cpp
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2003 Andreas Beckermann ([email protected])
4  Copyright (C) 2003 Martin Heni (kde at heni-online.de)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #include "kgamesequence.h"
22 
23 
24 #include "kplayer.h"
25 #include "kgame.h"
26 
27 class KGameSequence::KGameSequencePrivate
28 {
29  public:
30  KGameSequencePrivate()
31  : mGame(nullptr), mCurrentPlayer(nullptr)
32  {
33  }
34 
35  KGame* mGame;
36  KPlayer* mCurrentPlayer;
37 };
38 
39 KGameSequence::KGameSequence()
40  : QObject(), d(new KGameSequencePrivate)
41 {
42 }
43 
44 KGameSequence::~KGameSequence()
45 {
46  delete d;
47 }
48 
50 {
51  d->mGame = game;
52 }
53 
55 {
56  return d->mGame;
57 }
58 
59 KPlayer* KGameSequence::currentPlayer() const
60 {
61  return d->mCurrentPlayer;
62 }
63 
64 void KGameSequence::setCurrentPlayer(KPlayer* player)
65 {
66  d->mCurrentPlayer = player;
67 }
68 
70 {
71  qCDebug(GAMES_PRIVATE_KGAME) << "=================== NEXT PLAYER ==========================";
72  if (!game())
73  {
74  qCCritical(GAMES_PRIVATE_KGAME) << "NULL game object";
75  return nullptr;
76  }
77  unsigned int minId,nextId,lastId;
78  KPlayer *nextplayer, *minplayer;
79  if (last)
80  {
81  lastId = last->id();
82  }
83  else
84  {
85  lastId = 0;
86  }
87 
88  qCDebug(GAMES_PRIVATE_KGAME) << "nextPlayer: lastId="<<lastId;
89 
90  // remove when this has been checked
91  minId = 0x7fff; // we just need a very large number...properly MAX_UINT or so would be ok...
92  nextId = minId;
93  nextplayer = nullptr;
94  minplayer = nullptr;
95 
96  QList<KPlayer*>::iterator it = game()->playerList()->begin();
97  for (;it != game()->playerList()->end(); it++ )
98  {
99  KPlayer* player = *it;
100  // Find the first player for a cycle
101  if (player->id() < minId)
102  {
103  minId=player->id();
104  minplayer=player;
105  }
106  if (player==last)
107  {
108  continue;
109  }
110  // Find the next player which is bigger than the current one
111  if (player->id() > lastId && player->id() < nextId)
112  {
113  nextId=player->id();
114  nextplayer=player;
115  }
116  }
117 
118  // Cycle to the beginning
119  if (!nextplayer)
120  {
121  nextplayer=minplayer;
122  }
123 
124  qCDebug(GAMES_PRIVATE_KGAME) << " ##### lastId=" << lastId << "exclusive="
125  << exclusive << " minId=" << minId << "nextid=" << nextId
126  << "count=" << game()->playerList()->count();
127  if (nextplayer)
128  {
129  nextplayer->setTurn(true,exclusive);
130  }
131  else
132  {
133  return nullptr;
134  }
135  return nextplayer;
136 }
137 
138 // Per default we do not do anything
140 {
141  return 0;
142 }
143 /*
144  * vim: et sw=2
145  */
quint32 id() const
Returns the id of the player.
Definition: kplayer.cpp:246
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:341
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:69
QList::iterator end()
The main KDE game object.
Definition: kgame.h:64
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.