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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "gfCWordSearchMinigameGenerateComponent.h"

#include <algorithm>

#include <fsCore/fsIFile.h>
#include <fsCore/src/fsNRnd.h>
#include <fsCore/src/fsCResourceName.h>

const fsCUniqueId gfCWordSearchMinigameGenerateComponent::mTypeId("fsCWordSearchMinigameGenerateComponent");
const fsStr gfCWordSearchMinigameGenerateComponent::mPlaceHolderTile("-");


void gfCWordSearchMinigameGenerateComponent::fileSave()
{
	fsStr saveStr;
	for (fsS32 row = 0; row < mDimensions.y; ++row)
	{
		fsStr rowStr;
		for (fsS32 column = 0; column < mDimensions.x; ++column)
		{
			rowStr += mTiles[row * mDimensions.x + column] + ",";
		}
		rowStr += '\n';
		saveStr += rowStr;
	}

	fsIFile::strWriteToFile(fsCResourceName(fsStr("test.csv"), fsCResourceName::eRES_LOCATION_USER_SETTINGS), saveStr);
}

fsStr gfCWordSearchMinigameGenerateComponent::randomLetterGet() const
{
	fsStr letter;
	const fsS32 asciiCodeLowerCaseA(0x61);
	letter = static_cast<fsChar>(asciiCodeLowerCaseA + fsNRnd::uInt32GetWithLimit(26));
	return letter;
}

void gfCWordSearchMinigameGenerateComponent::emptyTilesReplaceWithRandomLetter()
{
	for (fsS32 row = 0; row < mDimensions.y; ++row)
	{
		for (fsS32 column = 0; column < mDimensions.x; ++column)
		{
			if (mTiles[row * mDimensions.x + column] == mPlaceHolderTile)
			{
				mTiles[row * mDimensions.x + column] = randomLetterGet();
			}
		}
	}
}

std::vector<fsS32> gfCWordSearchMinigameGenerateComponent::randommIndicesGet(fsS32 pMaxIndex) const
{
	std::vector<fsS32> indices;
	for (fsS32 index = 0; index < pMaxIndex; ++index)
	{
		indices.push_back(index);
	}
	std::shuffle(std::begin(indices), std::end(indices), fsNRnd::randomDeviceGet());
	return indices;
}

fsBool gfCWordSearchMinigameGenerateComponent::wordAddHorizontal(const fsStr& pWord)
{
	for (fsS32 row : randommIndicesGet(mDimensions.y))<--- Consider using std::any_of algorithm instead of a raw loop.
	{
		gfSRow rowCheck(this, mDimensions.x, row, pWord, 0 == fsNRnd::uInt32GetWithLimit(3));
		fsS32 column = rowCheck.startIndexFind();
		if (-1 != column)
		{
			rowCheck.copyToGrid(column, row);
			return true;
		}
	}
	return false;
}

fsBool gfCWordSearchMinigameGenerateComponent::wordAddVertical(const fsStr& pWord)
{
	for (fsS32 column : randommIndicesGet(mDimensions.x))<--- Consider using std::any_of algorithm instead of a raw loop.
	{
		gfSColumn columnCheck(this, mDimensions.y, column, pWord, 0 == fsNRnd::uInt32GetWithLimit(3));
		fsS32 row = columnCheck.startIndexFind();
		if (-1 != row)
		{
			columnCheck.copyToGrid(column, row);
			return true;
		}
	}
	return false;
}

fsBool gfCWordSearchMinigameGenerateComponent::wordAddDiagonalTLBR(const fsStr& pWord)
{
	for (fsS32 row : randommIndicesGet(mDimensions.y))<--- Consider using std::any_of algorithm instead of a raw loop.
	{
		gfSDiagonalTLBR diagonalCheck(this, mDimensions.x, row, pWord, 0 == fsNRnd::uInt32GetWithLimit(3));
		fsS32 column = diagonalCheck.startIndexFind();
		if (-1 != column)
		{
			diagonalCheck.copyToGrid(column, row);
			return true;
		}
	}
	return false;
}

