1/***************************************************************************
2* KBlocks, a falling blocks game for KDE *
3* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
4* *
5* This program is free software; you can redistribute it and/or modify *
6* it under the terms of the GNU General Public License as published by *
7* the Free Software Foundation; either version 2 of the License, or *
8* (at your option) any later version. *
9***************************************************************************/
10#include "KBlocksGameMessage.h"
11
12KBlocksGameMessage::KBlocksGameMessage(int poolSize)
13{
14 mPoolSize = poolSize;
15
16 mActionCount = 0;
17 maActionType = new int[mPoolSize];
18 maActionList = new int[mPoolSize];
19
20 mResultCount = 0;
21 maResultList = new int[mPoolSize];
22}
23
24KBlocksGameMessage::~KBlocksGameMessage()
25{
26 delete [] maResultList;
27 delete [] maActionList;
28 delete [] maActionType;
29}
30
31bool KBlocksGameMessage::pickGameResult(int * result)
32{
33 if (mResultCount == 0)
34 {
35 return false;
36 }
37
38 *result = maResultList[0];
39
40 for(int j = 0; j < mResultCount - 1; j++)
41 {
42 maResultList[j] = maResultList[j + 1];
43 }
44
45 mResultCount--;
46
47 return true;
48}
49
50bool KBlocksGameMessage::putGameResult(int result)
51{
52 if (mResultCount == mPoolSize)
53 {
54 return false;
55 }
56
57 maResultList[mResultCount] = result;
58
59 mResultCount++;
60
61 return true;
62}
63
64void KBlocksGameMessage::clearGameResult()
65{
66 mResultCount = 0;
67}
68
69bool KBlocksGameMessage::pickGameAction(int * type, int * action)
70{
71 if (mActionCount == 0)
72 {
73 return false;
74 }
75
76 if (*type != GameAction_None)
77 {
78 for(int i = 0; i < mActionCount; i++)
79 {
80 if (*type == maActionType[i])
81 {
82 *action = maActionList[i];
83
84 for(int j = i; j < mActionCount - 1; j++)
85 {
86 maActionType[j] = maActionType[j + 1];
87 maActionList[j] = maActionList[j + 1];
88 }
89
90 mActionCount--;
91
92 return true;
93 }
94 }
95
96 return false;
97 }
98 else
99 {
100 *type = maActionType[0];
101 *action = maActionList[0];
102
103 for(int j = 0; j < mActionCount - 1; j++)
104 {
105 maActionType[j] = maActionType[j + 1];
106 maActionList[j] = maActionList[j + 1];
107 }
108
109 mActionCount--;
110
111 return true;
112 }
113}
114
115bool KBlocksGameMessage::putGameAction(int type, int action)
116{
117 if (mActionCount == mPoolSize)
118 {
119 return false;
120 }
121
122 maActionType[mActionCount] = type;
123 maActionList[mActionCount] = action;
124
125 mActionCount++;
126
127 return true;
128}
129
130void KBlocksGameMessage::clearGameAction()
131{
132 mActionCount = 0;
133}
134