Advertisement
Guest User

Really Terrible Word Search Creation Program

a guest
Feb 3rd, 2014
3,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.73 KB | None | 0 0
  1. //2014 C. Lohr - I hereby place the following code into the public domain.
  2. //You can use it for whatever you want or modify it however you want.
  3. //It's probably riddled with bugs, so I can't promise it'll work for anything.
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <math.h>
  9.  
  10. char * board;
  11. const char * word;
  12. const char * letters;
  13. int letterslen;
  14. int wordlen;
  15. int w, h;
  16. int hits = 0;
  17.  
  18. int CheckPlace( int x, int y )
  19. {
  20.     int i;
  21.  
  22.     if( x < 0 || y < 0 || x>=w || y >= h ) return 0;
  23.  
  24.     //check up.
  25.     for( i = 0; i < wordlen; i++ )
  26.     {
  27.         if( y-i < 0 ) break;
  28.         char c = board[x+(y-i)*w];
  29.         if( c != word[i] ) break;
  30.     }
  31.     if( i == wordlen ) return 1;
  32.  
  33.     //Check up (reversed)
  34.     for( i = 0; i < wordlen; i++ )
  35.     {
  36.         if( y-i < 0 ) break;
  37.         char c = board[x+(y-i)*w];
  38.         if( c != word[wordlen-i-1] ) break;
  39.     }
  40.     if( i == wordlen ) return 1;
  41.  
  42.     //check left.
  43.     for( i = 0; i < wordlen; i++ )
  44.     {
  45.         if( x-i < 0 ) break;
  46.         char c = board[(x-i)+y*w];
  47.         if( c != word[i] ) break;
  48.     }
  49.     if( i == wordlen ) return 1;
  50.  
  51.     //check left (Reversed)
  52.     for( i = 0; i < wordlen; i++ )
  53.     {
  54.         if( x-i < 0 ) break;
  55.         char c = board[(x-i)+y*w];
  56.         if( c != word[wordlen-i-1] ) break;
  57.     }
  58.     if( i == wordlen ) return 1;
  59.  
  60.     //check down.
  61.     for( i = 0; i < wordlen; i++ )
  62.     {
  63.         if( y+i >= h ) break;
  64.         char c = board[x+(y+i)*w];
  65.         if( c != word[i] ) break;
  66.     }
  67.     if( i == wordlen ) return 1;
  68.  
  69.     //check right.
  70.     for( i = 0; i < wordlen; i++ )
  71.     {
  72.         if( x+i >= w ) break;
  73.         char c = board[(x+i)+y*w];
  74.         if( c != word[i] ) break;
  75.     }
  76.     if( i == wordlen ) return 1;
  77.  
  78.     //check upleft.
  79.     for( i = 0; i < wordlen; i++ )
  80.     {
  81.         if( x-i < 0 ) break;
  82.         if( y-i < 0 ) break;
  83.         char c = board[(x-i)+(y-i)*w];
  84.         if( c != word[i] ) break;
  85.     }
  86.     if( i == wordlen ) return 1;
  87.  
  88.     //check upleft. (reversed)
  89.     for( i = 0; i < wordlen; i++ )
  90.     {
  91.         if( x-i < 0 ) break;
  92.         if( y-i < 0 ) break;
  93.         char c = board[(x-i)+(y-i)*w];
  94.         if( c != word[wordlen-i-1] ) break;
  95.     }
  96.     if( i == wordlen ) return 1;
  97.  
  98.     //check upright.
  99.     for( i = 0; i < wordlen; i++ )
  100.     {
  101.         if( x+i >= w ) break;
  102.         if( y-i < 0 ) break;
  103.         char c = board[(x+i)+(y-i)*w];
  104.         if( c != word[i] ) break;
  105.     }
  106.     if( i == wordlen ) return 1;
  107.  
  108.     //Check upright (reversed)
  109.     for( i = 0; i < wordlen; i++ )
  110.     {
  111.         if( x+i >= w ) break;
  112.         if( y-i < 0 ) break;
  113.         char c = board[(x+i)+(y-i)*w];
  114.         if( c != word[wordlen-i-1] ) break;
  115.     }
  116.     if( i == wordlen ) return 1;
  117.  
  118.  
  119.     //check downleft.
  120.     for( i = 0; i < wordlen; i++ )
  121.     {
  122.         if( x-i < 0 ) break;
  123.         if( y+i >= h ) break;
  124.         char c = board[(x-i)+(y+i)*w];
  125.         if( c != word[i] ) break;
  126.     }
  127.     if( i == wordlen ) return 1;
  128.  
  129.  
  130.     //check downright.
  131.     for( i = 0; i < wordlen; i++ )
  132.     {
  133.         if( x+i >= w ) break;
  134.         if( y+i >= h ) break;
  135.         char c = board[(x+i)+(y+i)*w];
  136.         if( c != word[i] ) break;
  137.     }
  138.  
  139.     if( i == wordlen ) return 1;
  140.  
  141.  
  142.     return 0;
  143. }
  144.  
  145. int main( int argc, char ** argv )
  146. {
  147.     int x, y;
  148.     int i = 0;
  149.  
  150.     if( argc < 5 )
  151.     {
  152.         fprintf( stderr, "Error: usage:  ./wordsearch [x] [y] [word] [letters]\n" );
  153.         return -1;
  154.     }
  155.  
  156.     w = atoi( argv[1] );
  157.     h = atoi( argv[2] );
  158.     word = argv[3];
  159.     letters = argv[4];
  160.     wordlen = strlen( word );
  161.     letterslen = strlen( letters );
  162.     board = malloc( w * h );
  163.     memset( board, 0, w * h );
  164.  
  165.     for( i = 0; i < w*h; i++ )
  166.     {
  167.         x = i % w;
  168.         y = i / w;
  169.  
  170.         board[i] = letters[rand()%letterslen];
  171.  
  172.         if( CheckPlace( x, y ) )
  173.         {
  174.             //Fail -- backup.
  175.             board[i] = 0;
  176.             int backtrack = rand()%100;
  177.             float backt = pow((float)backtrack/80.0, 9.0f);
  178.             i-=(int)backt+1;
  179.             if( i < 0 ) i = 0;
  180.             hits++;
  181.             continue;
  182.         }
  183.     }
  184.  
  185.     printf( "HITS: %d\n", hits );
  186.  
  187.     for( y = 0; y < h; y++ )
  188.     {
  189.         for( x = 0; x < w; x++ )
  190.         {
  191.             printf( "%c ", board[x+y*w] );
  192.         }
  193.         printf( "\n" );
  194.     }
  195.  
  196.     return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement