Jacob_DiDiodato

Scrabble Word Solver

Feb 23rd, 2017
405
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.74 KB | None | 0 0
  1. //Project Name: wordPuzzle.c
  2. //Author Name: Jacob DiDiodato
  3. //Project Version: 0.8
  4. //Last Edit: Thursday February 23rd 2017 @ 8:25 PM
  5. //Compilation instructions: gcc -Wall -std=c99 -o words wordPuzzle.c dict.c
  6. //This program, when given a set of letters (comma seperated) and a length of word to produce, will produce all words with those combinations using a cartesian product (not yet implemented)
  7. //You are free to use this software however you like, however, if you are using it please reference my name and the original page you found this program on (probably this: http://pastebin.com/2qsrrbnm)
  8. //The dictionary used for this program can be found here: http://www.filedropper.com/scrabbledictionary
  9. //In order to use the dictionary, simply add it the same folder you are running this program in.
  10. //As of right now this program is unfinished. The cartesian product function must be added.
  11. //Unfortunately I can't recall who initially wrote dict.c and dict.h, If someone can find out who it is send me an email @ [email protected] and I will update that here!
  12. //dict.c can be found here: http://pastebin.com/ebf27VHV
  13. //dict.h can be found here: http://pastebin.com/x5f5iJh9
  14.  
  15.  
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <string.h>
  20.  
  21. #include "dict.h"
  22.  
  23. //A recursive function to find the power of a base raised to a positive power
  24. int power(int base, int exp)
  25. {
  26.     if (exp == 0)
  27.     {
  28.         return 1;
  29.     }
  30.     else if (exp == 1)
  31.     {
  32.         return base;
  33.     }
  34.     else
  35.     {
  36.         return base * power(base, exp - 1);
  37.     }
  38. }
  39.  
  40. //A function to find the length of a pointer to an array of characters
  41. int len(char* string)
  42. {
  43.     for (int i = 0;;i++)
  44.     {
  45.         if (*(string+i) == '\0')
  46.         {
  47.             return i;
  48.         }
  49.     }
  50. }
  51.  
  52. //A function to split a pointer to an array of characters into a smaller pointer to an array of characters where delimeter is removed
  53. //For example split("a,b,c,d,e", ',') returns "abcde"
  54. char* split(char* string, char delimiter)
  55. {
  56.     if (delimiter == '\0')
  57.     {
  58.         printf("You can't use the null character as a delimeter\n");
  59.         exit(0);
  60.     }
  61.     //Get enough memory to store all the information
  62.     char* newString = (char*) malloc(len(string)*sizeof(char));
  63.     int posInString = 0;
  64.     for(int i = 0;;i++)
  65.     {
  66.         if (*(string+i) == '\0')
  67.         {
  68.             break;
  69.         }
  70.         else if ((char)*(string+i) == delimiter){}
  71.         else
  72.         {
  73.             *(newString + posInString) = *(string + i);
  74.             posInString++;
  75.         }
  76.     }
  77.    
  78.     //Add a null-terminator
  79.     *(newString + posInString) = '\0';
  80.    
  81.     return newString;
  82. }
  83.  
  84. //A function to check if a pointer to an array of characters is a decimal integer
  85. int isNumber(char* word)
  86. {
  87.     int i = 0;
  88.     for (;;i++)
  89.     {
  90.         if (!isdigit(*(word+i)))
  91.         {
  92.             if (*(word+i) == (int)'\0')
  93.             {
  94.                 return 0;
  95.             }
  96.             return -1;
  97.         }
  98.     }
  99. }
  100.  
  101. //A function to make the cartesian product of strings
  102. char* makeStrings(int lengthOfWord, char* charSet)
  103. {
  104.     int numStrings = power(len(charSet), lengthOfWord);
  105.    
  106.     char* strings = (char*) malloc(numStrings*(sizeof(char)*lengthOfWord));
  107.    
  108.     //Make the cartesian product
  109.    
  110.     //For now we need to return nothing because i'm not sure what would happen if you returned some allocated memory. Probably would suck
  111.     return "";
  112.     //When done making the cartesian product do this:
  113.     //return strings;
  114. }
  115.  
  116. int main(int argc, char * argv[])
  117. {
  118.     int rows = 22, posInWords = 0;
  119.     const char delimeter[2] = ",";
  120.     char* token;
  121.    
  122.     char* charSet;
  123.     int wordLen;
  124.    
  125.     Dict d = DictCreate();
  126.  
  127.     FILE *fp;
  128.  
  129.     fp = fopen("./scrabbleDictionary.txt", "r");
  130.     fseek(fp, 0, SEEK_END);
  131.     long int numChars = ftell(fp);
  132.     fseek(fp, 0, SEEK_SET);
  133.    
  134.     /*Change the following block of code to use the split functionality that I made*/
  135.     /*-----------------------------------------------------------------------------*/
  136.     char dictionary[numChars];
  137.     fgets(dictionary, numChars, (FILE*)fp);
  138.  
  139.     token = strtok(dictionary, delimeter);
  140.     DictInsert(d, token, "");
  141.     while(token != NULL)
  142.     {
  143.         DictInsert(d, token, "");
  144.         token = strtok(NULL, delimeter);
  145.     }
  146.     /*-----------------------------------------------------------------------------*/
  147.    
  148.     if (argc < 3)
  149.     {
  150.         printf("Usage: %s <character set> <length of word>\n", argv[0]);
  151.     }
  152.     else
  153.     {
  154.         //printf("%s\n", argv[2]);
  155.         if (isNumber(argv[2]) != 0)
  156.         {
  157.             printf("Usage: %s <character set> <length of word>\nThe length of the word must be a valid integer\n", argv[0]);
  158.         }
  159.         else
  160.         {
  161.             wordLen = atoi(argv[2]);
  162.             if (strcmp(argv[1], "alphabet") == 0)
  163.             {
  164.                 charSet = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  165.             }
  166.             else
  167.             {
  168.                 charSet = argv[1];
  169.             }
  170.             printf("The character set you are using is: %s\nThe length of word you want is: %d\n", charSet, wordLen);
  171.            
  172.             makeStrings(wordLen, split(charSet, ','));
  173.         }
  174.     }
  175.    
  176.     DictDestroy(d);
  177.     free(fp);
  178.    
  179.     return 0;
  180. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment