《C++游戏编程入门》第2章 真值、分支与游戏循环: Guess My Number

《C++游戏编程入门》第2章 真值、分支与游戏循环: Guess My Number

    • 2.1 关系运算符
    • 2.2 条件语句
        • 02.score_rater.cpp
        • 02.score_rater2.cpp
        • 02.score_rater3.cpp
    • 2.5 switch语句
        • 02.menu_chooser.cpp
    • 2.6 while循环
        • 02.play_again.cpp
    • 2.7 do循环
        • 02.play_again2.cpp
    • 2.8 break和continue语句
        • 02.finicky_counter.cpp
    • 2.9 逻辑运算符
        • 02.designers_network.cpp
    • 2.10 随机数
        • 02.die_roller.cpp
    • 2.12 Guess My Number游戏
        • 02.guess_my_number.cpp

2.1 关系运算符

==, !=
>, >=
<, <=

2.2 条件语句

02.score_rater.cpp
#include <iostream>
using namespace std;int main()
{if (true){cout << "This is always displayed.\n\n";}if (false)cout << "This is never displayed.\n\n";int score = 1000;if (score){cout << "At least you didn't score zero.\n\n";}if (score >= 250)cout << "You scored 250 or more. Decent.\n\n";if (score >= 500){cout << "You scored 500 or more. Nice.\n\n";if (score >= 1000)cout << "You scored 1000 or more. Impressive!\n";}return 0;
}
02.score_rater2.cpp
#include <iostream>
using namespace std;int main()
{int score;cout << "Enter your score: ";cin >> score;if (score >= 1000)cout << "You scored 1000 or more. Impressive!\n";elsecout << "You scored less than 1000.\n";return 0;
}
02.score_rater3.cpp
#include <iostream>
using namespace std;int main()
{int score;cout << "Enter your score: ";cin >> score;if (score >= 1000){cout << "You scored 1000 or more. Impressive!\n";}else if (score >= 500){cout << "You scored 500 or more. Nice.\n";}else if (score >= 250){cout << "You scored 250 or more. Decent.\n";}else{cout << "You scored less than 250. Nothing to brag about.\n";}return 0;
}

2.5 switch语句

只用于比较int型(或可当作int型处理的值,char型或枚举型)。

02.menu_chooser.cpp
#include <iostream>
using namespace std;int main()
{cout << "Difficulty Levels\n\n";cout << "1 - Easy\n";cout << "2 - Normal\n";cout << "3 - Hard\n\n";int choice;cout << "Choice: ";cin >> choice;switch (choice){default:cout << "You made an illegal choice.\n";break;case 1:cout << "You picked Easy.\n";break;case 2:cout << "You picked Normal.\n";break;case 3:cout << "You picked Hard.\n";break;}return 0;
}

2.6 while循环

02.play_again.cpp
#include <iostream>
using namespace std;int main()
{char again = 'y';while (again == 'y'){cout << "\n**Played an exciting game**";cout << "\nDo you want to play again? (y/n): ";cin >> again;}cout << "\nOkay, bye.";return 0;
}

2.7 do循环

02.play_again2.cpp
#include <iostream>
using namespace std;int main()
{char again;do{cout << "\n**Played an exciting game**";cout << "\nDo you want to play again? (y/n): ";cin >> again;} while (again == 'y');cout << "\nOkay, bye.";return 0;
}

2.8 break和continue语句

