-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionB_20L-1361_A5.cpp
More file actions
937 lines (766 loc) · 24.3 KB
/
SectionB_20L-1361_A5.cpp
File metadata and controls
937 lines (766 loc) · 24.3 KB
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#include <iostream>
#include <string>
#include <fstream>
#include <conio.h>
#include<windows.h>
using namespace std;
//Search Functionality
void searchGrid();
void createGrid();
char* GetStringFromBuffer(char*);
void deAllocateGridMemory(char**& , const int &);
bool seachGridForWords(char*, char**, int, int, int&, int&, int&, int&);
bool searchHorizontally(char*, char**, int, int, int&, int&, int&, int&);
bool searchVertically(char*, char**, int, int, int&, int&, int&, int&);
bool searchDiagonally(char*, char**, int, int, int&, int&, int&, int&);
bool searchReverseDiagonally(char*, char**, int, int, int&, int&, int&, int&);
void outputResults(ofstream&, const int &, const int &, const int &, const int &, bool);
//Create Functionality
int highestLength(char**);
char** allocateGridMemory(int, int);
void outputGrid(char**&, const int &, string);
void fillRemainingGrid(char**& , const int);
void diagonal_topLeftToBottomRight(char**, char* );
void fillGridWithWords(char**, char**, const int, int);
void leftToRight(char**, char*, int);
void rightToLeft(char**, char*, const int &, int);
void topToBottom(char**, char*, int);
void bottomToTop(char**, char*, int, int);
bool inputFileForGridCreation(char**&, int&, int&, int&, string&, int&);
void diagonal_bottomRightToTopLeft(char**, char*, int, int);
void diagonal_topRightToBottomLeft(char**, char*, const int &);
void diagonal_bottomLeftToTopRight(char**, char*, int, int);
void main()
{
char choice;
do
{
cout << "\n\n1. Create Grid\t\t\t[Press C]\n2. Search Words from Grid\t[Press S]\n3. Quit\t\t\t\t[Press Q]\n";
choice = getch();
if(choice == 'C' || choice == 'c')
{
cout << "\n\n------------------------------------- Creating Grid ------------------------------------------------\n\n";
createGrid();
}
else if(choice == 'S' || choice == 's')
{
cout << "\n\n------------------------------------- Searching Grid ------------------------------------------------\n\n";
searchGrid();
}
else if(choice != 'C' && choice != 'c' && choice != 'S' && choice != 's' && choice != 'Q' && choice != 'q')
{
cout << "\n\nGoing to Main Menu in 3 seconds......\n";
Sleep(3000);
}
}
while(choice != 'Q' && choice != 'q');
cout << "\n\nQuitting program in 3 seconds.......\n";
Sleep(3000);
return;
}
//------------------------------------------------------------------------------Search Functionality--------------------------------------------------------
//Simple function that receives double pointers to Grid and WordList as references (we can only return one thing so we use references to avoid returning anything at all).
//Checks to make sure that file names end with ".txt", otherwise appends this to the end of the names (to prevent file handling errors.
//Loads rows, cols, number of test cases directly into the variables. Loads words into tempbuffer, uses GetStringFromBuffer function to allocate exact amount of memory on heap.
//Stores the pointer returned by GetStringFromBuffer into array of double pointers. After words have been loaded, uses a simple nested loop to populate the 2D grid from text file.
void inputFileForGridSearch(int& gridRows, int& gridCols, int& testCases, char**& testWords, char**& characterGrid, string& nameOfOutputFile)
{
char tempBuffer[30];
int gridFileLength;
string nameOfInputFile, nameOfGridFile;
cout << "Enter the name of text file that contains the grid: ";
cin >> nameOfGridFile;
cout << "Enter the name of text file to load the words: ";
cin >> nameOfInputFile;
cout << "Enter the name of Output text file where you wnat to place the results: ";
cin >> nameOfOutputFile;
if((nameOfInputFile.substr(nameOfInputFile.size() - 4, 4) != ".txt"))
{
nameOfInputFile += ".txt";
}
if((nameOfGridFile.substr(nameOfGridFile.size() - 4, 4) != ".txt"))
{
nameOfGridFile += ".txt";
}
if((nameOfOutputFile.substr(nameOfOutputFile.size() - 4, 4) != ".txt"))
{
nameOfOutputFile += ".txt";
}
ifstream inputFile(nameOfInputFile.c_str());
if(!inputFile)
{
cout << "Text file could not be loaded. Please verify its name and location.\n";
return;
}
ifstream inputGrid(nameOfGridFile.c_str());
if(!inputGrid)
{
cout << "Text file could not be loaded. Please verify its name and location.\n";
return;
}
inputFile >> gridRows >> gridCols >> testCases;
testWords = new char*[testCases];
characterGrid = new char*[gridRows];
for(int i = 0; i < testCases; i++)
{
inputFile >> tempBuffer;
testWords[i] = GetStringFromBuffer(tempBuffer);
}
for(int i = 0; i < gridRows; i++)
{
characterGrid[i] = new char[gridCols];
}
for(int i = 0; i < gridRows; i++)
{
for(int j = 0; j < gridCols; j++)
{
inputGrid >> characterGrid[i][j];
}
}
inputFile.close();
inputGrid.close();
return;
}
//"Main" create function. Acts as the bridge between multiple functions to create a grid. Functionalitites include, taking grid input, taking words input, searching grid, printing results,
//and de-allocating dynamic memory
void searchGrid()
{
string nameOfOutputFile;
bool resultOfSearch;
int gridRows, gridCols, testCases, startRow, endRow, startCol, endCol;
char** testWords = NULL;
char** characterGrid = NULL;
inputFileForGridSearch(gridRows, gridCols, testCases, testWords, characterGrid, nameOfOutputFile);
ofstream outputObj(nameOfOutputFile.c_str());
if (!outputObj)
{
cout << "Text File to store Results couldn't be created, please try again\n";
return;
}
for(int i = 0; i < testCases; i++)
{
resultOfSearch = seachGridForWords(testWords[i], characterGrid, gridRows, gridCols, startRow, endRow, startCol, endCol);
outputResults(outputObj, startRow, endRow, startCol, endCol, resultOfSearch);
}
for(int i = 0; i < testCases; i++)
{
delete[] testWords[i];
}
delete[] testWords;
deAllocateGridMemory(characterGrid, gridRows);
return;
}
//Simple driver function that takes the grid and word to search as paramemters and passes it to multiple specific functions.
//Then whatever output it receives from those functions, it passes along to the main function.
bool seachGridForWords(char* testCase, char** charGrid, int gridRows, int gridCols, int& startRow, int& endRow, int& startCol, int& endCol)
{
if(searchHorizontally(testCase, charGrid, gridRows, gridCols, startRow, endRow, startCol, endCol) == true)
{
return true;
}
else if(searchVertically(testCase, charGrid, gridRows, gridCols, startRow, endRow, startCol, endCol) == true)
{
return true;
}
else if(searchDiagonally(testCase, charGrid, gridRows, gridCols, startRow, endRow, startCol, endCol) == true)
{
return true;
}
else if(searchReverseDiagonally(testCase, charGrid, gridRows, gridCols, startRow, endRow, startCol, endCol) == true)
{
return true;
}
return false;
}
/* DESCRIPTION OF THE 4 SEARCH FUNCTIONS BELOW
Takes the grid and the word to search as paramemters, along with index/coordinate variables so we can use this information inside the output function.
It traverses the 2D grid and searches for the first occurance of initial character of search word matching. Then it starts a loop till the length of the word to check if the
rest of the alphabets are found in the grid as well. If the loop doesn't break, it means that the whole word has been found. Thus, returning true.
If the loop breaks, this means that the whole word couldn't be located. So, it contunues searching till the grid ends. Then it returns false. */
bool searchHorizontally(char* testCase, char** charGrid, int gridRows, int gridCols, int& startRow, int& endRow, int& startCol, int& endCol)
{
int lengthOfTestWord = strlen(testCase);
bool foundWord;
for(int i = 0; i < gridRows; i++)
{
for(int j = 0; j < gridCols; j++)
{
foundWord = false;
if(charGrid[i][j] == testCase[0])
{
startRow = i, startCol = j;
foundWord = true;
//------------------------------------------------------------Left To Right---------------------------------------------------------------------
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((j + k) >= 0) && ((j + k) < gridCols))
{
if(testCase[k] != charGrid[i][j + k])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow;
endCol = j + (lengthOfTestWord - 1);
return true;
}
//------------------------------------------------------------Right to Left---------------------------------------------------------------------
foundWord = true;
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((j - k) >= 0) && ((j - k) < gridCols))
{
if(testCase[k] != charGrid[i][j - k])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow;
endCol = j - (lengthOfTestWord - 1);
return true;
}
}
}
}
return false;
}
bool searchVertically(char* testCase, char** charGrid, int gridRows, int gridCols, int& startRow, int& endRow, int& startCol, int& endCol)
{
int lengthOfTestWord = strlen(testCase);
bool foundWord;
//------------------------------------------------------------Top To Bottom---------------------------------------------------------------------
for(int i = 0; i < gridRows; i++)
{
for(int j = 0; j < gridCols; j++)
{
foundWord = false;
if(charGrid[i][j] == testCase[0])
{
startRow = i, startCol = endCol = j;
foundWord = true;
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((i + k) >= 0) && ((i + k) < gridRows))
{
if(testCase[k] != charGrid[i + k][j])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow + (lengthOfTestWord - 1);
return true;
}
}
}
}
//------------------------------------------------------------Bottom To Top---------------------------------------------------------------------
for(int i = gridRows - 1; i >= 0; i--)
{
for(int j = 0; j < gridCols; j++)
{
foundWord = false;
if(charGrid[i][j] == testCase[0])
{
startRow = i, startCol = endCol = j;
foundWord = true;
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((i - k) >= 0) && ((i - k) < gridRows))
{
if(testCase[k] != charGrid[i - k][j])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow - (lengthOfTestWord - 1);
return true;
}
}
}
}
return false;
}
bool searchDiagonally(char* testCase, char** charGrid, int gridRows, int gridCols, int& startRow, int& endRow, int& startCol, int& endCol)
{
int lengthOfTestWord = strlen(testCase);
bool foundWord;
//------------------------------------------------------------Top-Left to Bottom-Right---------------------------------------------------------------------
for(int i = 0; i < gridRows; i++)
{
for(int j = 0; j < gridCols; j++)
{
foundWord = false;
if(charGrid[i][j] == testCase[0])
{
startRow = i, startCol = j;
foundWord = true;
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((i + k) >= 0) && ((i + k) < gridRows) && ((j + k) < gridCols) && ((j + k) >= 0))
{
if(testCase[k] != charGrid[i + k][j + k])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow + (lengthOfTestWord - 1);
endCol = startCol + (lengthOfTestWord - 1);
return true;
}
}
}
}
//------------------------------------------------------------Bottom-Right to Top-Left---------------------------------------------------------------------
for(int i = gridRows - 1; i >= 0; i--)
{
for(int j = gridCols - 1; j >= 0; j--)
{
foundWord = false;
if(charGrid[i][j] == testCase[0])
{
startRow = i, startCol = j;
foundWord = true;
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((i - k) >= 0) && ((i - k) < gridRows) && ((j - k) < gridCols) && ((j - k) >= 0))
{
if(testCase[k] != charGrid[i - k][j - k])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow - (lengthOfTestWord - 1);
endCol = startCol - (lengthOfTestWord - 1);
return true;
}
}
}
}
return false;
}
bool searchReverseDiagonally(char* testCase, char** charGrid, int gridRows, int gridCols, int& startRow, int& endRow, int& startCol, int& endCol)
{
int lengthOfTestWord = strlen(testCase);
bool foundWord;
for(int i = gridRows - 1; i >= 0; i--)
{
for(int j = 0; j < gridCols; j++)
{
foundWord = false;
if(charGrid[i][j] == testCase[0])
{
startRow = i, startCol = j;
//----------------------------------------------------------------------Bottom-Left to top-Right---------------------------------------------
foundWord = true;
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((i - k) >= 0) && ((i - k) < gridRows) && ((j + k) < gridCols) && ((j + k) >= 0))
{
if(testCase[k] != charGrid[i - k][j + k])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow - (lengthOfTestWord - 1);
endCol = startCol + (lengthOfTestWord - 1);
return true;
}
//----------------------------------------------------------------------Top-Right to Bottom-Left-----------------------------------------------
foundWord = true;
for(int k = 1; (k < lengthOfTestWord) && (foundWord == true); k++)
{
if(((i + k) >= 0) && ((i + k) < gridRows) && ((j - k) < gridCols) && ((j - k) >= 0))
{
if(testCase[k] != charGrid[i + k][j - k])
{
foundWord = false;
}
}
else
{
foundWord = false;
}
}
if(foundWord == true)
{
endRow = startRow + (lengthOfTestWord - 1);
endCol = startCol - (lengthOfTestWord - 1);
return true;
}
}
}
}
return false;
}
//If the main function says that the word has been found, it prints the result in correct format on console, and also outputs the information in the text file.
//Otherwise, displays the error message.
void outputResults(ofstream& outputObj, const int &startRow, const int &endRow, const int &startCol, const int &endCol, bool searchResult)
{
if(searchResult == true)
{
cout << "{" << startRow << "," << startCol << "}{" << endRow << "," << endCol << "}" << endl;
outputObj << "{" << startRow << "," << startCol << "}{" << endRow << "," << endCol << "}" << endl;
}
else
{
cout << "Not Found\n";
outputObj << "Not Found\n";
}
return;
}
//Takes a cstring. Creates a new char pointer of bufferSize + 1 (for null character). Deep Copies all elements of Buffer to the newly created pointer. Returns the Pointer.
char* GetStringFromBuffer(char* buffer)
{
int strSize = strlen(buffer);
char* dataDestination = 0;
if (strSize > 0)
{
dataDestination = new char[strSize+1];
for(int i = 0; buffer[i] != '\0'; i++)
{
dataDestination[i] = buffer[i];
}
dataDestination[strSize] = '\0';
}
return dataDestination;
}
//
void deAllocateGridMemory(char**& charGrid, const int &gridSize)
{
for(int i = 0; i < gridSize; i++)
{
delete[] charGrid[i];
}
delete[] charGrid;
return;
}
//------------------------------------------------------------------------------Create Functionality--------------------------------------------------------
//"Main" create function. Acts as the bridge between multiple functions to create a grid. Functionalitites include, taking text input, creating grid, printing grid,
//and de-allocating dynamic memory
void createGrid()
{
char** listOfWords = NULL;
char** characterGrid = NULL;
string outputFileName;
int highestLength, numberOfWords, gridRows, gridCols;
bool result = inputFileForGridCreation(listOfWords, gridRows, gridCols, numberOfWords, outputFileName, highestLength);
if(result == false)
{
return;
}
for(int i = 0; i < numberOfWords; i++)
{
cout << listOfWords[i] << endl;
}
ofstream outputObj(outputFileName.c_str());
if (!outputObj)
{
cout << "Text File to store Results couldn't be created, please try again\n";
return;
}
characterGrid = allocateGridMemory(gridRows, gridCols);
fillGridWithWords(characterGrid, listOfWords, 15, 10);
for(int i = 0; i < numberOfWords; i++)
{
delete[] listOfWords[i];
}
delete[] listOfWords;
outputGrid(characterGrid, 15, outputFileName);
deAllocateGridMemory(characterGrid, gridRows);
return;
}
//Takes the size of the grid, dynamiccaly allocated required memory on heap an dreturn the pointer of the allocated memory.
char** allocateGridMemory(int rows, int columns)
{
char** charGrid = new char*[rows];
for(int i = 0; i < rows; i++)
{
charGrid[i] = new char[columns];
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
charGrid[i][j] = '*';
}
}
return charGrid;
}
//Simple function that loads each Word to be placed inside the puzzle and stores the word in a double pointer. Uses GetSTringFromBuffer to ensure that not a single byte is wasted.
bool inputFileForGridCreation(char**& listOfWords, int& gridRows, int& gridCols, int& numberOfWords, string& nameOfOutputFile, int& longestWord)
{
string nameOfInputFile;
cout << "Enter the name of text file to load the words: ";
cin >> nameOfInputFile;
if((nameOfInputFile.substr(nameOfInputFile.size() - 4, 4) != ".txt"))
{
nameOfInputFile += ".txt";
}
ifstream inputObject(nameOfInputFile.c_str());
if(!inputObject)
{
cout << "Text file could not be loaded. Please verify its name and location.\n";
return false;
}
cout<< "Enter number of Words in the file: ";
cin >> numberOfWords;
if(numberOfWords <= 0)
{
cout << "Number of words must be greater than 0\n\n";
return false;
}
cout << "Enter number of Rows you want in the grid: ";
cin >> gridRows;
if(gridRows <= 0)
{
cout << "Grid Rows must be greater than 0\n\n";
return false;
}
cout << "Enter number of Columns you want in the grid: ";
cin >> gridCols;
if(gridCols <= 0)
{
cout << "Grid Columns must be greater than 0\n\n";
return false;
}
cout << "Enter the name of Output text file where you want to place the grid: ";
cin >> nameOfOutputFile;
if((nameOfOutputFile.substr(nameOfOutputFile.size() - 4, 4) != ".txt"))
{
nameOfOutputFile += ".txt";
}
char tempBuffer[40];
listOfWords = new char*[numberOfWords];
for(int i = 0; i < numberOfWords; i++)
{
inputObject >> tempBuffer;
listOfWords[i] = new char;
listOfWords[i] = GetStringFromBuffer(tempBuffer);
}
longestWord = highestLength(listOfWords);
if(longestWord >= gridRows || longestWord >= gridCols)
{
cout << "Grid cannot be formed, press any key to return to the home screen: \n\n";
getch();
return false;
}
inputObject.close();
return true;
}
//Simple function that prints the grid in correct format on console, and also outputs the information in the text file.
//Displays the error message if some error occurs.
void outputGrid(char**& grid, const int &gridSize, string outputFileName)
{
ofstream outputObj(outputFileName.c_str());
if(!outputObj.is_open())
{
cout << "Output file could NOT be created for some reason!\n";
}
else
{
for(int i = 0; i < gridSize; i++)
{
for(int j = 0; j < gridSize; j++)
{
outputObj << grid[i][j] << " ";
cout << grid[i][j] << " ";
}
outputObj << endl;
cout << endl;
}
outputObj.close();
}
return;
}
//Main Driver function that passes individual words to different functions so they can add words in specific arrangements inside the Grid.
//Fills the remaining spaces with Random Characters.
void fillGridWithWords(char** charGrid, char** listOfWords, const int gridSize, int numberOfWords)
{
for(int i = 0; i < numberOfWords; i++)
{
switch (i)
{
case 0:
leftToRight(charGrid, listOfWords[0], 14);
break;
case 1:
diagonal_bottomLeftToTopRight(charGrid, listOfWords[1], 14, 4);
break;
case 2:
rightToLeft(charGrid,listOfWords[2], gridSize, 12);
break;
case 3:
rightToLeft(charGrid,listOfWords[3], gridSize, 11);
break;
case 4:
bottomToTop(charGrid, listOfWords[4], 12, 0);
break;
case 5:
bottomToTop(charGrid, listOfWords[5], 12, 1);
break;
case 6:
topToBottom(charGrid, listOfWords[6], 14);
break;
case 7:
topToBottom(charGrid, listOfWords[7], 13);
break;
case 8:
diagonal_bottomRightToTopLeft(charGrid, listOfWords[8], 9, 12);
break;
case 9:
diagonal_topLeftToBottomRight(charGrid, listOfWords[9]);
break;
default:
break;
}
}
fillRemainingGrid(charGrid, gridSize);
return;
}
/* DESCRIPTION FOR THE 8 FUNCTIONS BELOW
These functions take a single word and starting position for the word, they then populate the word's characters inside the grid in specific patterns */
void leftToRight(char** charGrid, char* wordToPlace, int rowCounter)
{
int wordSize = strlen(wordToPlace);
for(int i = 0; i < wordSize; i++)
{
charGrid[rowCounter][i] = wordToPlace[i];
}
return;
}
void rightToLeft(char** charGrid, char* wordToPlace, const int &gridSize, int rowCounter)
{
int wordSize = strlen(wordToPlace);
for(int i = 0; i < wordSize; i++)
{
charGrid[rowCounter][(gridSize - i) - 1] = wordToPlace[i];
}
return;
}
void topToBottom(char** charGrid, char* wordToPlace, int colCounter)
{
int wordSize = strlen(wordToPlace);
for(int i = 0; i < wordSize; i++)
{
charGrid[i][colCounter] = wordToPlace[i];
}
}
void bottomToTop(char** charGrid, char* wordToPlace, int startRow, int colCounter)
{
int wordSize = strlen(wordToPlace);
for(int i = startRow, counter = 0; counter < wordSize; i--, counter++)
{
charGrid[i][colCounter] = wordToPlace[counter];
}
return;
}
void diagonal_topLeftToBottomRight(char** charGrid, char* wordToPlace)
{
int wordSize = strlen(wordToPlace);
for(int i = 0; i < wordSize; i++)
{
charGrid[i][i] = wordToPlace[i];
}
return;
}
void diagonal_bottomRightToTopLeft(char** charGrid, char* wordToPlace, int startRow, int startCol)
{
int wordSize = strlen(wordToPlace);
for(int counter = 0; counter < wordSize; counter++)
{
charGrid[startRow - counter][startCol - counter] = wordToPlace[counter];
}
return;
}
void diagonal_topRightToBottomLeft(char** charGrid, char* wordToPlace, const int &gridSize)
{
int wordSize = strlen(wordToPlace);
for(int i = 0; i < wordSize; i++)
{
}
}
void diagonal_bottomLeftToTopRight(char** charGrid, char* wordToPlace, int startRow, int startCol)
{
int wordSize = strlen(wordToPlace);
for(int counter = 0; counter < wordSize; counter++)
{
charGrid[startRow - counter][startCol + counter] = wordToPlace[counter];
}
return;
}
//Takes the grid as reference. Wherever it encounters an asterisk (*), it generates a random Capital alphabet andd places it there.
void fillRemainingGrid(char**& charGrid, const int sizeOfGrid)
{
for(int i = 0; i < sizeOfGrid; i++)
{
for(int j = 0; j < sizeOfGrid; j++)
{
if(charGrid[i][j] == '*')
{
charGrid[i][j] = char(rand()% 26 + 65);
}
}
}
}
//Traverses the list of words and finds the length of the longest word. Returns that length.
int highestLength(char** wordList)
{
int maxLength = 0;
for(int i = 0; i < 10; i++)
{
if(strlen(wordList[i]) > maxLength)
{
maxLength = strlen(wordList[i]);
}
}
return maxLength;
}