简体   繁体   中英

horizontal, and/or vertical and/or diagonal words creation

I am trying to fill a crossowrd by randomly picked (from a list) words in either horizontal, vertical and/or diagonal directions.

I have this code here so far which seems only to create horizontal words. I duplicate the "putHorizzontalWord" function inverting the order of rCol and rRow but it still won't work.

Could anyone suggest me how to correct it?

Thanks in advance

ps: I searched the forum database and I found the same problem here however, the answer suggested didn't work for me. I am using KDevelop

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

#define ROWS 10
#define COLUMNS 10

//Creating two arrays Columns and Rows
char puzzle[ROWS][COLUMNS];

//List of words from which four will be picked up by the program, with which the user to play will search
char allWords[20][10] = {"GIRL" , "BOY" , "SHIP" , "CAT" , "FOG" , "KITE" , "BAG" , "STAMP" , "ZOOM" , "JOY", "CAR" , "BUS" , "VAN" , "BOAT" , "BIKE" , "TURBO" , "SCHOOL" , "DOVIC" , "VIRUS" , "STAR"};

char fourWords[4][10];

//creates random characters from ASCII starting at 65 (=A)
char getRandomCharacter()
{
    int r = (rand() % 26) + 65;
    return (char)r;
}

//pick 4 random words from a list of 20
void getFourRandomWords() {
    int draws[4];
    // draw 4 words from the list, no duplicates
    for (int i = 0; i < 4; i++) {
        int n = rand() % (20 - i);
        int j;
        for (j = 4 - i; j < 4 && n >= draws[j]; j++) {
            draws[j - 1] = draws[j];
            n++;
        }
        draws[j - 1] = n;
        strcpy(fourWords[i], allWords[n]);
    }
}

// word to be displayed/searched by the user (I CAN'T GET THIS PART WO WORK)
void displayWords(){
    int words = (rand() % 4);
    printf("%d\n " , words);
}

//Create a grid filled only with asterisks
void createBlankPuzzle()
{
    int i , j;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            puzzle [i][j] = '*';
        }
    }
}
/*
//Create a grid filled with random characters
void createNewPuzzle()
{
    int i , j;

    for(i=0; i<ROWS; i++)
    {
        for(j=0; j<COLUMNS; j++)
        {
            puzzle [i][j] = getRandomCharacter();
        }
    }
}
*/
//Display the character-populated grid
void displayPuzzle()
{
    int i , j , rowNum = 0;
    char x = 'A';

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

    for(i = 0;i < ROWS;i++)
    {
        printf("%d " ,rowNum);
        rowNum++;
        for(j=0; j<COLUMNS; j++)
        {
            printf("%c ", puzzle[i][j]);
        }
        printf("\n");
    }
}

//Check horizontal orientation and place the word if available spaces allow it
void putHorizzontalWord(char word[10])
{
    int rRow, rCol , ok , i;

    do
    {
        rRow = rand() % 10;
        rCol = rand() % 10;

        ok = 1;
        if(rCol + strlen(word) < 10)
        {
            for(i = 0;i < strlen(word);i++)
            {
                if(puzzle[rRow][rCol + i] == '*' ||
                    puzzle[rRow][rCol + i] == word[i])
                {
                    puzzle[rRow][rCol + i] = word[i];
                }
                else
                {
                    ok = 0;
                }
            }
        }
        else
        {
            ok = 0;
        }
    }
    while(ok == 0);
}

//Check vertical orientation and place the word if available spaces allow it
void putVerticalWord(char word[10]) //this, doesn't seem to work'
{
    int rRow, rCol , ok , i;

    do
    {
        rRow = rand() % 10;
        rCol = rand() % 10;

        ok = 0;
        if(rRow + strlen(word) < 10)
        {
            for(i = 0;i < strlen(word);i++)
            {
                if(puzzle[rRow + i][rCol] == '*' ||
                    puzzle[rRow + i][rCol] == word[i])
                {
                    puzzle[rRow + i][rCol] = word[i];
                }
                else
                {
                    ok = 1;
                }
            }
        }
        else
        {
            ok = 1;
        }
    }
    while(ok == 1);
}

//Check diagonal orientation and place the word if available spaces allow it
void putDiagonalWord(char word[10]) //this, doesn't seem to work'
{
    int rRow, rCol , ok , i;

    do
    {
        rRow = rand() % 10;
        rCol = rand() % 10;

        ok = 2;
        if(rRow + strlen(word) < 10)
        {
            for(i = 0;i < strlen(word);i++)
            {
                if(puzzle[rRow + i][rCol + i] == '*' ||
                    puzzle[rRow + i][rCol + i] == word[i])
                {
                    puzzle[rRow + i][rCol + i] = word[i];
                }
                else
                {
                    ok = 2;
                }
            }
        }
        else
        {
            ok = 1;
        }
    }
    while(ok == 0);
}

void fillPuzzleWithWords()
{
    int i , orientation;
    getFourRandomWords();

    for(i=0; i<4; i++)
    {
        orientation = rand() % 3;
        if(orientation == 0)
        {
            putHorizzontalWord(fourWords[i]);
        }
        else if(orientation == 1)
        {
            putVerticalWord(fourWords[i]);
        }
        else
        {
            putDiagonalWord(fourWords[i]);
        }
    }
}

//Create a user-interactive menu
void mainMenu()
{
    char menuChoice;
    do
    {
        printf("\n~~~ DAILY CROSSWORD ~~~\n");
        printf("1. New game\n");
        printf("2. Exit\n");
        menuChoice = getchar();

        switch (menuChoice)
        {
            case '1': displayPuzzle();
                printf("\n------------------------");
                printf("\nUse coordinate to solve\nthe puzzle; i.e. C3, G3\n");
                printf("------------------------\n");
                printf("\n"); break;
        }

    } while (menuChoice != '2');
}

int main(int argc, char *argv[])
{
    srand(time(NULL));

    createBlankPuzzle();
    displayPuzzle();
    fillPuzzleWithWords();


    mainMenu();
    getchar();
    getFourRandomWords();
    printf("Thank you for playing today! :)\nGood Bye");
    return 0;
}

Thanks in advance for any help

you need to change

          if(puzzle[rCol][rRow + i] == '*' ||
                puzzle[rCol][rRow + i] == word[i])
            {
                puzzle[rCol][rRow + i] = word[i];
            }

to this

          if(puzzle[rCol][rRow + i] == '*' ||
                puzzle[rCol + i][rRow] == word[i])
            {
                puzzle[rCol + i][rRow] = word[i];
            }

cant test becuase we dont have a complete test program

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM