i was given an assignment to create a word construction game where the program will randomly picks a combination of vowels and consonants and the user will build as much words as possible from the choosen letters. my prog will check for the correctness of the words and gives the user points to each word built, with higher point given to words that use all the letters. i limited the number of letters to 4 or 5 letters only. i justtttt learned programming from the intro up to the string topic. so , i havee no idea how to create this game. can anyone help me with this ? i'm currently using dev c++ 5.5.3. someone pleasee help me with this :( and i also need some help on the calculations of marks.

#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#include<ctype.h>

void Question(int);
void PrintLine();
void Menu();
void Easy();
void Medium();
void Hard();
void Compareone(char[50][10],char[30][10]);
void CompareTwo(char[50][10],char[30][10]);
void CompareThree(char[50][10],char[30][10]);

char AnslistOne[30][10] = { "horse","shore","her","shoe","sore","hero","rose","so","hose","she","he","hoe","peach","cheap","cap","pace","hap","chap","ape","heap","pec","ache" };
char AnslistTwo[30][10]= { "wodge","dew","go","do","wed","dog","god","woe","doe","ode","cove","vet","veto","cot","cote","vote","evo","toe","covet" };
char AnslistThree[30][10]= { "pooch","hoop","hop","cop","coop","poo","poco","chop","anime","amine","mine","name","mean","in","am","man","men","him","min" };
char cont,Ans[50][10];
int level,num,i,v;
int main ()
{
    char name[50];
    system("color 05");
    PrintLine();
    printf("\n\t\t\tWelcome to Boggles\t\n");
    PrintLine();
    system("title Word Construction Game");

    printf("\t\t\tEnter name: "); //input user's name
    gets(name);


    do
    {
        system("cls");
        system("color 80"); 
        Menu();
        printf("\n\nHi %s, please select difficulty (1-3) :",name); //asking user to select the level of difficulty
        scanf(" %c",&level);
        Question(level);
        while( level == 1 && level == 2 && level == 3 )
        {
            printf("Sorry. we only have level 1 to level 3 only. Please re-enter: ");
            scanf(" %c",&level);
            Question(level);
        }
        printf("\n\nDo you want to continue? (Y-yes|N-no)"); //asking user to continue or not
        scanf(" %c", &cont);
        while(cont!='y'&&cont!='Y'&&cont!='n'&&cont!='N')
           {
              fflush(stdin);
              printf("Invalid input. Please re-enter. (Y-yes|N-no)");
              scanf(" %c", &cont);
           }        
    }while(cont=='y'||cont=='Y');



    system("pause");
    return 0;

}
void PrintLine ()
{
    printf("\n\t\t=======================================\n");
    return;
}
void Menu()
{
    printf("Level 1 - Easy\n");
    printf("Level 2 - Medium\n");
    printf("Level 3 - Hard\n");

}
void Question(level)
{
    switch (level)
    {
        case 1: Easy();
                break;
        case 2: Medium();
                break;
        case 3: Hard();
                break;                  

    }
}
void Easy()
{
       srand(time(NULL)); //to get random question

       int num = rand() % (2);
       switch(num)
       {
        case 1 : printf("\n\nConstruct as much word as you can from the letters below:\n");
                 printf("S E R O\n\n");
                 printf("Answers : \n");
                 scanf("%s",&Ans[50][10]); //user can enter as much words as they want
                 break;
        case 2 : printf("\n\nConstruct as much word as you can from the letters below:\n");
                 printf("E C H A P\n\n");
                 printf("Answers : \n");
                 scanf("%s",&Ans[50][10]); //user can enter as much words as they want
                 break;                  
       }
       CompareOne(Ans[50][10],AnslistOne[30][10]);
}
void Medium()
{
       srand(time(NULL)); //to get random question

       int num = rand() % (2);
       switch(num)
       {
        case 1 : printf("\n\nConstruct as much word as you can from the letters below:\n");
                 printf("D W O E G\n\n");
                 printf("Answers : \n");
                 scanf("%s",&Ans[50][10]); //user can enter as much words as they want
                 break;
        case 2 : printf("\n\nConstruct as much word as you can from the letters below:\n");
                 printf("V C T O E\n\n");
                 printf("Answers : \n");
                 scanf("%s",&Ans[50][10]); //user can enter as much words as they want
                 break;                  
       }
       CompareTwo(Ans[50][10],AnslistTwo[30][10]);
}
void Hard()
{
       srand(time(NULL)); //to get random question

       int num = rand() % (2);
       switch(num)
       {
        case 1 : printf("\n\nConstruct as much word as you can from the letters below:\n");
                 printf("O C O H P\n\n");
                 printf("Answers : \n");
                 scanf("%s",&Ans[50][10]); //user can enter as much words as they want
                 break;
        case 2 : printf("\n\nConstruct as much word as you can from the letters below:\n");
                 printf("M N E I A\n\n");
                 printf("Answers : \n");
                 scanf("%s",&Ans[50][10]); //user can enter as much words as they want
                 break;                  
       }
      CompareThree(Ans[50][10],AnslistThree[30][10]);
}
void CompareOne(char Ans[50][10],char AnslistOne[30][10])
{
    for (i=0;i<50;i++) //to check for the correctness of the word entered
    {
        if ((strcmpi(Ans[i],AnslistOne[30]))==0)
           printf("CORRECT! ^_^\n");
        else 
           printf("OOPS, SORRY! WORD NOT IN MY LIST\n");
    }
}
void CompareTwo (char Ans[50][10],char AnslistTwo[30][10])
{
    for (i=0;i<50;i++)  //to check for the correctness of the word entered
    {
        if ((strcmpi(Ans[i],AnslistTwo[30]))==0)
           printf("CORRECT! ^_^\n");
        else 
           printf("OOPS, SORRY! WORD NOT IN MY LIST\n");
    }   
}
void CompareThree (char Ans[50][10],char AnslistThree[30][10])
{
    for (i=0;i<50;i++) //to check for the correctness of the word entered
    {
        if ((strcmpi(Ans[i],AnslistThree[30]))==0)
           printf("CORRECT! ^_^\n");
        else 
           printf("OOPS, SORRY! WORD NOT IN MY LIST\n");
    }   
}

my program is almost finish but how can i let my prog randomly pick the question ?

First, you need to be able to generate a random number (int) within your stored question array. In this case, I believe you set it to 30, so the number should be from 0 up to 29 (inclusive). You should read this page on how to use rand() function in order to get a number in a range.

Here is how I might start to tackle a problem like this ...

Note the comments in the program to find all the .h files needed to test it out.

You can find file "CvecOfString.h" at:
http://developers-heaven.net/forum/index.php/topic,2580.msg2865.html#msg2865

You can find file "readLine.h" at:
http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

You can find file "Cvec.h" at:
http://developers-heaven.net/forum/index.php/topic,2580.msg2862.html#msg2862

The following program assumes that you already have a big dictionary text (.txt) file (pre-fixed, sorted and all lower case words) available.

/* makeWordsFromLetters.c */  /* 2018-09-19 */


#include <time.h> /* re. srand( time(0) ); */

#include "CvecOfString.h" /* also includes readLine.h, Cvec.h  ...
                             which also include ... (look in files, to see) */

/*
find "CvecOfString.h" at:
    http://developers-heaven.net/forum/index.php/topic,2580.msg2865.html#msg2865

find "readLine.h" at:
     http://developers-heaven.net/forum/index.php/topic,2580.msg2864.html#msg2864

find "Cvec.h" at:
     http://developers-heaven.net/forum/index.php/topic,2580.msg2862.html#msg2862

*/




const char* FNAME = "fixedWords.txt";

const char* HEADER1 =
    "This game asks how many letters (3 to 7 ): your wordLen = ?\n\n"

    "It then builds a dictionary of words with that length, \n"
    "   from a big dictionary of words ...\n\n"

    "It then randomly selects (makes a copy of) a word from that built list,\n"
    "   then scrambles the letters of the copy and displays it. \n";

const char* HEADER2 =
    "It then asks the user to enter a word, using as many letters as possible, \n"
    "   from that C string of scrambled letters. \n"
    "It then compares the user entered word to the BIG dictionary of words\n"
    "   using a binary search of a vector of words in memory.\n"

    "If that user entered word is in the BIG dictionary ... \n"
    "   it gives a score for that word, \n"
    "   (and here ... it's just a simple score related to word length.)\n"
    "Else it loops to try again ... while user wants to ... .\n";


unsigned takeInUnsigned( const char* msg, unsigned min, unsigned max )
{
    unsigned val = 0;
    while( 1 )
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%u", &val ) == 1 && getchar() == '\n' )
        {
            if( val >= min && val <= max )
                return val;
            printf( "Only values in range %d..%d are valid here.\n", min, max );
        }
        else
        {
            printf( "\nPositive integers only ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }
    }
}

int takeInChr( const char* msg )
{
    int reply;
    fputs( msg, stdout ); fflush( stdout );
    reply = getchar();
    if( reply != '\n' )
        while( getchar() != '\n' ) ; /* 'flush' stdin buffer */
    return reply;
}

int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChr( "More (y/n) ? " )) == 'n' ) return 0;
    /* else ... */
    return 1;
}

int loadDict( const char* fname, Cvec* cv  )
{
    FILE* fin = fopen( fname, "r" );
    if( fin )
    {
        Rec rc;
        while( (rc.str = readLine( fin )) )
            push_backCvec( cv, &rc );
        fclose( fin );
        return 1;
    }
    return 0;
}

void getSubDict( Cvec* cv1, Cvec* cv2, unsigned wordLen )
{
    int i;
    Rec rc;
    for( i = 0; i < cv1->size; ++ i )
    {
        if( strlen( cv1->ary[i].str ) == wordLen )
        {
            rc.str = newCopy( cv1->ary[i].str );
            push_backCvec( cv2, &rc );
        }
    }
}

char* scramble( const char* str )
{
    unsigned i, len = strlen( str );
    char* tmp = newCopy( str );
    for( i = 0; i < len; ++ i )
    {
        int j = rand() % len, k = rand() % len;
        char c = tmp[j];
        tmp[j] = tmp[k];
        tmp[k] = c;
    }
    return tmp;
}

char* buildWord( const char* letters )
{
    char* word;
    char* p;
    int notDone = 1;
    while( notDone )
    {
        notDone = 0;
        printf( "Enter your test word: " ); fflush( stdout );
        p = word = readLine( stdin );

        while( *p )
        {
            if( !strchr( letters, *p ) )
            {
                printf( "Letter %c NOT available to use here ...\n", *p );
                free( word );
                notDone = 1;
                break; /* break OUT of INNER loop */
            }
            ++p;
        }
    }
    return word;
}

/* iterative ...
   if multiple same target values ... finds index of first target value in ary */
int iBinSearch( const Cvec* cv, int low, int high, const char* target )
{
    while( low < high )
    {
        int mid = low + ( ( high - low ) >> 1 ); /* (low+hign)/2 */

        if( strcmp(cv->ary[mid].str, target) < 0 )
            low = mid + 1;
        else
            high = mid;
    }

    /* when reach here ... */
    if( high == low && strcmp( target, cv->ary[low].str) == 0 )
        return low;

    /* else ... */
    return -1;
}




int main()
{
    Cvec cv, cv2, cv3;
    initCvec( &cv ); initCvec( &cv2 ); initCvec( &cv3 );

    fputs( HEADER1, stdout ); fputs( HEADER2, stdout );

    if( loadDict( FNAME, &cv ) )
    {
        /* unsigned wordLen = 0; */
        printf( "\nThere are %d words in the 'main dictionary' ...\n", cv.size );

        srand( time(0) );

        do
        {
            /* get a 'Sub Dictionary' ... */
            char* tmp, *test;
            int i, ranNum = 0, score = 0;
            unsigned wordLen = takeInUnsigned( "\nEnter number of letters (3..7) : ", 3, 7 );
            getSubDict( &cv, &cv2, wordLen );

            printf( "There are %d words from which to pick a %u letter word.\n",
                cv2.size, wordLen );

            ranNum = rand() % cv2.size;

            printf( "The word at postion %d is '%s'\n", ranNum, cv2.ary[ranNum].str );

            tmp = scramble( cv2.ary[ranNum].str ); /* return  a NEW scrambled COPY in tmp */
            printf( "the word scramnled is '%s'\n", tmp );

            do
            {
                test = buildWord( tmp );

                printf( "The test word you entered is %s\n", test );

                i = iBinSearch( &cv, 0, cv.size-1, test );
                if( i != -1 )
                {
                    Rec tmpRec;
                    printf( "Word %s was found in 'main dictionary at index %d\n", test, i );
                    tmpRec.str = test;
                    push_backCvec( &cv3, &tmpRec );
                }
                else
                {
                    printf( "'%s' was NOT found in 'the 'main dictionary' ... try again ...\n", test );
                    free( test );
                }
            }
            while( tolower(takeInChr( "Another test word to try (y/n) ? " )) != 'n' );


            uniqueCvec( &cv3 );

            printf( "You found: " );
            for( i = 0; i < cv3.size; ++ i )
            {
                score += strlen( cv3.ary[i].str );
                printf( "'%s' ", cv3.ary[i].str );
            }
            printf( "\ni.e. %d words, with score %d\n\n", i, score );

            clearCvec( &cv3 );

            free( tmp );
            clearCvec( &cv2 );

        }
        while( more() );


        clearCvec( &cv ); /* free all dynamic memory when done with it ... */

    }
    else printf( "There was a problem opening file %s\n", FNAME );

    return 0;
}

Edited 1 Year Ago by David W

This article has been dead for over six months. Start a new discussion instead.