Jacob_DiDiodato icon

Scrabble Word Solver

Jacob_DiDiodato | PRO | 02/24/17 12:51:56 AM UTC | 0 ⭐ | 353 👁️ | Never ⏰ | []
C |

4.74 KB

|

None

|

0 👍

/

0 👎

//Project Name: wordPuzzle.c
//Author Name: Jacob DiDiodato
//Project Version: 0.8
//Last Edit: Thursday February 23rd 2017 @ 8:25 PM
//Compilation instructions: gcc -Wall -std=c99 -o words wordPuzzle.c dict.c
//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)
//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)
//The dictionary used for this program can be found here: http://www.filedropper.com/scrabbledictionary
//In order to use the dictionary, simply add it the same folder you are running this program in.
//As of right now this program is unfinished. The cartesian product function must be added.
//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!
//dict.c can be found here: http://pastebin.com/ebf27VHV
//dict.h can be found here: http://pastebin.com/x5f5iJh9
 
 
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
 
#include "dict.h"
 
//A recursive function to find the power of a base raised to a positive power
int power(int base, int exp)
{
    if (exp == 0)
    {
        return 1;
    }
    else if (exp == 1)
    {
        return base;
    }
    else
    {
        return base * power(base, exp - 1);
    }
}
 
//A function to find the length of a pointer to an array of characters
int len(char* string)
{
    for (int i = 0;;i++)
    {
        if (*(string+i) == '\0')
        {
            return i;
        }
    }
}
 
//A function to split a pointer to an array of characters into a smaller pointer to an array of characters where delimeter is removed
//For example split("a,b,c,d,e", ',') returns "abcde"
char* split(char* string, char delimiter)
{
    if (delimiter == '\0')
    {
        printf("You can't use the null character as a delimeter\n");
        exit(0);
    }
    //Get enough memory to store all the information
    char* newString = (char*) malloc(len(string)*sizeof(char));
    int posInString = 0;
    for(int i = 0;;i++)
    {
        if (*(string+i) == '\0')
        {
            break;
        }
        else if ((char)*(string+i) == delimiter){}
        else
        {
            *(newString + posInString) = *(string + i);
            posInString++;
        }
    }
    
    //Add a null-terminator
    *(newString + posInString) = '\0';
    
    return newString;
}
 
//A function to check if a pointer to an array of characters is a decimal integer
int isNumber(char* word)
{
    int i = 0;
    for (;;i++)
    {
        if (!isdigit(*(word+i)))
        {
            if (*(word+i) == (int)'\0')
            {
                return 0;
            }
            return -1;
        }
    }
}
 
//A function to make the cartesian product of strings 
char* makeStrings(int lengthOfWord, char* charSet)
{
    int numStrings = power(len(charSet), lengthOfWord);
    
    char* strings = (char*) malloc(numStrings*(sizeof(char)*lengthOfWord));
    
    //Make the cartesian product
    
    //For now we need to return nothing because i'm not sure what would happen if you returned some allocated memory. Probably would suck
    return "";
    //When done making the cartesian product do this:
    //return strings;
}
 
int main(int argc, char * argv[])
{
    int rows = 22, posInWords = 0;
    const char delimeter[2] = ",";
    char* token;
    
    char* charSet;
    int wordLen;
    
    Dict d = DictCreate();
 
    FILE *fp;
 
    fp = fopen("./scrabbleDictionary.txt", "r");
    fseek(fp, 0, SEEK_END);
    long int numChars = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    
    /*Change the following block of code to use the split functionality that I made*/
    /*-----------------------------------------------------------------------------*/
    char dictionary[numChars];
    fgets(dictionary, numChars, (FILE*)fp);
 
    token = strtok(dictionary, delimeter);
    DictInsert(d, token, "");
    while(token != NULL)
    {
        DictInsert(d, token, "");
        token = strtok(NULL, delimeter);
    }
    /*-----------------------------------------------------------------------------*/
    
    if (argc < 3)
    {
        printf("Usage: %s <character set> <length of word>\n", argv[0]);
    }
    else
    {
        //printf("%s\n", argv[2]);
        if (isNumber(argv[2]) != 0)
        {
            printf("Usage: %s <character set> <length of word>\nThe length of the word must be a valid integer\n", argv[0]);
        }
        else
        {
            wordLen = atoi(argv[2]);
            if (strcmp(argv[1], "alphabet") == 0)
            {
                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";
            }
            else
            {
                charSet = argv[1];
            }
            printf("The character set you are using is: %s\nThe length of word you want is: %d\n", charSet, wordLen);
            
            makeStrings(wordLen, split(charSet, ','));
        }
    }
    
    DictDestroy(d);
    free(fp);
    
    return 0;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