fsBool gfCWordSearchMinigameGenerateComponent::wordAddDiagonalBLTR(const fsStr& pWord)
{
	for (fsS32 row : randommIndicesGet(mDimensions.y))<--- Consider using std::any_of algorithm instead of a raw loop.
	{
		gfSDiagonalBLTR diagonalCheck(this, mDimensions.x, row, pWord, 0 == fsNRnd::uInt32GetWithLimit(3));
		fsS32 column = diagonalCheck.startIndexFind();
		if (-1 != column)
		{
			diagonalCheck.copyToGrid(column, row);
			return true;
		}
	}
	return false;
}

fsBool gfCWordSearchMinigameGenerateComponent::wordsAdd(std::vector<fsStr>& pWords)<--- Parameter 'pWords' can be declared as reference to const
{
	typedef fsBool (gfCWordSearchMinigameGenerateComponent::*fsTWordInsertFnc)(const fsStr& pWord);
	std::vector<fsTWordInsertFnc> wordInsertFncs;

	wordInsertFncs.push_back(&gfCWordSearchMinigameGenerateComponent::wordAddHorizontal);
	wordInsertFncs.push_back(&gfCWordSearchMinigameGenerateComponent::wordAddDiagonalTLBR);
	wordInsertFncs.push_back(&gfCWordSearchMinigameGenerateComponent::wordAddDiagonalBLTR);
	wordInsertFncs.push_back(&gfCWordSearchMinigameGenerateComponent::wordAddVertical);

	for (fsStr word : pWords)
	{
		std::shuffle(std::begin(wordInsertFncs), std::end(wordInsertFncs), std::default_random_engine{});

		for (fsTWordInsertFnc wordInsertFnc : wordInsertFncs)
		{
			if (((this)->*(wordInsertFnc))(word))
			{
				break;
			}
		}
	}
	return true;
}

void gfCWordSearchMinigameGenerateComponent::gridCreate()
{
	for (fsS32 row = 0; row < mDimensions.y; ++row)
	{
		for (fsS32 column = 0; column < mDimensions.x; ++column)
		{
			mTiles.push_back(mPlaceHolderTile);
		}
	}
}

void gfCWordSearchMinigameGenerateComponent::wordListSortLongestFirst(std::vector<fsStr>& pWords)
{
	// Word-search entries are treated as English/ASCII words for placement and tile counts.
	std::sort(pWords.begin(), pWords.end(),
	          [](fsStr wordA, fsStr wordB) { return wordB.lengthGet() < wordA.lengthGet(); });
}

fsBool gfCWordSearchMinigameGenerateComponent::generate(fsStr const& pTileSet, fsStr const& pHilight,
                                                        fsPoint const& pDimensions, fsPoint const& pOffset,
                                                        std::vector<fsStr> pWords)
{
	mTileSet = pTileSet;
	mHilight = pHilight;
	mDimensions = pDimensions;
	mWords = pWords;
	mOffest = pOffset;

	gridCreate();
	wordListSortLongestFirst(pWords);
	wordsAdd(mWords);
	emptyTilesReplaceWithRandomLetter();
	fileSave();
	return true;
}

fsIComponent* gfCWordSearchMinigameGenerateComponent::create(fsCEntity* pParent)
{
	return new gfCWordSearchMinigameGenerateComponent(pParent);
}

gfCWordSearchMinigameGenerateComponent::gfCWordSearchMinigameGenerateComponent(fsCEntity* const pParent) :
	fsIComponent(pParent),
	mDimensions{0, 0}
{
}

gfCWordSearchMinigameGenerateComponent::~gfCWordSearchMinigameGenerateComponent()
{
}

void gfCWordSearchMinigameGenerateComponent::gfSGridDirection::copyToGrid(fsS32 pColumn, fsS32 pRow) const
{
	// Grid placement is byte-per-letter by design for the authored word lists used here.
	for (fsS32 currChar = 0; currChar < mWord.lengthGet(); ++currChar)
	{
		doCopyToGrid(pColumn, pRow, currChar);
	}
}

fsS32 gfCWordSearchMinigameGenerateComponent::gfSGridDirection::startIndexFind()
{
	fsS32 suitableTileCount = 0;
	for (fsS32 index = dimensionLimitGet() - 1; index >= 0; --index)
	{
		if (legalEntry(index))
		{
			if (++suitableTileCount == wordLengthGet())
			{
				return index;
			}
		}
		else
		{
			suitableTileCount = -1;
		}
	}
	return -1;
}


