1 | /* kmahjongg, the classic mahjongg game for KDE project |
2 | * |
3 | * Copyright (C) 1997 Mathias Mueller <in5y158@public.uni-hamburg.de> |
4 | * Copyright (C) 2006-2007 Mauricio Piacentini <mauricio@tabuleiro.com> |
5 | * |
6 | * This program is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by |
8 | * the Free Software Foundation; either version 2 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
19 | |
20 | #ifndef GAMEDATA_H |
21 | #define GAMEDATA_H |
22 | |
23 | |
24 | #include "KmTypes.h" |
25 | #include "BoardLayout.h" |
26 | |
27 | #include <QByteArray> |
28 | #include <QVector> |
29 | |
30 | #include <krandomsequence.h> |
31 | |
32 | |
33 | // tiles symbol names: |
34 | /*#define TILE_OFFSET 2 |
35 | |
36 | #define TILE_CHARACTER ( 0 + TILE_OFFSET) |
37 | #define TILE_BAMBOO ( 9 + TILE_OFFSET) |
38 | #define TILE_ROD (18 + TILE_OFFSET) |
39 | #define TILE_SEASON (27 + TILE_OFFSET) |
40 | #define TILE_WIND (31 + TILE_OFFSET) |
41 | #define TILE_DRAGON (36 + TILE_OFFSET) |
42 | #define TILE_FLOWER (39 + TILE_OFFSET)*/ |
43 | |
44 | |
45 | /** |
46 | * @short This class implements |
47 | * |
48 | * longer description |
49 | * |
50 | * @author Mauricio Piacentini <mauricio@tabuleiro.com> */ |
51 | class GameData |
52 | { |
53 | public: |
54 | /** |
55 | * Constructor |
56 | * |
57 | * @param boardlayout |
58 | * @see BoardLayout */ |
59 | explicit GameData(BoardLayout *boardlayout); |
60 | |
61 | /** |
62 | * Default Destructor */ |
63 | ~GameData(); |
64 | |
65 | /** |
66 | * Method Description |
67 | * |
68 | * @param e |
69 | * @param y |
70 | * @param x |
71 | * @param f */ |
72 | void putTile(short e, short y, short x, UCHAR f); |
73 | |
74 | /** |
75 | * Method Description |
76 | * |
77 | * @param pos |
78 | * @ref pos */ |
79 | void putTile(POSITION &pos) {putTile(pos.e, pos.y, pos.x, pos.f);} |
80 | |
81 | /** |
82 | * Method Description |
83 | * |
84 | * @param z |
85 | * @param y |
86 | * @param x |
87 | * @return @c true if ... |
88 | * @return @c false if ... */ |
89 | bool tilePresent(int z, int y, int x); |
90 | |
91 | /** |
92 | * Method Description |
93 | * |
94 | * @param z |
95 | * @param y |
96 | * @param x |
97 | * @return @c true if ... |
98 | * @return @c false if ... */ |
99 | bool partTile(int z, int y, int x); |
100 | |
101 | /** |
102 | * Method Description |
103 | * |
104 | * @param z |
105 | * @param y |
106 | * @param x |
107 | * @return UCHAR */ |
108 | UCHAR BoardData(short z, short y, short x); |
109 | |
110 | /** |
111 | * Method Description |
112 | * |
113 | * @param z |
114 | * @param y |
115 | * @param x |
116 | * @param value */ |
117 | void setBoardData(short z, short y, short x, UCHAR value); |
118 | |
119 | /** |
120 | * Method Description |
121 | * |
122 | * @param z |
123 | * @param y |
124 | * @param x |
125 | * @return UCHAR */ |
126 | UCHAR MaskData(short z, short y, short x); |
127 | |
128 | /** |
129 | * Method Description |
130 | * |
131 | * @param z |
132 | * @param y |
133 | * @param x |
134 | * @return UCHAR */ |
135 | UCHAR HighlightData(short z, short y, short x); |
136 | |
137 | /** |
138 | * Method Description |
139 | * |
140 | * @param z |
141 | * @param y |
142 | * @param x |
143 | * @param value */ |
144 | void setHighlightData(short z, short y, short x, UCHAR value); |
145 | |
146 | /** |
147 | * Method Description |
148 | * |
149 | * @param i |
150 | * @return POSITION |
151 | * @ref pos */ |
152 | POSITION& MoveListData(short i); |
153 | |
154 | /** |
155 | * Method Description |
156 | * |
157 | * @param i |
158 | * @param value |
159 | * @ref pos */ |
160 | void setMoveListData(short i, POSITION &value); |
161 | |
162 | /** |
163 | * Method Description |
164 | * |
165 | * @return *char blah blah */ |
166 | char* getMaskBytes() {return Mask.data();} |
167 | |
168 | bool saveToStream(QDataStream &out); |
169 | bool loadFromStream(QDataStream &in); |
170 | |
171 | void generatePositionDepends(); |
172 | void generateTilePositions(); |
173 | |
174 | bool generateStartPosition2(); |
175 | |
176 | bool findMove(POSITION &posA, POSITION &posB); |
177 | int moveCount(); |
178 | short findAllMatchingTiles(POSITION &posA); |
179 | void initialiseRemovedTiles(); |
180 | void setRemovedTilePair(POSITION &a, POSITION &b); |
181 | void clearRemovedTilePair(POSITION &a, POSITION &b); |
182 | bool isMatchingTile(POSITION &Pos1, POSITION &Pos2); |
183 | void shuffle(); |
184 | POSITION& getFromPosTable(int index) {return PosTable[index];} |
185 | |
186 | int allow_undo; |
187 | int allow_redo; |
188 | |
189 | USHORT TileNum; |
190 | USHORT MaxTileNum; |
191 | |
192 | //Board Layout dimensions |
193 | short m_width; |
194 | short m_height; |
195 | short m_depth; |
196 | short m_maxTiles; |
197 | |
198 | KRandomSequence random; |
199 | |
200 | private: |
201 | int tileAt(int x, int y, int z); |
202 | bool generateSolvableGame(); |
203 | bool onlyFreeInLine(int position); |
204 | int selectPosition(int lastPosition); |
205 | void placeTile(int position, int tile); |
206 | void updateDepend(int position); |
207 | |
208 | //other generation bits |
209 | void randomiseFaces(); |
210 | void getFaces(POSITION &a, POSITION &b); |
211 | |
212 | int tilesAllocated; |
213 | int tilesUsed; |
214 | |
215 | UCHAR tilePair[144]; |
216 | |
217 | // storage to keep track of removed tiles |
218 | unsigned char removedCharacter[9]; |
219 | unsigned char removedBamboo[9]; |
220 | unsigned char removedRod[9]; |
221 | unsigned char removedDragon[3]; |
222 | unsigned char removedWind[9]; |
223 | unsigned char removedFlower[4]; |
224 | unsigned char removedSeason[4]; |
225 | |
226 | // new bits for new game generation, with solvability. Scratch storage |
227 | int numTilesToGenerate; |
228 | |
229 | QByteArray Board; |
230 | QByteArray Mask; |
231 | QByteArray Highlight; |
232 | QVector<POSITION> MoveList; |
233 | |
234 | QVector<POSITION> tilePositions; |
235 | QVector<DEPENDENCY> positionDepends; |
236 | |
237 | //PosTable, scratch storage used for highlighting matching tiles |
238 | QVector<POSITION> PosTable; |
239 | }; |
240 | |
241 | |
242 | #endif // GAMEDATA_H |
243 | |