《C++游戏编程入门》第3章 for循环、字符串与数组: World Jumble

《C++游戏编程入门》第3章 for循环、字符串与数组: World Jumble

    • 3.1 使用for循环
        • 03.counter.cpp
    • 3.2 了解对象
    • 3.3 使用string对象
        • 03.string_tester.cpp
    • 3.4 使用数组
        • 03.heros_inventory.cpp
    • 3.6 多维数组
        • 03.tic-tac-toe_board.cpp
    • 3.7 World Jumble程序
        • 03.word_jumble.cpp

3.1 使用for循环

03.counter.cpp
#include <iostream>
using namespace std;int main()
{cout << "Counting forward:\n";for (int i = 0; i < 10; ++i){cout << i << " ";}cout << "\n\nCounting backward:\n";for (int i = 9; i >= 0; --i){cout << i << " ";}cout << "\n\nCounting by fives:\n";for (int i = 0; i <= 50; i += 5){cout << i << " ";}cout << "\n\nCounting with null statements:\n";int count = 0;for (; count < 10;){cout << count << " ";++count;}cout << "\n\nCounting with nested for loops:\n";const int ROWS = 5;const int COLUMNS = 3;for (int i = 0; i < ROWS; ++i){for (int j = 0; j < COLUMNS; ++j){cout << i << "," << j << "  ";}cout << endl;}return 0;
}

3.2 了解对象

类与对象,数据与函数封装。

3.3 使用string对象

03.string_tester.cpp
#include <iostream>
#include <string>
using namespace std;int main()
{// 创建string对象string word1 = "Game";string word2("Over");string word3(3, '!');string phrase = word1 + " " + word2 + word3; // string对象连接cout << "The phrase is: " << phrase << "\n\n";cout << "The phrase has " << phrase.size() << " characters in it.\n\n"; // size()成员函数cout << "The character at position 0 is: " << phrase[0] << "\n\n"; // 索引获取字符cout << "Changing the character at position 0.\n";phrase[0] = 'L'; // 索引字符赋值cout << "The phrase is now: " << phrase << "\n\n";for (unsigned int i = 0; i < phrase.size(); ++i) // 循环访问单个字符{cout << "Character at position " << i << " is: " << phrase[i] << endl;}cout << "\nThe sequence 'Over' begins at location ";cout << phrase.find("Over") << endl; // find()查找字符串if (phrase.find("eggplant") == string::npos) // 未找到{cout << "'eggplant' is not in the phrase.\n\n";}phrase.erase(4, 5); // 移除从位置4开始的长度为5个字符的子字符串cout << "The phrase is now: " << phrase << endl;phrase.erase(4);cout << "The phrase is now: " << phrase << endl;phrase.erase();cout << "The phrase is now: " << phrase << endl;if (phrase.empty())//是否空{cout << "\nThe phrase is no more.\n";}return 0;
}

3.4 使用数组

存储多个相同类型的元素。

03.heros_inventory.cpp
#include <iostream>
#include <string>
using namespace std;int main()
{const int MAX_ITEMS = 10;string inventory[MAX_ITEMS];//创建数组int numItems = 0;//数组索引inventory[numItems++] = "sword";inventory[numItems++] = "armor";inventory[numItems++] = "shield";cout << "Your items:\n";for (int i = 0; i < numItems; ++i){cout << inventory[i] << endl;}cout << "\nYou trade your sword for a battle axe.";inventory[0] = "battle axe";cout << "\nYour items:\n";for (int i = 0; i < numItems; ++i){cout << inventory[i] << endl;}cout << "\nThe item name '" << inventory[0] << "' has ";cout << inventory[0].size() << " letters in it.\n";cout << "\nYou find a healing potion.";if (numItems < MAX_ITEMS){inventory[numItems++] = "healing potion";}else{cout << "You have too many items and can't carry another.";}cout << "\nYour items:\n";for (int i = 0; i < numItems; ++i){cout << inventory[i] << endl;}return 0;
}

3.6 多维数组

03.tic-tac-toe_board.cpp
#include <iostream>
using namespace std;int main()
{const int ROWS = 3;const int COLUMNS = 3; // 创建多维数组char board[ROWS][COLUMNS] = {{'O', 'X', 'O'},{' ', 'X', 'X'},{'X', 'O', 'O'}};cout << "Here's the tic-tac-toe board:\n";for (int i = 0; i < ROWS; ++i){for (int j = 0; j < COLUMNS; ++j){cout << board[i][j]; // 多维数组索引}cout << endl;}cout << "\n'X' moves to the empty location.\n\n";board[1][0] = 'X';cout << "Now the tic-tac-toe board is:\n";for (int i = 0; i < ROWS; ++i){for (int j = 0; j < COLUMNS; ++j){cout << board[i][j];}cout << endl;}cout << "\n'X' wins!";return 0;
}

