Thread: crossword help

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Post crossword help

    Hey, could someone look at this code and tell me what is wrong with this. Im not getting any error while compiling nor is the program giving any output.

    There is a puzzle with words hiddn in the puzzle. Besides the words there are just '+'. the C code has to pull words out of the crossword and compare it with a word list to see if the word is present in the list. if present it has to give its coordinates. Also, if the word is present in the reverse direction in the puzzle, the coordinates will be neative.

    Code:
    #define DIM 15 /* size of array*/
    #define WORDNUM	18 /* number of words in the list */
    void printwords();
    int compareWords(int value, char array[]);
    typedef struct {
    	char structword[15];
    	int row;
    	int column;
    } WORD;
    
    WORD wordList[WORDNUM];
    void readWords(void);
    
    char puzzle[DIM][DIM]={ {'E','U','N','I','T','N','O','C','+','+','+','+','+','+','+'},  
    					    {'X','+','+','+','+','+','+','+','+','+','+','+','A','+','+'}, 
    					    {'I','+','+','+','+','+','+','T','+','P','R','I','N','T','F'},  
    					    {'T','+','+','+','+','+','+','U','+','+','+','+','S','+','L'}, 
    					    {'+','W','H','I','T','E','S','P','A','C','E','+','I','+','O'},  
    					    {'T','+','+','+','+','+','+','+','+','+','+','+','+','+','A'}, 					    {'E','+','N','O','I','S','S','E','R','P','X','E','+','+','T'}, 
    					    {'S','+','+','+','+','+','+','+','+','+','+','+','+','+','+'},  
    					    {'T','N','E','M','E','R','C','N','I','+','P','O','W','E','R'}, 
    					    {'+','+','+','A','+','+','+','+','+','+','+','+','+','+','E'},  
    					    {'E','R','O','C','+','I','+','E','L','B','U','O','D','+','T'},  
    					    {'+','+','+','R','+','N','+','+','+','+','+','L','L','U','N'}, 
    					    {'+','+','+','O','+','T','L','U','A','F','E','D','+','+','I'}, 
    					    {'+','+','+','S','+','+','+','+','+','+','+','+','+','+','O'},  
    {'+','+','+','+','+','S','Y','M','B','O','L','I','C','+','P'}}; 
    
    int main()
    {
    
    
    
          char array[DIM] ;
          int x, y, w;
          int type=0;
          int c = 0, r = 0;
          readWords();
          int numb =0;
          int count;
    
        for (count = 0; count< WORDNUM; count++)
        {
    	printf("%s \n",wordList[count].structword);
        }
    
    
    
        /* horizontal comparison*/
    	
        for (x=1; x<=15; x++)
        {
    	for (y=1; y<=15; y++)
    	{
    	        w=0;
    	        /*printf("%c",puzzle[x][y]);*/
    	       if (puzzle[x][y]!= '+')
    	       {
    		r = y;
    		while (puzzle[x][y]!= '+')
    		{
    		        array[w] = puzzle[x][y];
    		        printf("%c",array[w]);
    		        w++;
    		        y++;
    		}
    
    		array[w] = '\0';
    		result = compareWords(r, array);
    		for (numb=0; numb < WORDNUM; numb++)
    		{
    		if (strcmp(wordList[numb].structword , array)  
                                                                                              == 0)
    		{
    						  
                                        wordList[numb].row = result;
    		    wordList[numb].column = x;
    		}
    
    	               }
    	     y = y + strlen(array);
    	    /*printf("-%d-",y);*/
    
                     }
                    else
                    continue;
    
              }
        }
    
    	/* vertical comparison*/
        for (y=1; y<=15;y++)
        {
        for (x=1; x<=15; x++)
        {
    	w=0;
    	if (puzzle[x][y]!= '+')
    	{
    	       c = x;
    
    	       while (puzzle[x][y]!='+')
    	       {
    		array[w] = puzzle[x][y];
    		w++;
    		x++;
    
    	       }
    
    	      array[w] = '\0';
    	      result = compareWords(c, array);
    	      for (numb=0; numb < WORDNUM; numb++)
    	     {
    	        if (strcmp(wordList[numb].structword , array) == 0)
    	        {
    		wordList[numb].row = y;
    		wordList[numb].column = type;
    	        }
    
    	    }
    
    	    x = x + strlen(array);
    
    	}
           }
       }
    printwords ();
    
    
    
    
    return 0;
    
    }
    
    
    void printwords()
    {
            int linecount;
            for (linecount=0; linecount < WORDNUM; linecount++)
            {
                 printf("\n %s (%d,%d)",wordList[linecount].structword,wordList[linecount].row,wordList[linecount].column);
             }
    }
    
    
    int compareWords(int value, char array[])
    {
    	int linenum;
    	int l = strlen(array);
    
     	for( linenum = 0; linenum < WORDNUM; linenum++)
     	{
    		if ( wordList[linenum].structword[0] == array[0])
    			return value;
    		else if ( wordList[linenum].structword[0] == array[l - 1] )
    			return -1 * (value + l);
    		else
    		{
    			++linenum;
    
    		}
    	}
    
    return 0;
    
    }
    
    void readWords(void)
    {
    	 int count = 0;
    	 char word[15];
    	 while (scanf("%s", word)!=EOF)
    	 {
    		strcpy(wordList[count].structword, word);
    		count++;
    		
    	 }
    
    }

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    169
    Why not printing the puzzle like this:
    Code:
    for (y=0 ; y<DIM ; ++y) {
    
        for (x=0 ; x<DIM ; x++)
            printf("%c ", puzzle[x][y]);
        printf("\n");
    }
    btw, I suggest cleanning your code up before posting in the forum, makes it helluva easier to make out what youre trying to do.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    151

    Arrow

    The way I see it, the compiler doesnt proceed after readingfrom the external word list file and copying its contents into the array of structures. I could print the contents of the structures if I put a print command. But nothing happens after that.

    Its difficult to format (indent tabs, etc) the code in this Forum, its displays in a different format when I post a codee.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int l = strlen(array);
    strlen() returns a size_t, and you might want to consider another name for that variable. l (lowercase L, 12th letter in the alphabet) looks too much like 1 (digit one) in my opinion.

    [edit]
    And declaring variables in the middle of blocks is C++/C99:
    Code:
    int main()
    {
    
    
    
          char array[DIM] ;
          int x, y, w;
          int type=0;
          int c = 0, r = 0;
          readWords();
          int numb =0;
          int count;
    [/edit]
    Last edited by dwks; 06-10-2006 at 02:00 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crossword Puzzle Program
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 07-31-2006, 11:08 PM
  2. Crossword Puzzle
    By darcbleu in forum Game Programming
    Replies: 0
    Last Post: 01-14-2002, 01:40 AM