簡體   English   中英

水平和/或垂直和/或對角線單詞創建

[英]horizontal, and/or vertical and/or diagonal words creation

我正在嘗試通過在水平、垂直和/或對角線方向上隨機挑選(從列表中)的單詞來填充一個 crossowrd。

到目前為止,我這里有這段代碼,它似乎只能創建水平單詞。 我復制了“putHorizzontalWord”函數,顛倒了 rCol 和 rRow 的順序,但它仍然不起作用。

誰能建議我如何糾正它?

提前致謝

ps:我搜索了論壇數據庫, 在這里發現了同樣的問題,但是建議的答案對我不起作用。 我正在使用 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;
}

在此先感謝您的幫助

你需要改變

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

對此

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

無法測試,因為我們沒有完整的測試程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM