KDEGames

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

KDE's Doxygen guidelines are available online.