C++ 猜数字
#include <iostream>
#include <random>
#include <limits>
namespace random
{
std::random_device rd;
std::seed_seq rr = {rd(), rd(), rd(), rd(), rd(), rd(), rd(), rd()};
std::mt19937 mt{rr};
int get(int min, int max)
{
std::uniform_int_distribution uid{min, max};
return uid(mt);
}
} // namespace random
int getGuess(int i)
{
while (true)
{
std::cout << "Guess #" << i << ":";
int guess{};
std::cin >> guess;
if (!std::cin || guess < 1 || guess > 100)
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Please enter the correct number!" << '\n';
continue;
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return guess;
}
}
bool playGame(int guesses, int random)
{
std::cout << "Let's play a game. I'm thinking of a number between 1 and 100. You have " << guesses << " tries to guess what it is. " << '\n';
for (int i{1}; i <= guesses; i++)
{
int guess{getGuess(i)};
if (random > guess)
std::cout << "Your guess is too low." << '\n';
else if (random < guess)
std::cout << "Your guess is too high." << '\n';
else // random == guess
return true;
}
return false;
}
bool playAgain()
{
while (true)
{
std::cout << "Would you like to play again (y/n)? ";
char ch{};
std::cin >> ch;
switch (ch)
{
case 'y':
return true;
case 'n':
return false;
default:
std::cout << "Please enter the correct option!" << '\n';
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
int main()
{
do
{
constexpr int guesses{7};
int num{random::get(1, 100)};
bool won{playGame(guesses, num)};
if (won)
{
std::cout << "Correct! You win!" << '\n';
}
else
{
std::cout << "Sorry, you lose. The correct number was " << num << "\n";
}
} while (playAgain());
return 0;
}
C++ 猜数字的更多相关文章
- C语言猜数字游戏
猜数字游戏,各式各样的实现方式,我这边提供一个实现方式,希望可以帮到新手. 老程序猿就不要看了,黑呵呵 源代码1 include stdio.h include stdlib.h include ti ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- java 猜数字游戏
作用:猜数字游戏.随机产生1个数字(1~10),大了.小了或者成功后给出提示. 语言:java 工具:eclipse 作者:潇洒鸿图 时间:2016.11.10 >>>>> ...
- 【原创Android游戏】--猜数字游戏Version 0.1
想当年高中时经常和小伙伴在纸上或者黑板上或者学习机上玩猜数字的游戏,在当年那个手机等娱乐设备在我们那还不是很普遍的时候是很好的一个消遣的游戏,去年的时候便写了一个Android版的猜数字游戏,只是当时 ...
- 【原创Android游戏】--猜数字游戏V1.1 --数据存储,Intent,SimpleAdapter的学习与应用
--------------------------------------------------------------- V0.1版本 上次做完第一个版本后,发现还有一些漏洞,并且还有一些可以添 ...
- python学习笔记 ——python写的猜数字游戏 002
from sys import exit import random def Arrfor(str): #CONTST = CONTST + 1 artificial = input("请输 ...
- Python小游戏之猜数字
最近师兄师姐毕业,各种酒席,酒席上最常玩的一个游戏就是猜数字,游戏规则如下: 出题人在手机上输入一个0-100之间的数字,其它人轮流猜这个数字,如果你不幸猜中则要罚酒一杯.每次猜数字,出题人都要缩小范 ...
- 【Qt】2.4 做一个“猜数字”的游戏
使用对话框和Qt设计师来实现一个相当简单的小游戏.同时将通过这个程序来看布局的隐藏和显示是如何来影响窗口界面的变化的. 新建一个Qt项目,把Qt Creator默认给的mainwindow.h.mai ...
- hihocoder 1169 猜数字
传送门 时间限制:10000ms 单点时限:5000ms 内存限制:256MB 描述 你正在和小冰玩一个猜数字的游戏.小冰首先生成一个长为N的整数序列A1, A2, …, AN.在每一轮游戏中,小冰会 ...
- python写的第一个简单小游戏-猜数字
#Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...
随机推荐
- 3Com-OfficeConnect-Wireless-11宽带路由器默认口令
网络空间资产搜索: app="3Com-OfficeConnect-Wireless-11g-Cable/DSL-Router" 找到环境 账户密码 admin/a***n End ...
- appium程序下载安装/appium desktop
官网地址:http://appium.io/ 点击下载按钮 默认跳转到最新版本,点击 Releases 回到版本列表页 该页可以看到对应的版本及更新时间,(最好不要下载最新版本) 如果是 Window ...
- ssh连接不上、Xshell意外关闭Socket error Event: 32 Error: 10053.
Xshell意外关闭可能会出现这种问题,如遇如下错误可解决: Connecting to 47.106.80.28:22- Connection established. To escape to l ...
- 使用LitJson输出格式化json文件到本地
百度上搜了半天,竟然没有C#使用LitJson格式化输出的例子,全都是Newtonsoft.Json的,最后在litjson的官网找到了方法. 给大家分享一下: https://litjson.net ...
- “jupyter notebook 不能导入python库但是终端上可以实现”的问题的解决
在使用jupyter notebook的过程中,创建了一个新的环境(anaconda中env)后遇到了这样一个问题,就是: 在jupyter notebook上运行程序,中间发现有一个python库未 ...
- AutoCAD_2020_Simplified_Chinese_Win_64bit_dlm
「AutoCAD_2020_Simplified_Chinese_Win_64bit_dlm」https://www.aliyundrive.com/s/zN8zAGgXZ58 点击链接保存,或者复制 ...
- 后台http请求
HttpResponse response = HttpContext.Current.Response; response.Buffer = true; response.Clear(); resp ...
- vue富文本(5版本)组件
<template> <div> <div style="border: 1px solid #ccc; width: 500px"> < ...
- 基于airtest验证Android端app是否安装及自动化安装
1.检测app是否安装: 使用check_app方法检测是否安装:为什么需要在封装一层做断言呢?主要check_app方法安装成功会返回True,但是未检测到安装时直接报异常了,停止执行.无法直接 ...
- JMeter参数化(一)--CSV参数化
一.CSV Data Set Config 1.添加配置元件-CSV Data Set Config 其中,分隔符不能是参数化的值中的符号,否则会被截断. 2.调用 3.循环读取文件中数据 假设数据内 ...