This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardBuilderFinal.cpp
561 lines (494 loc) · 12 KB
/
BoardBuilderFinal.cpp
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//Board Builder:
//Creates playable boards to use in Scrabble Junior
//Therefore it is a program complemented by the game itself
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <random>
#include <time.h>
#include <algorithm>
using namespace std;
short int sizeofboard, count = 0;
//creates vector with all the words
void dictionaryCreator(string lang, vector<string>& words)
{
//opens dictionary for corresponding language
string dic = lang + ".txt";
ifstream file(dic);
string w;
while (getline(file, w))
{
words.push_back(w);
}
file.close();
}
//verifies if word exists in the dictionary
void wordVerifier(vector<string> words, string& Word)
{
while (find(words.begin(), words.end(), Word) == words.end())
{
cout << endl << "Word does not exist, choose another one: " << endl;
cin >> Word;
}
}
//Verifier for words vertically (s = size of board, p = pos[0])
bool sizeVerifier(int x, int y, string dir, int wordlength)
{
if ((y + wordlength) - 1 > sizeofboard&& dir == "H")
{
return false;
}
else if ((x + wordlength) - 1 > sizeofboard&& dir == "V")
{
return false;
}
else
{
return true;
}
}
//checks for intersections and verifies the places arround word
bool intersection(vector<char> WordSplit, int x, int y, vector<vector<char>> board, string pos, string dir, int wordlength) //verify if any intersection occur, and if it occurs, verify if possible
{
short int n = 0;
char h, v;
if (dir == "H")
{
if (wordlength + 1 + y < sizeofboard)
{
if (board[x][wordlength + y + 1] != 0)
{
return false;
n++;
}
}
if (y > 0)
{
if (board[x][y-1] != 0)
{
return false;
n++;
}
}
}
else if (dir == "V")
{
if (wordlength + x + 1 < sizeofboard)
{
if (board[wordlength + x + 1][y] != 0)
{
return false;
n++;
}
}
if (x > 0)
{
if (board[x - 1][y] != 0)
{
return false;
n++;
}
}
}
if (n == 0)
{
for (int i = 0; i < wordlength; i++) //Checks for intersections and if they occur and are not valid, break.
{
if (dir == "H")
{
h = board[x][y + i];
if (h != 0)
{
if (h != WordSplit[i])
{
return false;
n++;
break;
}
}
}
else if (dir == "V")
{
v = board[x + i][y];
if (v != 0)
{
if (v != WordSplit[i])
{
return false;
n++;
break;
}
}
}
}
}
if (n == 0)
{
return true;
}
}
//Inserts word in board
void insertInBoard(vector<char> WordSplit, ofstream& myfile, int x, int y, vector<vector<char>>& board, string Word, string pos, string dir, int wordlength) //Insert word in board after it has been verified
{
for (int i = 0; i < wordlength; i++)
{
if (dir == "H")
{
char r = WordSplit[i];
board[x][y + i] = r;
}
else
{
char r = WordSplit[i];
board[x + i][y] = r;
}
}
myfile << pos << " " << dir << " " << Word << "\n";
}
//Let the user creat the board manually
void manualBoard(ofstream& myfile, vector<string> words, vector<vector<char>>& board, string& pos, string& dir) //Manual board creator
{
string ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alpha = "abcdefghijklmnopqrstuvwxyz";
short int x = 0, y = 0;
string end = "yes";
unsigned short int wordcount = 0;
string Word;
while (end != "no" && wordcount < 15) //loops until no more words are asked, reasonable Maximum of words 15
{
dir = "";
x = NULL;
y = NULL;
cout << "\nWord: \n"; //Input a word, check its size, then split it into a vector
cin >> Word;
int wordlength = Word.size();
vector<char> WordSplit(Word.begin(), Word.end());
wordVerifier(words, Word);
while (dir != "H" || dir != "V") //Ask for words direction
{
cout << "\nDirection of Word (H or V): \n";
cin >> dir;
if (dir == "V")
{
break;
}
if (dir == "H")
{
break;
}
}
cout << "\nPosition of First letter: (in letters, ex: Aa) \n";
cin >> pos;
x = ALPHA.find(pos[0]) + 1;
y = alpha.find(pos[1]) + 1;
if (sizeVerifier(x, y, dir, wordlength) == false)
{
cout << "Word too long";
continue;
}
if (intersection(WordSplit, x, y, board, pos, dir, wordlength))
{
insertInBoard(WordSplit, myfile, x, y, board, Word, pos, dir, wordlength);
}
else
{
continue;
}
wordcount++;
Word = "";
cout << "\nYou already have " << wordcount << " words in your board\n" << endl;
cout << "Do you want to add more words? (yes or no) \n"; cin >> end;
}
}
//Check if Word2 is valid and if it fits
void Wordintersecting(vector<vector<char>>& board, string& Word2, vector<string> words, string dir, int random_number, vector<char> WordSplit, int x, int y, ofstream& myfile, int& W2)
{
string ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alpha = "abcdefghijklmnopqrstuvwxyz";
string Xl, Yl; //Letter in x and Letter in Y
string dir2, pos2;
vector<char> WordSplit2(Word2.begin(), Word2.end());
short int x2 = -1;
short int y2 = -1;
bool F = true;
if (dir == "H") //Word2 needs to be perpendicular to Word1 so it intersects
{
dir2 = "V";
}
else
{
dir2 = "H";
}
int check = 0;
for (int i = 0; i < WordSplit.size(); i++) //Check if Words have any letter in common
{
for (int j = 0; j < WordSplit2.size(); j++)
{
if (WordSplit[i] == WordSplit2[j])
{
if (dir2 == "H")
{
x2 = x - j;
y2 = y + i;
check++;
break;
}
else
{
x2 = x + i;
y2 = y - j;
check++;
break;
}
}
}
if (check != 0)
{
break;
}
}
//Check if starting position is possible
if ((x2 < 0 || y2 < 0 || (y2 + WordSplit2.size()) < sizeofboard) && dir2 == "H")
{
F = false;
}
else if ((x2 < 0 || y2 < 0 || (x2 + WordSplit2.size()) < sizeofboard) && dir2 == "V")
{
F = false;
}
if (F == true)
{
Xl = ALPHA[x2];
Yl = alpha[y2];
pos2 = "";
pos2.append(Xl);
pos2.append(Yl);
if (sizeVerifier(x2, y2, dir2, Word2.size()))
{
if (intersection(WordSplit2, x2, y2, board, pos2, dir2, Word2.size()) == true)
{
insertInBoard(WordSplit2, myfile, x2, y2, board, Word2, pos2, dir2, Word2.size());
W2++;
}
}
}
}
void automaticBoard(ofstream& myfile, string, vector<string> words, vector<vector<char>>& board, string& pos, string& dir)
{
string ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alpha = "abcdefghijklmnopqrstuvwxyz";
short int x = 0, y = 0, word_count = 0;
int random_number;
string RandDir = "HV", RandX, RandY;
string Word, Word2;
vector<int> x_axis, y_axis;
cout << "\nHow many words do you want in the board? (less than 15)\n"; cin >> word_count;
while (word_count < 1 || word_count > sizeofboard)
{
cout << "\nInvalid number of words; select another number:" << endl;
cin >> word_count;
}
for (int i = 0; i < sizeofboard; i++)
{
x_axis.push_back(i);
y_axis.push_back(i);
cout << x_axis[i] << endl;
}
while (word_count != 0)
{
random_number = rand() % words.size();
Word = words[random_number];
int wordlength = Word.size();
vector<char> WordSplit(Word.begin(), Word.end());
dir = "H";//RandDir[rand() % 2];
//ignore this word if it is too long
if (wordlength > (sizeofboard - 1))
{
continue;
}
int c = sizeofboard - wordlength;
//removes need to check if word fits (selects random number in range)
if (dir == "H")
{
int random_x_axis = rand() % x_axis.size();
x = x_axis[random_x_axis];
y = rand() % c;
}
else
{
x = rand() % c;
int random_y_axis = rand() % y_axis.size();
y = y_axis[random_y_axis];
}
RandX = ALPHA[x];
RandY = alpha[y];
//Create position
pos = "";
pos.append(RandX);
pos.append(RandY);
if (intersection(WordSplit, x, y, board, pos, dir, wordlength) == false)
{
continue;
}
else if (intersection(WordSplit, x, y, board, pos, dir, wordlength) == true && word_count > 1)
{
insertInBoard(WordSplit, myfile, x, y, board, Word, pos, dir, wordlength);
int T = 0;
if (dir == "H")
{
for (int i = 0; i < x_axis.size(); i++)
{
cout << x_axis[i] << endl;
}
if (x < sizeofboard - 1 && x > 0)
{
vector<int>::iterator itr = find(x_axis.begin(), x_axis.end(), x);
x_axis.erase(x_axis.begin() + distance(x_axis.begin(), itr) - 1, x_axis.begin() + distance(x_axis.begin(), itr) + 2);
T++;
}
if (x > 0 && T == 0)
{
vector<int>::iterator itr = find(x_axis.begin(), x_axis.end(), x);
x_axis.erase(x_axis.begin() - 1 + distance(x_axis.begin(), itr));
}
if (x + 1 < sizeofboard && T == 0)
{
vector<int>::iterator itr = find(x_axis.begin(), x_axis.end(), x);
x_axis.erase(x_axis.begin() + 1 + distance(x_axis.begin(), itr));
}
if (T == 0)
{
vector<int>::iterator itr = find(x_axis.begin(), x_axis.end(), x);
x_axis.erase(x_axis.begin() + distance(x_axis.begin(), itr));
}
for (int i = 0; i < x_axis.size(); i++)
{
cout << x_axis[i] << endl;
}
}
else
{
if (y < sizeofboard - 1 && y > 0)
{
vector<int>::iterator itr = find(y_axis.begin(), y_axis.end(), y);
y_axis.erase(y_axis.begin() - 1 + distance(y_axis.begin(), itr), y_axis.begin() + 2 + distance(y_axis.begin(), itr));
T++;
}
if (y > 0 && T == 0)
{
vector<int>::iterator itr = find(y_axis.begin(), y_axis.end(), y);
y_axis.erase(y_axis.begin() - 1 + distance(y_axis.begin(), itr));
}
if (y + 1 < sizeofboard && T == 0)
{
vector<int>::iterator itr = find(y_axis.begin(), y_axis.end(), y);
y_axis.erase(y_axis.begin() + 1 + distance(y_axis.begin(), itr));
}
if (T == 0)
{
vector<int>::iterator itr = find(y_axis.begin(), y_axis.end(), y);
y_axis.erase(y_axis.begin() + distance(y_axis.begin(), itr));
}
}
int RN = rand() % words.size();
Word2 = words[RN];
int W2 = 0;
Wordintersecting(board, Word2, words, dir, random_number, WordSplit, x, y, myfile, W2);
if (W2 == 0)
{
word_count--;
}
else
{
word_count -= 2;
}
}
else
{
insertInBoard(WordSplit, myfile, x, y, board, Word, pos, dir, wordlength);
word_count--;
}
}
cout << endl << "Board has been automatically created, heres a copy of it: " << endl;
}
//prints the whole board text file for user visualization
void printBoard(string nameFile)
{
ifstream print;
print.open(nameFile + ".txt");
string w;
while (getline(print, w))
{
cout << w << endl;
}
}
int main()
{
srand((unsigned)time(NULL));
short int x, y;
vector<string> words;
string l, nameFile, personalized, pos, dir;
//int sizeofboard;
vector<vector<char>> board(sizeofboard, vector<char>(sizeofboard)); //maps the whole board
//opens file that contains language choice made in Scrabble Junior
ifstream language;
language.open("lang.txt");
getline(language, l);
language.close();
cout << "language: " << l << endl;
dictionaryCreator(l, words);
int count = words.size(); //number of words in dictionary
cout << endl << count << endl;
cout << endl << "Size of the Board: " << endl;
cin >> sizeofboard;
while (sizeofboard <= 9 || sizeofboard >= 21)
{
cout << "Size of the Board is not available, try another size: (Between 10 & 20)" << endl;
cin >> sizeofboard;
}
//fills the map with zeros
for (int i = 0; i < sizeofboard; i++)
{
vector<char> filler;
for (int j = 0; j < sizeofboard; j++)
{
filler.push_back(0);
}
board.push_back(filler);
}
cout << endl << "Do you want to create the board yourself? (yes or no)" << endl;
cin >> personalized;
while (personalized != "yes")
{
if (personalized == "no")
break;
cout << "Option not available, try again: (yes or no)" << endl;
cin >> personalized;
}
//Give different to board depending if its created by the user or automatically
if (personalized == "yes")
{
nameFile = "custom_";
}
else if (personalized == "no")
{
nameFile = "random_";
}
nameFile += l;
//writes the first line in board text file
ofstream myfile;
myfile.open(nameFile + ".txt");
myfile << sizeofboard << " x " << sizeofboard << "\n";
if (personalized == "yes")
{
manualBoard(myfile, words, board, pos, dir);
}
else if (personalized == "no")
{
automaticBoard(myfile, l, words, board, pos, dir);
}
myfile.close();
printBoard(nameFile);
return 0;
}