C语言实践:找字游戏

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

C语言实践:找字游戏

文章插图
每次随机选择一种模式 。注意最后释放内存 。
游戏效果截图:
C语言实践:找字游戏

文章插图
【C语言实践:找字游戏】输入级别,游戏开始:
/*****************************************************//*Word find game*//*Author:chenweiliang2016.10.10*//*****************************************************/#include #include #include 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 leftif (rand() % 2 == 0) {int x = begin_x;int y = begin_y;for (int i = 0; i < length; ++i) {//Check whether conflictif (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 rightelse {int x = begin_x;int y = begin_y;for (int i = 0; i < length; ++i) {//Check whether conflictif (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 bottomif (rand() % 2 == 0) {int x = begin_x;int y = begin_y;for (int i = 0; i < length; ++i) {//Check whether conflictif (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 topelse {int x = begin_x;int y = begin_y;for (int i = 0; i < length; ++i) {//Check whether conflictif (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 conflictif (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 conflictif (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 conflictif (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 conflictsif (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 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;}