C语言实践:找字游戏

单词查找游戏

说明:在填满字母的正方形表格中找出指定集合中的所有单词。可以竖着读,可以横着读,可以斜着读。遇到边界可以环绕,但方向不得改变,同一个单元格可以出现在多个单词中,但在一个单词中只能出现一次。
这里我们用指针实现字符矩阵。首先给Table分配一块内存,接着初始化。初始化把单词填写到Table里有多重模式:

  • 行方向
  • 列方向
  • 对角方向
  • 反对角方向

每次随机选择一种模式。注意最后释放内存。
游戏效果截图:
这里写图片描述
输入级别,游戏开始:
这里写图片描述

/*****************************************************/
/*Word find game                                     */
/*Author:chenweiliang   2016.10.10          */
/*****************************************************/

#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int TABLE_SIZE = 10;
const int NUMBER_OF_WORDS = 25;
string WORDS[NUMBER_OF_WORDS] = {"face","she","me","his","book","school","bear","hello","world","afternoon",
"monday","tuesday","friday","father","mother","learn","math","english","china","university",
"physics","beat","pencil","jaw","source"};

int mod(int a, int m) {
    while (a < 0) {
        a += m;
    }
    return a%m;
}

//
bool printWordToTable(int *table, string word) {
    int begin_x = rand() % TABLE_SIZE;
    int begin_y = rand() % TABLE_SIZE;
    int mode = rand() % 3;

    const int buffer_len = sizeof(int)*TABLE_SIZE*TABLE_SIZE;
    int *buffer = (int *)malloc(buffer_len);
    memcpy(buffer, table, buffer_len);

    //Mode 0.Store row by row.
    int length = word.size();
    if (mode == 0) {
        //Go left
        if (rand() % 2 == 0) {
            int x = begin_x;
            int y = begin_y;
            for (int i = 0; i < length; ++i) {
                //Check whether conflict
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[ x + y*TABLE_SIZE] = word.c_str()[i] - 96;
                x = (x+1)%TABLE_SIZE;
            }
        }
        //Go right
        else {
            int x = begin_x;
            int y = begin_y;
            for (int i = 0; i < length; ++i) {
                //Check whether conflict
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[x + y*TABLE_SIZE] = word.c_str()[i] - 96;
                x = mod(x - 1, TABLE_SIZE);
            }
        }
    }
    //Mode 1.Store col by col.
    else if (mode == 1) {
        //Go bottom
        if (rand() % 2 == 0) {
            int x = begin_x;
            int y = begin_y;
            for (int i = 0; i < length; ++i) {
                //Check whether conflict
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[x  + y*TABLE_SIZE] = word.c_str()[i] - 96;
                y = (y + 1) % TABLE_SIZE;
            }
        }
        //Go top
        else {
            int x = begin_x;
            int y = begin_y;
            for (int i = 0; i < length; ++i) {
                //Check whether conflict
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[x + y*TABLE_SIZE] = word.c_str()[i] - 96;
                y = mod(y - 1,  TABLE_SIZE);
            }
        }
    }
    else if (mode == 2) {
        //主对角线
        if (rand() % 4 == 0) {
            int max_length = TABLE_SIZE-abs(begin_x - begin_y);
            if (max_length < length) {
                return printWordToTable(table, word);
            }
            int x = begin_x;
            int y = begin_y;
            for (int i = 0; i < length; ++i) {
                if (y > TABLE_SIZE - 1 || x > TABLE_SIZE - 1) {
                    begin_x = y - 1;
                    begin_y = x - 1;
                    x = begin_x;
                    y = begin_y;
                }
                //Check whether conflict
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[x + y*TABLE_SIZE] = word.c_str()[i] - 96;
                x++;
                y++;
            }
        }
        //主对角线反方向
        else if (rand() % 4 == 1) {
            int max_length = TABLE_SIZE - abs(begin_x - begin_y);
            if (max_length < length) {
                return printWordToTable(table, word);
            }
            int x = begin_x;
            int y = begin_y;
            for (int i = 0; i < length; ++i) {
                if (y < 0 || x < 0) {
                    begin_x = y + 1;
                    begin_y = x + 1;
                    x = begin_x;
                    y = begin_y;
                }
                //Check whether conflict
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[x  + y*TABLE_SIZE] = word.c_str()[i] - 96;
                x--;
                y--;

            }
        }
        //副对角线
        else if(rand() %4 == 2){
            int max_length = TABLE_SIZE - abs(TABLE_SIZE-1-begin_x - begin_y);
            if (max_length < length) {
                return printWordToTable(table, word);
            }
            int x = begin_x;
            int y = begin_y;
            for (int i = 0; i < length; ++i) {
                if (y > TABLE_SIZE - 1 || x < 0) {
                    begin_x = y - 1;
                    begin_y = x + 1;
                    x = begin_x;
                    y = begin_y;
                }
                //Check whether conflict
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[x + y*TABLE_SIZE] = word.c_str()[i] - 96;
                x--;
                y++;
            }
        }
        else {
            int max_length = TABLE_SIZE - abs(TABLE_SIZE - 1 - begin_x - begin_y);
            if (max_length < length) {
                return printWordToTable(table, word);
            }

            int x = begin_x ;
            int y = begin_y ;
            for (int i = 0; i < length; ++i) {
                if (x > TABLE_SIZE - 1 || y < 0) {
                    begin_x = y + 1;
                    begin_y = x - 1;
                    x = begin_x;
                    y = begin_y;
                }
                //Check whether conflicts
                if (table[x + y*TABLE_SIZE] != 0 &&
                    table[x + y*TABLE_SIZE] != word.c_str()[i] - 96) {
                    memcpy(table, buffer, buffer_len);
                    free(buffer);
                    return false;
                }
                table[x   + y *TABLE_SIZE] = word.c_str()[i] - 96;
                x++;
                y--;
            }
        }
    }
    return true;
}

