#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++ 猜数字的更多相关文章

  1. C语言猜数字游戏

    猜数字游戏,各式各样的实现方式,我这边提供一个实现方式,希望可以帮到新手. 老程序猿就不要看了,黑呵呵 源代码1 include stdio.h include stdlib.h include ti ...

  2. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  3. java 猜数字游戏

    作用:猜数字游戏.随机产生1个数字(1~10),大了.小了或者成功后给出提示. 语言:java 工具:eclipse 作者:潇洒鸿图 时间:2016.11.10 >>>>> ...

  4. 【原创Android游戏】--猜数字游戏Version 0.1

    想当年高中时经常和小伙伴在纸上或者黑板上或者学习机上玩猜数字的游戏,在当年那个手机等娱乐设备在我们那还不是很普遍的时候是很好的一个消遣的游戏,去年的时候便写了一个Android版的猜数字游戏,只是当时 ...

  5. 【原创Android游戏】--猜数字游戏V1.1 --数据存储,Intent,SimpleAdapter的学习与应用

    --------------------------------------------------------------- V0.1版本 上次做完第一个版本后,发现还有一些漏洞,并且还有一些可以添 ...

  6. python学习笔记 ——python写的猜数字游戏 002

    from sys import exit import random def Arrfor(str): #CONTST = CONTST + 1 artificial = input("请输 ...

  7. Python小游戏之猜数字

    最近师兄师姐毕业,各种酒席,酒席上最常玩的一个游戏就是猜数字,游戏规则如下: 出题人在手机上输入一个0-100之间的数字,其它人轮流猜这个数字,如果你不幸猜中则要罚酒一杯.每次猜数字,出题人都要缩小范 ...

  8. 【Qt】2.4 做一个“猜数字”的游戏

    使用对话框和Qt设计师来实现一个相当简单的小游戏.同时将通过这个程序来看布局的隐藏和显示是如何来影响窗口界面的变化的. 新建一个Qt项目,把Qt Creator默认给的mainwindow.h.mai ...

  9. hihocoder 1169 猜数字

    传送门 时间限制:10000ms 单点时限:5000ms 内存限制:256MB 描述 你正在和小冰玩一个猜数字的游戏.小冰首先生成一个长为N的整数序列A1, A2, …, AN.在每一轮游戏中,小冰会 ...

  10. python写的第一个简单小游戏-猜数字

    #Filename:game1.py guess=10 running=True while running: try: answer=int(raw_input('Guess what i thin ...

随机推荐

  1. 前端vue框架上手记录

    ---恢复内容开始--- Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,提供: 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-se ...

  2. Windows下Redis安装及自启动

    Redis下载 Redis 官方网站没有提供 Windows 版的安装包,可以通过 GitHub 来下载 Windows 版 Redis 安装包,下载地址:点击前往. 打开上述的下载地址链接,Redi ...

  3. std::ref

    The std::thread constructor copies the supplied values, without converting to the expected argument ...

  4. HDFS相关问题处理

    机房搬迁后datanode启动失败,报错如下: 2022-10-21 10:28:40,551 INFO org.apache.hadoop.hdfs.server.common.Storage: L ...

  5. java初学者-向一个长度为5的整型数组中随机生成5个1-10的随机整数 ,要求生成的数字中没有重复数

    public static void main(String[]args){ //定义一个数组 长度为5:角标为4 int []arr=new int[5]; for(int i=0;i<5;i ...

  6. cmd 下运行pyhon文件.py

    第一步: wind+R打开[运行],输入cmd,点击确定 第二步: ①输入:[cd]指定pyhon文件目录 ② cd C:\Users\pc\Desktop\test ③在指定目录下输入pyhon文件 ...

  7. 【Unity】关于VS条件编译符号

    写在前面 起因:我在回顾LuaFramework_UGUI(作者Jarjin Lee)代码时,看到了C#代码中的条件编译符号,比如下图的ASYNC_MODE.虽然字面上知道是什么意思,但我对VS的条件 ...

  8. C Ⅷ

    数组  int number[100];   //这个数组可以放100个数 int x; int cnt = 0; double sum = 0; scanf("%d", & ...

  9. 实验3 C语言分支语句和循环语句编程应用

    任务一 #include<math.h> #include<stdio.h> int main() { float a,b,c,x1,x2; float delta,real, ...

  10. 01 ansible的基本介绍

    1.现有的企业服务器环境 在现在的企业中,特别是互联网公司,他们的业务量众多:比如负载均衡服务器.web服务器.动态解析(php)服务器.数据库(mysql)服务器以及网站缓存服务器,等等: 例如:一 ...