C: Word Search Solver

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

C: Word Search Solver

Post by nigma »

Hey,

One of my current projects is writing a word search solver. I'm going to first write one in C, and then if things work out I may do one in PHP as well. I was wondering if anyone else has written one themselves and would be willing to talk, maybe even help me out when I get a working the first version working?

Also, there are a few questions I have about writing user interfaces in C that I would love to ask someone who would be interested in listening and maybe even responding :)?

Thanks,
Kris
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Although I haven't built a word solver, it sounds relatively simple... so go ahead and ask the C questions, I'm sure a few of us could chime in. :)
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Well, it's not necessarily difficult, but it is taking me a while to get this one fragment of code to work. I want to fiddle with it some more before I ask any questions here, but when I do get a version done I'd love some help designing a cool console interface.

Thanks feyd.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Hey, if anyone would be into checking out the code and giving me some pointers I'll post my code below. Also, if anyone would like to write the same program differently, or has written the same program in a non-similar way then i'd love to see how they went about doing things.

wsolver.c

Code: Select all

#include <stdio.h>
#define MAXROW 51
#define MAXCOL 51

int countA(char *cp);

int main (void)
{
	/* VARIABLE DECLARATIONS		*/
	int idx;
	int end;
	int totalCols;
	int totalRows;
	int cCol;
	int cRow;
	int totalLetters;

	char wordToFind[30];
	char crossword[MAXROW][MAXCOL];
	char crosswordFile[]	= "c:/crossword.txt";
	char answerFile[]			= "c:/answerfile.txt";
	
	FILE *fp;



	/* VARIABLE INITILIZATION		*/
	totalCols	= 0;
	totalRows	= 0;
	cCol			= 0;
	cRow			= 0;
	end				= 0;
	

	/* READ CROSSWORD PUZZLE		*/
	fp = fopen(crosswordFile, "r");
	if (fp != NULL)
	{
		while (!feof(fp))
		{
			fgets(crossword[cRow], MAXCOL, fp);
			cRow++;
		}
		fclose(fp);
	}
	else
	{
		fprintf(stderr, "\nUnable to read crossword file\n");
	}



	/* TALLY TOTAL COLS & ROWS & WORDS	*/
	for (idx=0;crossword[0][idx]!=NULL;idx++);
	totalCols	= idx-1;	// the newline column will not be counted when tallying number of cols
	totalRows	= cRow;
	

	/* GET WORD TO FIND				*/
	while (!end)
	{		
		printf("\nWord To Find (Ctrl+C to quit): ");
		if (scanf("%s",&wordToFind))
		{			
			char *cp	= wordToFind;
			totalLetters= countA(wordToFind);

			fp = fopen(answerFile, "a");
			for (cRow=0;cRow<totalRows;cRow++)
			{
				for (cCol=0;cCol<totalCols;cCol++)
				{
					// Check for vertical matches going down
					idx=0;
					while (*(cp+idx)==crossword[cRow+idx][cCol] && idx<totalLetters && 
						totalRows-idx>=totalLetters-idx) { idx++; }
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Down\n",wordToFind,cRow,cCol);
						printf("Vert-Down (%d,%d)\n",cRow,cCol);
					}

					idx=0;
					while (*(cp+idx)==crossword[cRow-idx][cCol] && idx<totalLetters && 
						totalRows-idx>=totalLetters-idx) { idx++; }
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Up\n",wordToFind,cRow,cCol);
						printf("Vert-Up (%d,%d)\n",cRow,cCol);
					}

					// Check for horizontal matches going right
					idx=0;
					while (*(cp+idx)==crossword[cRow][cCol+idx] && idx<totalLetters &&
						totalCols-idx>=totalLetters-idx) { idx++;}
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Right\n",wordToFind,cRow,cCol);
						printf("Horiz-Right (%d,%d)\n",cRow,cCol);
					}

					// Check for horizontal matches going left
					idx=0;
					while (*(cp+idx)==crossword[cRow][cCol-idx] && idx<totalLetters &&
						totalCols-idx>=totalLetters-idx) { idx++; }
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Left\n",wordToFind,cRow,cCol);
						printf("Horiz-Left (%d,%d)\n",cRow,cCol);
					}

					// Check for diagonal matches going down and right
					idx=0;
					while (*(cp+idx)==crossword[cRow+idx][cCol+idx] && idx<totalLetters &&
						totalCols-idx>=totalLetters-idx) { idx++; }
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Down Right\n",wordToFind,cRow,cCol);
						printf("Diag going down and right (%d,%d)\n",cRow,cCol);
					}

					// Check for diagonal matches going down and left
					idx=0;
					while (*(cp+idx)==crossword[cRow+idx][cCol-idx] && idx<totalLetters &&
						totalCols-idx>=totalLetters-idx) { idx++; }
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Down Up\n",wordToFind,cRow,cCol);
						printf("Diag going down and left (%d,%d)\n",cRow,cCol);
					}

					// Check for diagonal matches going up and right
					idx=0;
					while (*(cp+idx)==crossword[cRow-idx][cCol+idx] && idx<totalLetters &&
						totalCols-idx>=totalLetters-idx) { idx++; }
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Up Right\n",wordToFind,cRow,cCol);
						printf("Diag going up and right (%d,%d)\n",cRow,cCol);
					}

					// Check for diagonal matches going up and left
					idx=0;
					while (*(cp+idx)==crossword[cRow-idx][cCol-idx] && idx<totalLetters &&
						totalCols-idx>=totalLetters-idx) { idx++; }
					if (idx==totalLetters) {
						fprintf(fp,"%s (%d, %d) Up Left\n",wordToFind,cRow,cCol);
						printf("Diag going up and left (%d,%d)\n",cRow,cCol);
					}
				} // End column loop
			} // End row loop

			fclose(fp);
		} // End valid input check
		else
		{
			printf("Invalid input entered.\n");
			end = 1;
		}
	} // End while !end loop


	return 0;
}

int countA(char *cp)
{
	for(int idx=0;*(cp+idx)!=NULL;idx++);
	return idx;
}
edit patrikG: added

Code: Select all

- it's dead easy!
Post Reply