C语言实践:找字游戏

阅读: 评论:0

C语言实践:找字游戏

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 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 = 10n");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;
}

本文发布于:2024-01-31 13:00:34,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170667723428705.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:语言   游戏
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23