-
Notifications
You must be signed in to change notification settings - Fork 0
/
jumble.cpp
178 lines (152 loc) · 5.01 KB
/
jumble.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
// A word search game that uses the heap to store the vector of letters. Used to learn the nuance of using the heap
// in C++.
//
// Created by Cam Cunningham on 11/17/2019.
//
#include "jumble.h"
// Error implementation
BadJumbleException::BadJumbleException(const string & m) : message(m) {}
string& BadJumbleException::what() { return message; }
//Constructor
JumblePuzzle::JumblePuzzle(const string &word, const string &difficulty) {
//Checking word legality
if(word.size() < 3 || word.size() > 10) {
throw BadJumbleException("Supplied word must be between 3 and 10 characters in length!");
}
//Want to create the 2D array of charArrayPtr with random letters, and then attempt to place the word
int difficultyMultiplier = 1;
if(difficulty == "easy") {
difficultyMultiplier = 2;
} else if (difficulty == "medium") {
difficultyMultiplier = 3;
} else if (difficulty == "hard") {
difficultyMultiplier = 4;
} else {
throw BadJumbleException("Must supply a difficulty that is either: 'easy', 'medium' or 'hard'");
}
size = word.length() * difficultyMultiplier;
//Declaring jumble
jumble = new charArrayPtr[size];
for(int i = 0; i < size; ++i)
jumble[i] = new char[size];
//Random char
char cch;
//Populating the jumble
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
//Generate a new lowercase char
cch = 'a' + rand()%26;
jumble[i][j] = cch;
}
}
//Now we need to place the string in the jumble:
//Initial coordinates of first letter
rowPos = rand() % size;
colPos = rand() % size;
bool wordPlaced = false;
//While the word is not placed, get a random direction and attempt to place the word, if it goes outside the bounds of the puzzle, generate another direction and try again
while(!wordPlaced) {
//Get new direction
direction = randomDirection();
//Try to place
for(int i = 0; i < word.size(); i++) {
if(direction == 'n') {
if(rowPos-i < 0) {
//Illegal, need to try a different direction
break;
}
jumble[rowPos-i][colPos] = word[i];
} else if (direction == 's') {
if(rowPos+i >= size) {
//Illegal, need to try a different direction
break;
}
jumble[rowPos+i][colPos] = word[i];
} else if (direction == 'e') {
if(colPos+i >= size) {
//Illegal, need to try a different direction
break;
}
jumble[rowPos][colPos+i] = word[i];
} else if (direction == 'w') {
if(colPos-i < 0) {
//Illegal, need to try a different direction
break;
}
jumble[rowPos][colPos-i] = word[i];
}
//If this is satisfied, the entire word was placed and the loop can stop
if(i == word.size() - 1) {
wordPlaced = true;
}
}
}
}
//Returns a random direction (n,s,e,w)
char JumblePuzzle::randomDirection() {
char directions[] = {'n','s','e','w'};
return directions[rand()%4];
}
//Copy Constructor:
JumblePuzzle::JumblePuzzle(const JumblePuzzle &right) {
size = right.getSize();
jumble = new charArrayPtr[size];
for(int i = 0; i < size; ++i)
jumble[i] = new char[size];
charArrayPtr* rightJumble = right.getJumble();
//Assigning each element
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
//Generate a new lowercase char
jumble[i][j] = rightJumble[i][j];
}
}
}
//Destructor
JumblePuzzle::~JumblePuzzle() {
//Free each sub-array
for(int i = 0; i < size; i++) {
delete[] jumble[i];
}
//Free the array of pointers
delete[] jumble;
}
JumblePuzzle &JumblePuzzle::operator=(const JumblePuzzle &right) {
if(this != &right) {
//Delete the old jumble
for(int i = 0; i < size; ++i) {
delete[] jumble[i];
}
//Free the array of pointers
delete[] jumble;
size = right.getSize();
charArrayPtr* rightJumble = right.getJumble();
jumble = new charArrayPtr[size];
for(int i = 0; i < size; ++i)
jumble[i] = new char[size];
//Assigning each element
for(int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
//Generate a new lowercase char
jumble[i][j] = rightJumble[i][j];
}
}
}
return *this;
}
//Accessors
int JumblePuzzle::getColPos() const {
return colPos;
}
int JumblePuzzle::getRowPos() const {
return rowPos;
}
int JumblePuzzle::getSize() const {
return size;
}
charArrayPtr* JumblePuzzle::getJumble() const {
return jumble;
}
char JumblePuzzle::getDirection() const {
return direction;
}