02.finicky_counter.cpp
#include <iostream>
using namespace std;int main()
{int count = 0;while (true){count += 1;// end loop if count is greater than 10if (count > 10){break;}// skip the number 5if (count == 5){continue;}cout << count << endl;}return 0;
}

2.9 逻辑运算符

!
&&
||
注意优先级和短路现象

02.designers_network.cpp
#include <iostream>
#include <string>
using namespace std;int main()
{string username;string password;bool success;cout << "\tGame Designer's Network\n";do{cout << "\nUsername: ";cin >> username;cout << "Password: ";cin >> password;if (username == "S.Meier" && password == "civilization"){cout << "\nHey, Sid.";success = true;}else if (username == "S.Miyamoto" && password == "mariobros"){cout << "\nWhat's up, Shigeru?";success = true;}else if (username == "W.Wright" && password == "thesims"){cout << "\nHow goes it, Will?";success = true;}else if (username == "guest" || password == "guest"){cout << "\nWelcome, guest.";success = true;}else{cout << "\nYour login failed.";success = false;}} while (!success);return 0;
}

2.10 随机数

02.die_roller.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;int main()
{srand(static_cast<unsigned int>(time(0))); // seed random number generator based on current timeint randomNumber = rand(); // generate random numberint die = (randomNumber % 6) + 1; // get a number between 1 and 6cout << "You rolled a " << die << endl;return 0;
}

2.12 Guess My Number游戏

02.guess_my_number.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;int main()
{srand(static_cast<unsigned int>(time(0))); // seed random number generatorint secretNumber = rand() % 100 + 1; // 生成随机数int tries = 0;int guess;cout << "\tWelcome to Guess My Number\n\n";do{cout << "Enter a guess: ";cin >> guess; // 获取用户猜测++tries;	  // 增加猜测次数// 告知猜测情况if (guess > secretNumber){cout << "Too high!\n\n";}else if (guess < secretNumber){cout << "Too low!\n\n";}else{cout << "\nThat's it! You got it in " << tries << " guesses!\n";}} while (guess != secretNumber); // 是否猜对return 0;
}

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

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

相关文章

AHU 数据库 实验三

《数据库》实验报告 【实验名称】 实验3 数据库的连接查询 【实验目的】 1. 熟悉基本的连接查询的概念和作用&#xff1b; 2. 了解数据库管理系统DBMS 实现连接查询的基本方法&#xff1b; 3. 掌握SQL语言连接查询语句的语法和功能&#…

.NET CORE Aws S3 使用

1.安装指定的包 Install-Package AWSSDK.S3 -Version 3.3.104.10 2.使用帮助类 using System; using System.Collections.Generic; using System.Text; using Amazon; using Amazon.Runtime; using Amazon.S3; using Amazon.S3.Model; using System.IO; using System.Threadi…

Spring存储基础知识

一、对象存储 1.创建bean对象 public class User {public void sayHi() {System.out.println("hi student");} } 2.bean存入Spring 在spring-config.xml&#xff0c;将 bean&#xff08;com.spring.demo.User&#xff09;存到 Spring 容器中&#xff0c;它的名称…

【数据结构学习笔记】选择排序

【数据结构学习笔记】选择排序 参考电子书&#xff1a;排序算法精讲 算法原理 首先在未排序序列中找到最小&#xff08;大&#xff09;元素&#xff0c;存放到排序序列的起始位置&#xff0c;然后&#xff0c;再从剩余未排序元素中继续寻找最小&#xff08;大&#xff09;元…

读取CSV数据并写入MySQL

import pandas as pd #import tushare as ts from sqlalchemy import create_engineimport baostock as bs #### 登陆系统 #### lg bs.login() # 显示登陆返回信息 print(login respond error_code:lg.error_code) print(login respond error_msg:lg.error_msg) #### 获取沪深…

STM32第十课:串口发送

一、usart串口 1.1 USART串口协议 串口通讯(Serial Communication) 是一种设备间非常常用的串行通讯方式&#xff0c;因为它简单便捷&#xff0c;因此大部分电子设备都支持该通讯方式&#xff0c;电子工程师在调试设备时也经常使用该通讯方式输出调试信息。在计算机科学里&…

主流数据库的区别

几个主流的数据库有&#xff1a; 1. MySQL&#xff1a;MySQL是一种关系型数据库管理系统&#xff0c;常用于Web应用程序开发和数据存储。 2. Oracle&#xff1a;Oracle是一种关系型数据库管理系统&#xff0c;由Oracle Corporation开发和销售。它广泛用于企业级应用程序中。 …

在使用qml的qmldir文件创建常用组件报错unknow component

解决方法&#xff1a;Qt Creator中的工具-->QML/JS-->重置代码模型 参考博文:QML自定义模块及qmldir的使用_同一资源文件目录下的qml模块使用-CSDN博客 不一样的地方是我给我的文件起了别名 以及我的qrc文件路径有前缀/qml 总体操作&#xff1a; 1.使用模块中的组件时…

线程与进程的区别、协程

1【线程与进程的区别、协程】 【1】 进程跟线程 进程&#xff08;Process&#xff09;和 线程&#xff08;Thread&#xff09;是操作系统的基本概念&#xff0c; 但是它们比较抽象&#xff0c; 不容易掌握。关于多进程和多线程&#xff0c;教科书上对经典的一句话“进程是资源分…

铭文:探索比特币世界的数字印记

铭文是什么&#xff1f; 铭文指的是在某种物品&#xff08;如石头、硬币、平板等&#xff09;上刻有文字。在比特币领域&#xff0c;铭文指的是刻在聪&#xff08;satoshi&#xff09;上的元数据。比特币的最小单位是聪&#xff0c;1比特币可分为1亿聪。每个聪都通过序数理论进…

解决WSL2的ubuntu20.04中安装docker出现无法连接的问题(Cannot connect to the Docker daemon)

wsl2的ubuntu20.04系统安装docker可以参考官网教程操作&#xff0c;我个人喜欢参考其中的离线安装方式&#xff1a;Install from a package。只需要按照官网一步步操作即可&#xff0c;跟普通的ubuntu20.04的安装是一样的步骤。 在安装完以后&#xff0c;发现一旦使用docker相…

OpenAI GPT LLMs 高级提示词工程方法汇总

原文地址&#xff1a;An Introduction to Prompt Engineering for OpenAI GPT LLMs Github&#xff1a;Prompt-Engineering-Intro 2023 年 3 月 2 日 提示工程指南 | Prompt Engineering Guide Naive 提示词&#xff1a;带有提示的情感分类器 prompt Decide whether a T…

计算机缺失iutils.dll怎么办,分享5种靠谱的解决方法

​在计算机系统运行过程中&#xff0c;如果发现无法找到或缺失iutils.dll文件&#xff0c;可能会引发一系列的问题与故障。首先&#xff0c;由于iutils.dll是系统中一个重要的动态链接库文件&#xff0c;它的主要功能可能涉及到系统核心服务、应用程序支持或者特定功能模块的运…

互联网高频面:输入URL按下回车后,中间发生了什么

题目 输入URL按下回车后&#xff0c;中间发生了什么 这个问题其实是计算机网络里面很经典的一个问题&#xff0c;不能去死机硬背&#xff0c;很考察对网络架构和通信原理的理解&#xff0c;也是各个互联网大厂喜欢考察的面试题。 一些图片参考了小林的计算机网络面经 从输入…

“光谱视界革新:ChatGPT在成像光谱遥感中的智能革命“

遥感技术主要通过卫星和飞机从远处观察和测量我们的环境&#xff0c;是理解和监测地球物理、化学和生物系统的基石。ChatGPT是由OpenAI开发的最先进的语言模型&#xff0c;在理解和生成人类语言方面表现出了非凡的能力。本文重点介绍ChatGPT在遥感中的应用&#xff0c;人工智能…

LeetCode(力扣)算法题_2864_最大二进制奇数

最大二进制奇数 题目描述 给你一个 二进制 字符串 s &#xff0c;其中至少包含一个 1 。 你必须按某种方式 重新排列 字符串中的位&#xff0c;使得到的二进制数字是可以由该组合生成的 最大二进制奇数 。 以字符串形式&#xff0c;表示并返回可以由给定组合生成的最大二进…

爬虫案例2:playwright 超爽体验

参考链接&#xff1a;https://playwright.bootcss.com/python/docs/intro 目标网站&#xff1a;https://spa6.scrape.center/通过观察&#xff0c;页面的信息是通过Ajax请求后返回的信息 下面使用playwright实现绕过token的获取直接拿到返回的数据import asyncio import json f…

Docker安装达梦数据库(DM8)

安装目录 mkdir /opt/dm8 /opt/dm8/datadir && cd /opt/dm8 && chmod 777 /opt/dm8 /opt/dm8/datadir 下载镜像 yum -y install wget && wget https://download.dameng.com/eco/dm8/dm8_20230808_rev197096_x86_rh6_64_single.tar 导入镜像 docker loa…

深入挖掘C语言之——联合

目录 联合的定义 联合的特点 联合的应用场景 在C语言中&#xff0c;联合&#xff08;Union&#xff09;是一种特殊的数据结构&#xff0c;它允许在同一内存地址存储不同类型的数据。与结构体&#xff08;Struct&#xff09;不同的是&#xff0c;联合中的所有成员共享同一块内…

C语言--从零开始的扫雷游戏

C语言--从零开始的扫雷游戏 1. 游戏说明2. 总体代码3. 详细讲解3.1 菜单部分3.2 游戏主体部分3.2.1 总体分析3.2.2 棋盘初始化3.2.3 棋盘展示3.2.4 设置地雷3.2.5 扫雷阶段3.2.6 统计雷个数的代码3.2.7 使用迭代的方式进行展开&#xff1a;3.2.8 扫雷部分主体代码 4. 总结 1. 游…