fsS32 gfCWordSearchMinigameGenerateComponent::gfSGridDirection::wordIndexGet(fsS32 pIndex) const
{
	if (mReverse)
	{
		return mWord.lengthGet() - 1 - pIndex;
	}
	return pIndex;
}

fsBool gfCWordSearchMinigameGenerateComponent::gfSColumn::legalEntry(fsS32 pIndex) const
{
	return mParent->mTiles[pIndex * mParent->mDimensions.x + mDimensionIndex] == mParent->mPlaceHolderTile;
}

void gfCWordSearchMinigameGenerateComponent::gfSColumn::doCopyToGrid(fsS32 pColumn, fsS32 pRow, fsS32 pCurrChar) const
{
	mParent->mTiles[(pRow + pCurrChar) * mParent->mDimensions.x + pColumn] = mWord[wordIndexGet(pCurrChar)];
}

fsBool gfCWordSearchMinigameGenerateComponent::gfSRow::legalEntry(fsS32 pIndex) const
{
	return mParent->mTiles[mDimensionIndex * mParent->mDimensions.x + pIndex] == mParent->mPlaceHolderTile;
}

void gfCWordSearchMinigameGenerateComponent::gfSRow::doCopyToGrid(fsS32 pColumn, fsS32 pRow, fsS32 pCurrChar) const
{
	mParent->mTiles[pRow * mParent->mDimensions.x + pColumn + pCurrChar] = mWord[wordIndexGet(pCurrChar)];
}

fsBool gfCWordSearchMinigameGenerateComponent::gfSDiagonalBLTR::legalColRowEntry(fsS32 pColumn, fsS32 pRow) const
{
	return mParent->mTiles[pRow * mParent->mDimensions.x + pColumn] == mParent->mPlaceHolderTile;
}

void gfCWordSearchMinigameGenerateComponent::gfSDiagonalBLTR::doCopyToGrid(
	fsS32 pColumn, fsS32 pRow, fsS32 pCurrChar) const
{
	mParent->mTiles[(pRow + (mWord.lengthGet() - 1 - pCurrChar)) * mParent->mDimensions.x + pColumn + pCurrChar] = mWord
		[wordIndexGet(pCurrChar)];
}

fsS32 gfCWordSearchMinigameGenerateComponent::gfSDiagonalBLTR::startIndexFind()
{
	fsS32 suitableTileCount = 0;
	for (fsS32 column = dimensionLimitGet() - 1, row = mDimensionIndex; column >= 0 && row < mDimensionLimit; --column,
	     ++row)
	{
		if (legalColRowEntry(column, row))
		{
			if (++suitableTileCount == wordLengthGet())
			{
				return column;
			}
		}
		else
		{
			suitableTileCount = 0;
			row = mDimensionIndex - 1;
		}
	}
	return -1;
}

fsBool gfCWordSearchMinigameGenerateComponent::gfSDiagonalTLBR::legalColRowEntry(fsS32 pColumn, fsS32 pRow) const
{
	return mParent->mTiles[pRow * mParent->mDimensions.x + pColumn] == mParent->mPlaceHolderTile;
}

void gfCWordSearchMinigameGenerateComponent::gfSDiagonalTLBR::doCopyToGrid(
	fsS32 pColumn, fsS32 pRow, fsS32 pCurrChar) const
{
	mParent->mTiles[(pRow + pCurrChar) * mParent->mDimensions.x + pColumn + pCurrChar] = mWord[wordIndexGet(pCurrChar)];
}

fsS32 gfCWordSearchMinigameGenerateComponent::gfSDiagonalTLBR::startIndexFind()
{
	fsS32 suitableTileCount = 0;
	for (fsS32 column = dimensionLimitGet() - 1, row = mDimensionIndex + wordLengthGet() - 1; column >= 0 && row >= 0;
	     --column, --row)
	{
		if (legalColRowEntry(column, row))
		{
			if (++suitableTileCount == wordLengthGet())
			{
				return column;
			}
		}
		else
		{
			suitableTileCount = 0;
			row = mDimensionIndex + wordLengthGet();
		}
	}
	return -1;
}