File: GameData.h

package info (click to toggle)
kmahjongg 4:4.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,968 kB
  • ctags: 427
  • sloc: cpp: 3,945; makefile: 9; sh: 2
file content (242 lines) | stat: -rw-r--r-- 5,703 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* kmahjongg, the classic mahjongg game for KDE project
 *
 * Copyright (C) 1997 Mathias Mueller   <in5y158@public.uni-hamburg.de>
 * Copyright (C) 2006-2007 Mauricio Piacentini   <mauricio@tabuleiro.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. */

#ifndef GAMEDATA_H
#define GAMEDATA_H


#include "KmTypes.h"
#include "BoardLayout.h"

#include <QByteArray>
#include <QVector>

#include <krandomsequence.h>


// tiles symbol names:
/*#define TILE_OFFSET      2

#define TILE_CHARACTER  ( 0 + TILE_OFFSET)
#define TILE_BAMBOO     ( 9 + TILE_OFFSET)
#define TILE_ROD        (18 + TILE_OFFSET)
#define TILE_SEASON     (27 + TILE_OFFSET)
#define TILE_WIND       (31 + TILE_OFFSET)
#define TILE_DRAGON     (36 + TILE_OFFSET)
#define TILE_FLOWER     (39 + TILE_OFFSET)*/


/**
 * @short This class implements
 * 
 * longer description
 *
 * @author Mauricio Piacentini  <mauricio@tabuleiro.com> */
class GameData
{
public:
    /**
     * Constructor
     * 
     * @param boardlayout 
     * @see BoardLayout */
    explicit GameData(BoardLayout *boardlayout);

    /**
     * Default Destructor */
    ~GameData();

    /**
     * Method Description
     *
     * @param e
     * @param y
     * @param x
     * @param f */
    void putTile(short e, short y, short x, UCHAR f);

    /**
     * Method Description
     *
     * @param pos
     * @ref pos */
    void putTile(POSITION &pos) {putTile(pos.e, pos.y, pos.x, pos.f);}

    /**
     * Method Description
     *
     * @param z
     * @param y
     * @param x
     * @return @c true if ...
     * @return @c false if ... */
    bool tilePresent(int z, int y, int x);

    /**
     * Method Description
     *
     * @param z
     * @param y
     * @param x
     * @return @c true if ...
     * @return @c false if ... */
    bool partTile(int z, int y, int x);

    /**
     * Method Description
     *
     * @param z
     * @param y
     * @param x
     * @return UCHAR */
    UCHAR BoardData(short z, short y, short x);

    /**
     * Method Description
     *
     * @param z
     * @param y
     * @param x
     * @param value */
    void setBoardData(short z, short y, short x, UCHAR value);

    /**
     * Method Description
     *
     * @param z
     * @param y
     * @param x
     * @return UCHAR */
    UCHAR MaskData(short z, short y, short x);

    /**
     * Method Description
     *
     * @param z
     * @param y
     * @param x
     * @return UCHAR */
    UCHAR HighlightData(short z, short y, short x);

    /**
     * Method Description
     *
     * @param z
     * @param y
     * @param x
     * @param value */
    void setHighlightData(short z, short y, short x, UCHAR value);

    /**
     * Method Description
     *
     * @param i
     * @return POSITION
     * @ref pos */
    POSITION& MoveListData(short i);

    /**
     * Method Description
     *
     * @param i 
     * @param value
     * @ref pos */
    void setMoveListData(short i, POSITION &value);

    /**
     * Method Description
     *
     * @return *char blah blah */
    char* getMaskBytes() {return Mask.data();}

    bool saveToStream(QDataStream &out);
    bool loadFromStream(QDataStream &in);

    void generatePositionDepends();
    void generateTilePositions();

    bool generateStartPosition2();

    bool findMove(POSITION &posA, POSITION &posB);
    int moveCount();
    short findAllMatchingTiles(POSITION &posA);
    void initialiseRemovedTiles();
    void setRemovedTilePair(POSITION &a, POSITION &b);
    void clearRemovedTilePair(POSITION &a, POSITION &b);
    bool isMatchingTile(POSITION &Pos1, POSITION &Pos2);
    void shuffle();
    POSITION& getFromPosTable(int index) {return PosTable[index];}

    int allow_undo;
    int allow_redo;

    USHORT TileNum;
    USHORT MaxTileNum;

    //Board Layout dimensions
    short m_width;
    short m_height;
    short m_depth;
    short m_maxTiles;

    KRandomSequence random;

private:
    int tileAt(int x, int y, int z);
    bool generateSolvableGame();
    bool onlyFreeInLine(int position);
    int selectPosition(int lastPosition);
    void placeTile(int position, int tile);
    void updateDepend(int position);

    //other generation bits
    void randomiseFaces();
    void getFaces(POSITION &a, POSITION &b);

    int tilesAllocated;
    int tilesUsed;

    UCHAR tilePair[144];

    // storage to keep track of removed tiles
    unsigned char removedCharacter[9];
    unsigned char removedBamboo[9];
    unsigned char removedRod[9];
    unsigned char removedDragon[3];
    unsigned char removedWind[9];
    unsigned char removedFlower[4];
    unsigned char removedSeason[4];

    // new bits for new game generation, with solvability. Scratch storage
    int numTilesToGenerate;

    QByteArray Board;
    QByteArray Mask;
    QByteArray Highlight;
    QVector<POSITION> MoveList;

    QVector<POSITION> tilePositions;
    QVector<DEPENDENCY> positionDepends;

    //PosTable, scratch storage used for highlighting matching tiles
    QVector<POSITION> PosTable;
};


#endif // GAMEDATA_H