如何在搜索拼图中搜索单词并用随机字符填充拼图

时间:2018-01-24 17:51:36

标签: c wordsearch

C Word搜索拼图程序:

我正在尝试创建一个函数来搜索一个名为wordSearch()的随机生成的单词(在函数fillPuzzleWithWords()中生成)。我需要用户在拼图中输入一系列方块,但我不知道该怎么做 - 这个函数在程序中称为wordSearch()

另外,我想用随机字符(fillPuzzleWithLetters())填充拼图中的空白区域,但我的代码不起作用,我不知道出了什么问题。有什么想法吗?

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

#define ROWS 10
#define COLUMNS 10

char puzzle[ROWS][COLUMNS];

char allWords[20][10] = { "CAT" , "DOG" , "BIKE" , "CHAIR" , "LEFT" , "MUG" , "BAG" , "DUCK" , "CRAB" , "PHONE",
"EXAM" , "MAIL" , "LAUGH" , "PAPER" , "GOLF" , "FOG" , "HAND" , "MOUSE" , "GLASS" , "FAMILY" };

char fourWordsChosen[4][10];

char getRandChar()
{
    int r = (rand() % 26) + 65;
    return (char)r;
}

void getFourRandomWords()
{
    for (int i = 0; i < 4; i++)
    {
        strcpy(fourWordsChosen[i], allWords[rand() % 20]);
    }
}

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

void displayPuzzle()
{
    int i, j, rowNum = 0;
    char x = 'A';

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

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

void wordSearch() {
    char searchTerm[3];
    printf("Enter a range of squares to search:");
    scanf(searchTerm);

}

void putHorizontalWord(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);
}

void putVerticalWord(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[rCol + i][rRow] = word[i];
                }
                else
                {
                    ok = 0;
                }
            }
        }
        else
        {
            ok = 0;
        }
    } while (ok == 0);
}

void putDiagonalWord(char word[10])
{
    int i, ok, rRow, rCol;
    do
    {
        rRow = rand() % 10;
        rCol = rand() % 10;

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

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

    for (i = 0; i < 4; i++)
    {
        orientation = rand() % 3; // To generate a random number from 0, 1, & 2
        if (orientation == 0)
        {
            putHorizontalWord(fourWordsChosen[i]);
        }
        else if (orientation == 1)
        {
            putVerticalWord(fourWordsChosen[i]);
        }
        else
        {
            putDiagonalWord(fourWordsChosen[i]);
        }
    }
}

void fillPuzzleWithLetters()
{
    int i = 0;
    int j = 0;
    do
    {
        for (i = 0; i < COLUMNS; i++)
        {
            if (puzzle[0][i] == ' ')
            {
                puzzle[0][i] = getRandChar();
                i++;
            }
            else if ((puzzle[i][i] == ' '))
            {
                puzzle[i][i] = getRandChar();
                i++;
            }
            else
            {
                for (i = 0; i < ROWS; i++)
                {
                    if ((puzzle[i][0] == ' '))
                    {
                        puzzle[i][0] = getRandChar();
                        i++;
                    }
                    else if ((puzzle[i][i] == ' '))
                    {
                        puzzle[i][i] = getRandChar();
                        i++;
                    }
                }
            }
        }
    }while (j != 1);

void displayFourWords()
{
    printf("The 4 Words to be found are:\n");
    for (int i = 0; i < 4; i++)
    {
        printf("%s ", fourWordsChosen[i]);
    }
}

void menu(int x)
{
    if (x = 1)
    {
        int choice;
        puts("Word Search Puzzle\nChoose an option:");
        printf("\n");
        puts("1. Create Word Search Puzzle");
        puts("2. Exit");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
        {
            system("cls");
            createBlankPuzzle();
            fillPuzzleWithWords();
            fillPuzzleWithLetters();
            displayPuzzle();
            displayFourWords();
            getchar();
            break;
        }
        case 2:
        {
            exit(0);
            break;
        }
        default:
        {
            puts("Not a choice. Please try again.\nPress Enter to continue.");
            getchar();
            getchar();
            system("cls");
            menu(1);
        }
        }
    }
    else
    {

    }
}

int main(void)
{
    srand(time(NULL));
    menu(1);
    getchar();
    return 0;
}

0 个答案:

没有答案