File indexing completed on 2024-05-05 04:02:29

0001 /***************************************************************************
0002 *   KBlocks, a falling blocks game by KDE                                *
0003 *   SPDX-FileCopyrightText: 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com>       *
0004 *                                                                         *
0005 *   SPDX-License-Identifier: GPL-2.0-or-later
0006 ***************************************************************************/
0007 #include "KBlocksGameMessage.h"
0008 
0009 KBlocksGameMessage::KBlocksGameMessage(int poolSize)
0010 {
0011     mPoolSize = poolSize;
0012 
0013     mActionCount = 0;
0014     maActionType = new int[mPoolSize];
0015     maActionList = new int[mPoolSize];
0016 
0017     mResultCount = 0;
0018     maResultList = new int[mPoolSize];
0019 }
0020 
0021 KBlocksGameMessage::~KBlocksGameMessage()
0022 {
0023     delete [] maResultList;
0024     delete [] maActionList;
0025     delete [] maActionType;
0026 }
0027 
0028 bool KBlocksGameMessage::pickGameResult(int *result)
0029 {
0030     if (mResultCount == 0) {
0031         return false;
0032     }
0033 
0034     *result = maResultList[0];
0035 
0036     for (int j = 0; j < mResultCount - 1; j++) {
0037         maResultList[j] = maResultList[j + 1];
0038     }
0039 
0040     mResultCount--;
0041 
0042     return true;
0043 }
0044 
0045 bool KBlocksGameMessage::putGameResult(int result)
0046 {
0047     if (mResultCount == mPoolSize) {
0048         return false;
0049     }
0050 
0051     maResultList[mResultCount] = result;
0052 
0053     mResultCount++;
0054 
0055     return true;
0056 }
0057 
0058 void KBlocksGameMessage::clearGameResult()
0059 {
0060     mResultCount = 0;
0061 }
0062 
0063 bool KBlocksGameMessage::pickGameAction(int *type, int *action)
0064 {
0065     if (mActionCount == 0) {
0066         return false;
0067     }
0068 
0069     if (*type != GameAction_None) {
0070         for (int i = 0; i < mActionCount; i++) {
0071             if (*type == maActionType[i]) {
0072                 *action = maActionList[i];
0073 
0074                 for (int j = i; j < mActionCount - 1; j++) {
0075                     maActionType[j] = maActionType[j + 1];
0076                     maActionList[j] = maActionList[j + 1];
0077                 }
0078 
0079                 mActionCount--;
0080 
0081                 return true;
0082             }
0083         }
0084 
0085         return false;
0086     } else {
0087         *type = maActionType[0];
0088         *action = maActionList[0];
0089 
0090         for (int j = 0; j < mActionCount - 1; j++) {
0091             maActionType[j] = maActionType[j + 1];
0092             maActionList[j] = maActionList[j + 1];
0093         }
0094 
0095         mActionCount--;
0096 
0097         return true;
0098     }
0099 }
0100 
0101 bool KBlocksGameMessage::putGameAction(int type, int action)
0102 {
0103     if (mActionCount == mPoolSize) {
0104         return false;
0105     }
0106 
0107     maActionType[mActionCount] = type;
0108     maActionList[mActionCount] = action;
0109 
0110     mActionCount++;
0111 
0112     return true;
0113 }
0114 
0115 void KBlocksGameMessage::clearGameAction()
0116 {
0117     mActionCount = 0;
0118 }