C语言实践:找字游戏

2024-01-02 00:10
文章标签 语言 实践 游戏 找字

本文主要是介绍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 = 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;
}

这篇关于C语言实践:找字游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/560862

相关文章

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

MyBatis-Plus 中 nested() 与 and() 方法详解(最佳实践场景)

《MyBatis-Plus中nested()与and()方法详解(最佳实践场景)》在MyBatis-Plus的条件构造器中,nested()和and()都是用于构建复杂查询条件的关键方法,但... 目录MyBATis-Plus 中nested()与and()方法详解一、核心区别对比二、方法详解1.and()

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