3.7 World Jumble程序

03.word_jumble.cpp
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;int main()
{enum fields{WORD,HINT,NUM_FIELDS};const int NUM_WORDS = 5; // 单词列表以及提示const string WORDS[NUM_WORDS][NUM_FIELDS] ={{"wall", "Do you feel you're banging your head against something?"},{"glasses", "These might help you see the answer."},{"labored", "Going slowly, is it?"},{"persistent", "Keep at it."},{"jumble", "It's what the game is all about."}};// 随机选择单词srand(static_cast<unsigned int>(time(0)));int choice = (rand() % NUM_WORDS);string theWord = WORDS[choice][WORD]; // word to guessstring theHint = WORDS[choice][HINT]; // hint for word// 单词乱序string jumble = theWord; // jumbled version of wordint length = jumble.size();for (int i = 0; i < length; ++i){int index1 = (rand() % length);int index2 = (rand() % length);char temp = jumble[index1];jumble[index1] = jumble[index2];jumble[index2] = temp;}// 欢迎界面cout << "\t\t\tWelcome to Word Jumble!\n\n";cout << "Unscramble the letters to make a word.\n";cout << "Enter 'hint' for a hint.\n";cout << "Enter 'quit' to quit the game.\n\n";cout << "The jumble is: " << jumble;string guess;cout << "\n\nYour guess: ";cin >> guess;// 游戏主循环while ((guess != theWord) && (guess != "quit")){if (guess == "hint"){cout << theHint;}else{cout << "Sorry, that's not it.";}cout << "\n\nYour guess: ";cin >> guess;}// 游戏结束if (guess == theWord){cout << "\nThat's it!  You guessed it!\n";}cout << "\nThanks for playing.\n";return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/737318.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【鸿蒙 HarmonyOS 4.0】通知

一、介绍 通知旨在让用户以合适的方式及时获得有用的新消息&#xff0c;帮助用户高效地处理任务。应用可以通过通知接口发送通知消息&#xff0c;用户可以通过通知栏查看通知内容&#xff0c;也可以点击通知来打开应用&#xff0c;通知主要有以下使用场景&#xff1a; 显示接…

ctfshow web入门 php特性 web146-web150

1.web146 :被过滤了&#xff0c;三元运算符用不了&#xff0c;还可以用位运算符&#xff0c;逻辑运算符,等&#xff0c;逻辑运算符要注意或运算符的短路性 eval(return 1|phpinfo()|1) eval(return 1phpinfo()|1) payload&#xff1a; v11&v20&v3(~%8C%86%8C%8B%9A%92…

粉嘟嘟的免费wordpress模板

粉色好看的wordpress免费模板&#xff0c;用免费wordpress模板也可以搭建网站。 https://www.wpniu.com/themes/11.html

产品开发流程的意义:确保事情做正确——以苹果公司的iPhone为例

产品开发流程的意义&#xff1a;确保事情做正确——以苹果公司的iPhone为例 在当今高度竞争的商业环境中&#xff0c;产品开发流程的重要性愈发凸显。一个精心设计和执行的产品开发流程不仅确保了产品开发的顺利进行&#xff0c;同时也为产品的质量和市场竞争力提供了坚实的保…

解释一下分库分表的概念和优缺点。如何设计一个高性能的数据库架构?

解释一下分库分表的概念和优缺点。 分库分表是数据库架构优化的常见手段&#xff0c;主要用于解决单一数据库或表在数据量增大、访问频率提高时面临的性能瓶颈和扩展性问题。 概念&#xff1a; 分库&#xff08;Sharding-Database&#xff09;&#xff1a; 将原本存储在一个…

Anaconda 的一些配置

Anaconda 安装及修改环境默认位置 https://blog.csdn.net/qq_54562136/article/details/128932352 最重要的一步&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;改文件夹权限 Anaconda创建、激活、退出、删除虚拟环境 修改pip install 默认安装路径

C++ string类模拟实现

文章目录 构造函数拷贝构造函数析构流插入<<流提取>>begin()end()[]find()insert()push_back()appned()c_str()获取私有成员变量resize()reserve()<erase()完整代码 构造函数 string(const char* str ""):_size(strlen(str)){_str new char[strlen(…

牛客周赛 31

牛客周赛 Round 31 文章目录 牛客周赛 Round 31A 小红小紫替换B 小红的因子数C 小红的字符串中值D 小红数组操作E 小红的子集取反 A 小红小紫替换 语法 #include <bits/stdc.h>using namespace std;int main() {string s;cin >> s;if(s "kou"){cout &…

C# Path 类

在 C# 中&#xff0c;Path 类位于 System.IO 命名空间中&#xff0c;提供了一组用于操作和处理文件路径的静态方法。Path 类可以用于处理文件名、目录名和路径等相关操作。 下面是一些 Path 类常用的方法和示例说明&#xff1a; Path.Combine&#xff1a;用于组合多个字符串片…

docker开机启动设置

添加开机启动 # 添加开机启动配置 sudo vim /usr/lib/systemd/system/docker.service 文件内容 [Unit] DescriptionDocker Application Container Engine Documentationhttps://docs.docker.com Afternetwork-online.target firewalld.service Wantsnetwork-online.target […

【ros2 control 机器人驱动开发】双关节多控制器机器人学习-example 6

【ros2 control 机器人驱动开发】双关节多控制器机器人学习-example 6 文章目录 前言一、创建controller相关二、逻辑分析RRBotModularJoint类解析ros2_control.xacro解析三、测试运行测试forward_position_controller总结前言 本篇文章在上篇文章的基础上主要讲解双轴机器人驱…

uView Subsection 分段器

该分段器一般用于用户从几个选项中选择某一个的场景 #平台差异说明 App&#xff08;vue&#xff09;App&#xff08;nvue&#xff09;H5小程序√√√√ #基本使用 通过list数组参数传递分段的选项&#xff0c;数组元素可为字符串&#xff0c;或者通过keyName参数传入对象(默…

你真的用对了知识管理系统了吗?这篇文章教会你

面对信息化社会的信息爆炸&#xff0c;知识管理系统如同一艘救生船&#xff0c;帮助我们捕捉、储存、共享重要的知识&#xff0c;并提高工作效率。但是&#xff0c;你真的用对了知识管理系统吗&#xff1f;让这篇文章成为你的参考指南。 了解知识管理系统的功能导则 首先&…

数据通信练习题

1.0osi七层模型 应用层 data 表示层 会话层 传输层 数据段 防火墙&#xff0c;端口&#xff08;TCP UDP&#xff09; 网络层 数据包 路由器 数据链路层 数据帧 交换机 物理层 比特流 网卡 2.IP地址分类 私有地址 A类 0--127 10.0.0.0…

vue学习笔记23-组件事件⭐

组件事件 在组件的模板表达式中&#xff0c;可以直接使用$emit方法触发自定义事件&#xff1b;触发自定义事件的目的是组件之间传递数据 好好好今天又碰到问题了&#xff0c;来吧来吧 测试发现其他项目都可以 正常的run ,就它不行 搜索发现新建项目并进入以后&#xff0c;用指…

数据结构->双向链表带你体验开火车(哨兵)与拼接火车(应用)厢的乐趣

✅作者简介&#xff1a;大家好&#xff0c;我是橘橙黄又青&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;橘橙黄又青-CSDN博客 目的&#xff1a;学习双向带头链表的增&#xff0c;删&#xff0c;查&#xff0c;销毁…

代码随想录算法训练营第41天|背包理论基础 滚动数组法 416.分割等和子集

背包理论基础 终于开始刷大名鼎鼎的背包问题了&#xff0c;背包问题有点复杂&#xff0c;我还是懂了但没完全懂&#xff0c;下面是我现在的理解&#xff0c;我们把dp[i][j]定义为i个物品在背包容量为j下的价值&#xff0c;那么这样就转换为如果要求价值&#xff0c;这个价值和前…

链表——单链表的C实现(保姆级代码、注释教学)

首先需要借助三个文件 SList.h SList.c test.c 目录 SList.h &#xff1a;用来建立结构体、头文件、函数声明、全局变量建立 SList.c&#xff1a;对头文件中声明的函数的实现 void SLTPrint(SLTNode* phead) SLTNode* BuyLTNode(SLTDataType x) voi…

《软件项目管理:从规划到实施的全面指南》

软件项目管理是当今信息技术领域中至关重要的一环。在一个数字化、信息化的时代&#xff0c;几乎所有行业都依赖于软件来支撑其业务流程和运营。然而&#xff0c;软件开发过程往往是复杂的&#xff0c;充满了挑战和风险。为了确保软件项目能够按时交付、在预算范围内完成&#…

『大模型笔记』大模型中的Scaling Law(规模法则)

大模型中的Scaling Law(规模法则) 文章目录 一. 核心结论二. 大模型中的Scaling Law三. 参考文献Scaling Laws简单介绍就是:随着模型参数量大小、数据集大小和用于训练的浮点数计算量的增加,模型的性能会提高。并且为了获得最佳性能,所有三个因素必须同时放大。当不受其他两…