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
|
bool WordFinder::parseWord(string line, string& word)
{
int start, end;
start = line.find('=');
end = line.find_first_of(" #\t\n");
int len1 = end - start - 1;
word = line.substr(start+1,len1);
if (word.size()>1) return true;
else
{
cout << "Word is too small to play with: \'" << word << "\'" << endl;
return false;
}
}
bool WordFinder::parseSize(string line, int& row, int& col)
{
int size_pos[3]={ 0 };
string strRow, strCol;
size_pos[0] = line.find('='); // indexes
size_pos[1] = line.find(',');
size_pos[2] = line.find_first_of(" #\t\n");
int len1 = size_pos[1] - size_pos[0] - 1;
strRow = line.substr(size_pos[0]+1,len1);
int len2 = size_pos[2] - size_pos[1] - 1;
strCol = line.substr(size_pos[1]+1,len2);
//*
//*/
if (gotInteger(strRow,row))
{
if (gotInteger(strCol,col))
{
if (row > 1 && col > 1)
{
return true;
}
else
{
cout << "Puzzle size is too small to play with: "
<< row << " * " << col << endl;
return false; // row or col < 2
}
}
else
{
cout << "Cannot parse Column number." << endl;
return false; // col is not an int
}
}
else
{
cout << "Cannot parse Row number." << endl;
return false; // row is not an int
}
} // end parseSize();
void WordFinder::convert2Upper(string& str)
{
for (unsigned i=0; i<str.length(); i++)
str[i]=toupper(str[i]);
}
bool WordFinder::gotInteger(const string& str, int& i)
{
istringstream ss(str);
return ss >> i ? true : false;
}
void WordFinder::processPuzzle(Array2D& puz_arr, Array2D& solved_puz,
string word, int row, int col)
{
//*************************************//
int arr_word_size = word.size();
arr_word = new char[arr_word_size];
for (unsigned i=0; i<word.size(); i++)
arr_word[i] = word[i];
//**** no std::string or cstring from here on ****//
int north = -1;
int south = 1;
int west = -1;
int east = 1;
int straight =0;
for (int j=0; j<row; j++)
{
for (int k=0; k<col; k++)
{
// if element == word[0]
// then search in every direction
// n(-1,0), ne(-1,-1), e(0,1), se(1,1),
// s(1,0), sw(1,-1), w(0,-1), nw(-1,-1)
// for the rest of word
if (arr_word[0] == puz_arr(j,k))
{
bool gotDir=false;
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,north,straight,j,k);
if (gotDir) // NORTH
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,north,straight,j,k);
}
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,north,east,j,k);
if (gotDir) // NORTH-EAST
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,north,east,j,k);
}
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,straight,east,j,k);
if (gotDir) // EAST
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,straight,east,j,k);
}
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,south,east,j,k);
if (gotDir) // SOUTH-EAST
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,south,east,j,k);
}
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,south,straight,j,k);
if (gotDir) // SOUTH
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,south,straight,j,k);
}
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,south,west,j,k);
if (gotDir) // SOUTH-WEST
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,south,west,j,k);
}
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,straight,west,j,k);
if (gotDir) // WEST
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,straight,west,j,k);
}
gotDir = lookDirection(puz_arr,arr_word,arr_word_size,north,west,j,k);
if (gotDir) // NORTH-WEST
{
setSolvedPuzzle(solved_puz,arr_word,arr_word_size,north,west,j,k);
}
}
} // fk
} // fj
}
bool WordFinder::lookDirection(Array2D& puz_arr, char arr_word[], int arr_word_size,
int dirX, int dirY, int eleX, int eleY)
{
int row= puz_arr.getHt();
int col= puz_arr.getWd();
// don't need to start at zeroth, first char is good
// check for out of bounds, else check character.
for (int i=1; i<arr_word_size; i++)
{
if ((eleX += dirX)>row || (eleX += dirX)<0)
return false;
else if ((eleY += dirY)>col || (eleY += dirY)<0)
return false;
else
{
if (puz_arr((eleX+=dirX),(eleY+=dirY)) != arr_word[i])
return false;
}
}
return true;
}
void WordFinder::setSolvedPuzzle(Array2D& solved_puz, char arr_word[], int arr_word_size,
int dirX, int dirY, int eleX, int eleY)
{
for (int i=0; i<arr_word_size; i++)
{
solved_puz(eleX, eleY) = arr_word[i];
eleX += dirX;
eleY += dirY;
}
}
void WordFinder::outputSolvedPuzzle(Array2D& solved_puz, int row, int col)
{
for (int i=0; i<row; i++)
{
for (int j=0; j<col; j++)
cout << solved_puz(i,j);
cout << endl;
}
}
void WordFinder::resetBools(void)
{
gotSize=false;
gotWord=false;
gotPuzzle=false;
createdArr=false;
}
bool WordFinder::isComment(string str)
{
if (str == "" || str[0]=='#' || str[0]=='\n')
return true;
else return false;
}
bool WordFinder::isSize(string str)
{
if (str[0]=='S' && str[1]=='I' && str[2]=='Z' && str[3]=='E' && str[4]=='=')
return true;
else return false;
}
bool WordFinder::isSearch(string str)
{
if (str[0]=='S' && str[1]=='E' && str[2]=='A' && str[3]=='R' &&
str[4]=='C' && str[5]=='H' && str[6]=='F' && str[7]=='O' &&
str[8]=='R' && str[9]=='=')
return true;
else return false;
}
|