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

相关文章

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. 建立数据库连接二、定义模型结构体三、自动迁

MySQL 中 ROW_NUMBER() 函数最佳实践

《MySQL中ROW_NUMBER()函数最佳实践》MySQL中ROW_NUMBER()函数,作为窗口函数为每行分配唯一连续序号,区别于RANK()和DENSE_RANK(),特别适合分页、去重... 目录mysql 中 ROW_NUMBER() 函数详解一、基础语法二、核心特点三、典型应用场景1. 数据分

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

MySQL 用户创建与授权最佳实践

《MySQL用户创建与授权最佳实践》在MySQL中,用户管理和权限控制是数据库安全的重要组成部分,下面详细介绍如何在MySQL中创建用户并授予适当的权限,感兴趣的朋友跟随小编一起看看吧... 目录mysql 用户创建与授权详解一、MySQL用户管理基础1. 用户账户组成2. 查看现有用户二、创建用户1. 基

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot项目中整合高德地图的实践

《springboot项目中整合高德地图的实践》:本文主要介绍springboot项目中整合高德地图的实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一:高德开放平台的使用二:创建数据库(我是用的是mysql)三:Springboot所需的依赖(根据你的需求再