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

相关文章

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路

linux安装、更新、卸载anaconda实践

《linux安装、更新、卸载anaconda实践》Anaconda是基于conda的科学计算环境,集成1400+包及依赖,安装需下载脚本、接受协议、设置路径、配置环境变量,更新与卸载通过conda命令... 目录随意找一个目录下载安装脚本检查许可证协议,ENTER就可以安装完毕之后激活anaconda安装更

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

MySQL数据类型与表操作全指南( 从基础到高级实践)

《MySQL数据类型与表操作全指南(从基础到高级实践)》本文详解MySQL数据类型分类(数值、日期/时间、字符串)及表操作(创建、修改、维护),涵盖优化技巧如数据类型选择、备份、分区,强调规范设计与... 目录mysql数据类型详解数值类型日期时间类型字符串类型表操作全解析创建表修改表结构添加列修改列删除列

Go语言网络故障诊断与调试技巧

《Go语言网络故障诊断与调试技巧》在分布式系统和微服务架构的浪潮中,网络编程成为系统性能和可靠性的核心支柱,从高并发的API服务到实时通信应用,网络的稳定性直接影响用户体验,本文面向熟悉Go基本语法和... 目录1. 引言2. Go 语言网络编程的优势与特色2.1 简洁高效的标准库2.2 强大的并发模型2.

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

深入解析Java NIO在高并发场景下的性能优化实践指南

《深入解析JavaNIO在高并发场景下的性能优化实践指南》随着互联网业务不断演进,对高并发、低延时网络服务的需求日益增长,本文将深入解析JavaNIO在高并发场景下的性能优化方法,希望对大家有所帮助... 目录简介一、技术背景与应用场景二、核心原理深入分析2.1 Selector多路复用2.2 Buffer