Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active January 29, 2025 13:07
Show Gist options
  • Save juanfal/b4f5e72a12b0549e2014b7cebc5b5b81 to your computer and use it in GitHub Desktop.
Save juanfal/b4f5e72a12b0549e2014b7cebc5b5b81 to your computer and use it in GitHub Desktop.
Word Puzzle
// wordPuzzle.cpp
// juanfc 2022-12-07
// juanfc 22/1/02-2012-02-07-2015-02-02
// Program to find words in a word search game
// https://gist.github.com/juanfal/b4f5e72a12b0549e2014b7cebc5b5b81
#include <iostream>
#include <array>
#include <iomanip>
using namespace std;
int const MAX=10;
enum TDir {N, NE, E, SE, S, SW, W, NW};
typedef array <array<char,MAX>,MAX> TBoard;
int main()
{
void test(TBoard, string);
void fillBoard (TBoard&);
void printBoard(TBoard);
TBoard board;
fillBoard(board);
printBoard(board);
string s = "p";
test(board, s);
// to check an existing "word"
s = "";
s += board[2][2];
s += board[3][2];
s += board[4][2];
s += board[5][2];
test(board, s);
// most probably wont
test(board, "3a3");
return 0;
}
void test(TBoard board, string s)
{
bool found(TBoard, string, TDir&, int&, int&);
void printFound(TBoard, TDir, int, int, string);
int x, y;
TDir dir;
if (found(board, s, dir, x, y))
printFound(board, dir, x, y, s);
else
cout << s << " Not found" << endl;
}
bool searchWord(TBoard board, int x, int y, string b, TDir dir)
{
bool inside(int, int);
int dx, dy;
if (dir == W or dir == E) dx = 0;
if (dir == N or dir == S) dy = 0;
if (dir == N or dir == NE or dir == NW) dx = -1;
if (dir == S or dir == SE or dir == SW) dx = 1;
if (dir == E or dir == NE or dir == SE) dy = 1;
if (dir == W or dir == NW or dir == SW) dy = -1;
int i=0, xx=x, yy=y;
while ( inside(xx, yy) and i < b.size() and b[i] == board[xx][yy] ) {
xx += dx;
yy += dy;
++i;
}
return i == b.size();
} // searchWord
void fillBoard (TBoard& a)
{
int i,j;
srand(time(0));
for (j=0; j < MAX; j++)
for (i=0; i < MAX; i++)
// "random" number between 0 and RAND_MAX
a[i][j]= rand() % ('z'-'a'+1) + 'a';
} // fillBoard
void printBoard (TBoard a)
{
int i, j;
cout << " ";
for (j=0; j<MAX; j++)
cout << setw(2) << j;
cout << endl;
for (i=0; i<MAX; i++) {
cout << setw(3) << i;
for (j=0; j<MAX; j++)
cout << setw(2) <<a[i][j];
cout << endl;
}
} // printBoard
bool found(TBoard board, string theString, TDir& dir, int& x, int& y)
{
bool searchWord(TBoard, int, int, string, TDir);
bool ok = false;
y = 0;
while (y<MAX and not ok) {
x = 0;
while (x<MAX and not ok) {
dir = N;
while (dir<=NW and not ok)
if (searchWord(board, x, y, theString, dir))
ok = true;
else
dir = TDir((int)dir + 1);
++x;
}
++y;
}
if (ok) {
--x; --y;
}
return ok;
}
void printFound(TBoard board, TDir sen, int x, int y, string what)
{
cout << what << " found in coords: " << x << ", " << y <<
" to ";
switch (sen) {
case N: cout << "^ North"; break;
case S: cout << "v South"; break;
case E: cout << "> East"; break;
case W: cout << "< West"; break;
case NE: cout << "/^ NorthEast"; break;
case NW: cout << "\\^ NorthWest"; break;
case SE: cout << "\\v SouthEast"; break;
case SW: cout << "/v SouthWest"; break;
}
cout << endl;
}
bool inside(int x, int y)
{
return x >= 0 and x < MAX and y >= 0 and y < MAX;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment