samedi 26 décembre 2015

Issue with filling a grid with random letters - C Programming

I'm totally new to C Progamming and I encountered some issues.

I'm trying to create a Word Search, however I encountered a problem.

I'm looking to generate random letter inside the 2D array however I can't manage to do that with the srand function.

This is how the code looks like:

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

#define ROWS 10
#define COLUMNS 10




int getRandomWord()
{

    char *names1[]= {"DOG" , "CAT", "HORSE" , "SNAKE" };
    char *names2[]= {"RAT", "FOX", "FISH", "DRAGON" };
    char *names3[]= { "TIGER", "COW", "SHARK", "BEAR" };
    char *names4[]= { "NOSE", "TICKET", "GIRAFFE", "CAKE" };
    char *names5[]= { "HOLE", "RECORD", "DRY", "BED" };
    char *names6[]= { "LACE", "ZIP", "CAMP", "FROG" };
    char *names7[]= { "SHIP", "TOWN", "FLAG", "DRUMS" };
    char *names8[]= { "BEE", "SEA", "FIRE", "WATER" };

    int l;
    int rn = (rand() % 8) + 1;

    printf ("These are the random words:");

    switch(rn)

    {
    case 1 : 
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names1[l]);
    }
        break;

    case 2 : 
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names2[l]);
    }
        break;

    case 3 :  
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names3[l]);


    }
        break;

    case 4 :  
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names4[l]);


    }
        break;

    case 5 :  
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names5[l]);


    }
        break;

    case 6 :  
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names6[l]);


    }
        break;

    case 7 :  
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names7[l]);


    }
        break;

    case 8 :  
            for (l = 0; l  <4; l++ )
    {
        printf (" %s", names8[l]);


    }
        break;
    default : puts ("Invalid");

    }

    getchar();

    }



    char puzzle[ROWS][COLUMNS];

char getRandomCharacter() 
{
    int rl = (rand() % 26) + 65;
    return (char)rl;
}    


void createBlankPuzzle()
{
    int i , j;

    for(i = 0;i < ROWS;i++)
    {
        for(j = 0;j < COLUMNS;j++)
        {
            puzzle[i][j] = '.';
        }
    }
}

void displayPuzzle()   //gridpositions

{
    int i , j;
    char x = 'A';

    printf("\t");


    for(i = 0; i< COLUMNS; i++)
    {
        printf( "%c    " , x );
        x++;
    }
    printf ("\n\n");

    for (i=0; i < ROWS ; i ++)
    {
        printf("%d\t" , i);

        for(j = 0 ; j < COLUMNS ; j++ )
        {

            printf("%c    " , puzzle[i][j]);
        }
        printf ("\n\n");
    }
}



void createMenu()
{
    while(1)
{
    int selection = 0;
    printf("Menu\n");
    printf(" Press 1 to start the game\n");
    printf(" Press 2 to exit the game\n" );


    scanf("%d" , &selection);


    switch(selection)
    {
    case 1:
    createBlankPuzzle();
    displayPuzzle();
    getRandomWord();


    printf("\n");
    printf("\n");
    break;

    case 2:
        exit(0);
    break;


    default: puts ("Invalid Option");

    }

    }
}


    int main(void)
{   
    srand(time(NULL));
    createMenu();
    getchar();

}




Aucun commentaire:

Enregistrer un commentaire