/
Board.asc
280 lines (217 loc) · 7.09 KB
/
Board.asc
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// new module script
Point* Board::PosToXY(int row, int col)
{
return Point.Create(this.MarginLeft+col*this.TileSize, this.MarginTop+row*this.TileSize);
}
Point* Board::XYToPos(int x, int y)
{
return Point.Create((x-this.MarginLeft)/this.TileSize, (y-this.MarginTop)/this.TileSize);
}
void _CreatePieceOfType(int r, int c, PieceType p_type, Board* b, Matrix* mxPieces)
{
PieceID id;
id = mxPieces.GetCell(r, c);
if(id > 0) {
mxPieces.SetCell(r, c, 0);
Piece.Delete(id);
}
id = Piece.New(p_type, b.TileSize, b.PosToXY(r, c), Point.Create(r, c), b);
mxPieces.SetCell(r, c, id);
}
void _RandomFill(Board* b, Matrix* mxPieces, TypeRandomizer* typeRandomizer)
{
int numCols = mxPieces.NumCols;
int numRows = mxPieces.NumRows;
for(int r=0; r<numRows; r++) {
for(int c=0; c<numCols; c++) {
int p_type;
while(true) {
p_type = typeRandomizer.Get();
if ((r < 2) && (c < 2)) break;
int type_a = Piece.GetType(mxPieces.GetCell(r-1, c));
int type_b = Piece.GetType(mxPieces.GetCell(r, c-1));
int type_c = Piece.GetType(mxPieces.GetCell(r-2, c));
int type_d = Piece.GetType(mxPieces.GetCell(r, c-2));
bool makesMatch = p_type != ePiece_None && ((type_a == p_type) && (type_c == p_type)) || ((type_b == p_type) && (type_d == p_type));
if (!makesMatch) break;
}
_CreatePieceOfType(r, c, p_type, b, mxPieces);
}
}
}
void _ClearMatrix(Matrix* mxPieces)
{
if(mxPieces == null) return;
int numCols = mxPieces.NumCols;
int numRows = mxPieces.NumRows;
for(int r=0; r<numRows; r++) {
for(int c=0; c<numCols; c++) {
PieceID id = mxPieces.GetCell(r, c);
if(id <= 0) continue;
mxPieces.SetCell(r, c, 0);
Piece.Delete(id);
}
}
}
void _ReadjustMargins(Board* b)
{
b.MarginTop = ((b.Height - (b.NumRows * b.TileSize)) / 2) + b.PositionY_px;
b.MarginLeft = (( b.Width - (b.NumCols * b.TileSize)) / 2) + b.PositionX_px;
b.RowCol_Width = b.NumCols * b.TileSize;
b.RowCol_Height = b.NumRows * b.TileSize;
}
static void Board::Clear(Matrix* mxPieces)
{
_ClearMatrix(mxPieces);
}
void Board::ResizeRowCol(Matrix* mxPieces, int numRows, int numCols)
{
if(numRows == this.NumRows && numCols == this.NumCols) return;
IntArray* origCells = mxPieces.GetCells();
mxPieces.Resize(numRows, numCols);
IntArray* rszCells = mxPieces.GetCells();
IntArray* lostCells = origCells.Difference(rszCells);
lostCells = lostCells.Unique();
while(!lostCells.IsEmpty())
{
PieceID id = lostCells.Head();
if(id > 0) { Piece.Delete(id); }
lostCells.Pop();
}
this.NumRows = numRows;
this.NumCols = numCols;
if(numCols <= 4 && numRows <= 4) this.TileSize = 44;
else this.TileSize = 22;
this.RowCol_Height = numRows * this.TileSize;
this.RowCol_Width = numCols * this.TileSize;
_ReadjustMargins(this);
for(int r=0; r<numRows; r++) {
for(int c=0; c<numCols; c++) {
PieceID id = mxPieces.GetCell(r, c);
if(id == 0) continue;
Piece.SetRowCol(id, Point.Create(r, c));
Piece.SetTsize(id, this.TileSize);
Piece.SyncXYfromRowCol(id);
}
}
}
bool Board::FromString(Matrix* mxPieces, String boardDescription)
{
_ClearMatrix(mxPieces);
if(String.IsNullOrEmpty(boardDescription) || boardDescription.IndexOf("{\"board\":{") == -1) return false;
int numRows, numCols;
String json_string = boardDescription;
JsonParser* parser = new JsonParser;
int token_count = 512;
JsonToken* t[] = JsonToken.NewArray(token_count);
int r = parser.Parse(json_string, t, token_count);
// now that you have the Tokens, you can use them to parse as you wish!
if (r < 0) {
String JError = "";
if (r == eJSON_Error_InsuficientTokens) JError = JError.Append("\nNot enough tokens were provided");
if (r == eJSON_Error_InvalidCharacter) JError = JError.Append("\nInvalid character inside JSON string");
if (r == eJSON_Error_Partial) JError = JError.Append("\nThe string is not a full JSON packet, more bytes expected");
Display("Failed to parse JSON: %d\n%s", r, JError);
return false;
}
if (r < 1 || t[0].type != eJSON_Tok_OBJECT) {
Display("Object expected\n");
return false;
}
bool nxtRows, nxtCols, nxtPieces, nxtPiecesArray, nxtPiece, nxtPieceType;
int p_idx = -1;
for(int i=0; i<r ; i++){
JsonToken* tok = t[i];
String tok_as_str = tok.ToString(json_string);
if(nxtRows) {
nxtRows = false;
numRows = tok_as_str.AsInt;
}
if(nxtCols) {
nxtCols = false;
numCols = tok_as_str.AsInt;
}
if(nxtPiecesArray) {
if(nxtPiece) {
int p_type;
if(nxtPieceType) {
p_type = tok_as_str.AsInt;
if(p_type>0) {
_CreatePieceOfType(p_idx/numCols, p_idx%numCols, p_type, this, mxPieces);
}
nxtPieceType = false;
nxtPiece = false;
}
if(nxtPiece && (tok.type == eJSON_Tok_STRING) && (tok_as_str == "t")) nxtPieceType = true;
}
if(tok.type == eJSON_Tok_OBJECT) {
nxtPiece = true;
p_idx++;
mxPieces.SetCell(p_idx/numCols, p_idx%numCols, 0);
}
}
if(nxtPieces) {
this.ResizeRowCol(mxPieces, numRows, numCols);
nxtPieces = false;
nxtPiecesArray = true;
}
if(tok_as_str == "numRows") {
nxtRows = true;
}
if(tok_as_str == "numCols") {
nxtCols = true;
}
if(tok_as_str == "pieces") {
nxtPieces = true;
}
}
return true;
}
String Board::ToString(Matrix* mxPieces)
{
int numRows = this.NumRows;
int numCols = this.NumCols;
String st = "{\"board\":{";
st = st.Append(String.Format("\"numRows\":%d,",numRows));
st = st.Append(String.Format("\"numCols\":%d,",numCols));
st = st.Append("\"pieces\":[");
for(int r=0; r<numRows; r++) {
for(int c=0; c<numCols; c++) {
PieceID id = mxPieces.GetCell(r, c);
if(id <= 0) {
// empty cell
st = st.Append("{}");
} else {
// cell has a piece
PieceType type = Piece.GetType(id);
st = st.Append(String.Format("{\"t\":%d}",type));
}
if(r != numRows-1 || c != numCols-1) st = st.Append(",");
}
}
st = st.Append("]}}");
//File* f = File.Open("$SAVEGAMEDIR$/test.json",eFileWrite);
// f.WriteRawLine(st);
//f.Close();
return st;
}
void Board::RandomFill(Matrix* mxPieces, TypeRandomizer* typeRandomizer)
{
_RandomFill(this, mxPieces, typeRandomizer);
}
static Board* Board::Create(Point* pxPosition, Point* widthHeight, Point* rowsCols, Matrix* mxPieces, int tileSize, TypeRandomizer* typeRandomizer)
{
Board* b = new Board;
b.Width = widthHeight.x;
b.Height = widthHeight.y;
b.NumRows = rowsCols.x;
b.NumCols = rowsCols.y;
b.PositionX_px = pxPosition.x;
b.PositionY_px = pxPosition.y;
int tWidth = b.Width / b.NumCols;
int tHeight = b.Height / b.NumRows;
b.TileSize = MinI(tWidth, tHeight);
b.TileSize = tileSize;
_ReadjustMargins(b);
return b;
}