void printTable(int *table) {
    for (int y = 0; y < TABLE_SIZE; ++y) {
        for (int x = 0; x < TABLE_SIZE; ++x) {
            if (table[x + y*TABLE_SIZE] == 0) {
                printf("_ ");
            }
            else {
                printf("%c ", table[x+y*TABLE_SIZE] + 96);

            }
        }
        printf("\n");
    }
    printf("\n");

}

int main()
{
    //填字游戏说明:在填满字母的正方形表格中找出指定集合中的所有单词。可以竖着读,可以横着读,可以斜着读。遇到边界可以环绕
    //但方向不得改变,同一个单元格可以出现在多个单词中,但在一个单词中只能出现一次。
    printf("Welcome to the word-find game!Enter the level you want to play.[2-10]\n");
    scanf("%d", &TABLE_SIZE);
    if (TABLE_SIZE < 2 || TABLE_SIZE>20) {
        printf("Illegal input.Use default value.Level = 10\n");
        TABLE_SIZE = 10;
    }
    //The table is stored row by row.
    int *table = (int *)calloc(TABLE_SIZE*TABLE_SIZE, sizeof(int));
    vector<string> words;
    for (int i = 0; i < NUMBER_OF_WORDS; ++i) {
        if (WORDS[i].size() > TABLE_SIZE) {
            continue;
        }
        if (printWordToTable(table, WORDS[i])) {
            words.push_back(WORDS[i]);
        }
        printTable(table);
    }

    int *answers = (int *)malloc(sizeof(int)*TABLE_SIZE*TABLE_SIZE);
    memcpy(answers, table, sizeof(int)*TABLE_SIZE*TABLE_SIZE);

    //Init other elements randomly.
    for (int y = 0; y < TABLE_SIZE; ++y) {
        for (int x = 0; x < TABLE_SIZE; ++x) {
            if (table[x + y*TABLE_SIZE] == 0) {
                int r = rand() % 27;
                table[x + y*TABLE_SIZE] = r<=0?1:r;
            }
        }
    }

    printf("The table is :\n");
    printTable(table);

    printf("Words to find:\n");
    for (int i = 0; i < words.size(); ++i) {
        printf("%s ", words[i].c_str());
    }
    printf("\n");

    printf("Answers:\n");
    printTable(answers);
    scanf("\n");

    free(table);
    free(answers);
    return 0;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值