File indexing completed on 2024-06-16 03:57:56

0001 /*
0002     This file is part of the KDE games library
0003     SPDX-FileCopyrightText: 2003 Andreas Beckermann <b_mann@gmx.de>
0004     SPDX-FileCopyrightText: 2003 Martin Heni <kde at heni-online.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include "kgamesequence.h"
0010 
0011 // own
0012 #include "kgame.h"
0013 #include "kplayer.h"
0014 #include <kdegamesprivate_kgame_logging.h>
0015 
0016 class KGameSequencePrivate
0017 {
0018 public:
0019     KGameSequencePrivate() = default;
0020 
0021 public:
0022     KGame *mGame = nullptr;
0023     KPlayer *mCurrentPlayer = nullptr;
0024 };
0025 
0026 KGameSequence::KGameSequence()
0027     : QObject()
0028     , d(new KGameSequencePrivate)
0029 {
0030 }
0031 
0032 KGameSequence::~KGameSequence() = default;
0033 
0034 void KGameSequence::setGame(KGame *game)
0035 {
0036     d->mGame = game;
0037 }
0038 
0039 KGame *KGameSequence::game() const
0040 {
0041     return d->mGame;
0042 }
0043 
0044 KPlayer *KGameSequence::currentPlayer() const
0045 {
0046     return d->mCurrentPlayer;
0047 }
0048 
0049 void KGameSequence::setCurrentPlayer(KPlayer *player)
0050 {
0051     d->mCurrentPlayer = player;
0052 }
0053 
0054 KPlayer *KGameSequence::nextPlayer(KPlayer *last, bool exclusive)
0055 {
0056     qCDebug(KDEGAMESPRIVATE_KGAME_LOG) << "=================== NEXT PLAYER ==========================";
0057     if (!game()) {
0058         qCCritical(KDEGAMESPRIVATE_KGAME_LOG) << "NULL game object";
0059         return nullptr;
0060     }
0061     unsigned int minId, nextId, lastId;
0062     KPlayer *nextplayer, *minplayer;
0063     if (last) {
0064         lastId = last->id();
0065     } else {
0066         lastId = 0;
0067     }
0068 
0069     qCDebug(KDEGAMESPRIVATE_KGAME_LOG) << "nextPlayer: lastId=" << lastId;
0070 
0071     // remove when this has been checked
0072     minId = 0x7fff; // we just need a very large number...properly MAX_UINT or so would be ok...
0073     nextId = minId;
0074     nextplayer = nullptr;
0075     minplayer = nullptr;
0076 
0077     QList<KPlayer *>::iterator it = game()->playerList()->begin();
0078     for (; it != game()->playerList()->end(); it++) {
0079         KPlayer *player = *it;
0080         // Find the first player for a cycle
0081         if (player->id() < minId) {
0082             minId = player->id();
0083             minplayer = player;
0084         }
0085         if (player == last) {
0086             continue;
0087         }
0088         // Find the next player which is bigger than the current one
0089         if (player->id() > lastId && player->id() < nextId) {
0090             nextId = player->id();
0091             nextplayer = player;
0092         }
0093     }
0094 
0095     // Cycle to the beginning
0096     if (!nextplayer) {
0097         nextplayer = minplayer;
0098     }
0099 
0100     qCDebug(KDEGAMESPRIVATE_KGAME_LOG) << " ##### lastId=" << lastId << "exclusive=" << exclusive << "  minId=" << minId << "nextid=" << nextId
0101                                        << "count=" << game()->playerList()->count();
0102     if (nextplayer) {
0103         nextplayer->setTurn(true, exclusive);
0104     } else {
0105         return nullptr;
0106     }
0107     return nextplayer;
0108 }
0109 
0110 // Per default we do not do anything
0111 int KGameSequence::checkGameOver(KPlayer *)
0112 {
0113     return 0;
0114 }
0115 /*
0116  * vim: et sw=2
0117  */
0118 
0119 #include "moc_kgamesequence.cpp"