#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. c语言实现单链表的倒叙

    bool upsidedown_list(LinkList L) { Lnode *head, *tmp, *oldhead; head = L; tmp = L->next; oldhead ...

  2. Leetcode本地阅读器开发--01界面设计二

    返回项目声明及目录:Leetcode本地阅读器开发--总声明 继续上一节内容: 1.第一个内容是左边第一行的功能,读取默认路径和修改路径.此路径为本地阅读器的搜索题目的主目录. 为了整个程序都能读取工 ...

  3. iOS OC开发,文件的下载、预览

    /// 下载/打开 - (void)downloadActionWithDownloadString:(NSString *)downloadString{ //url : 下载地址 NSString ...

  4. OPENSSL 生成RSA公钥、私钥和证书

    在命令窗口执行下列操作. 1)生成RSA私钥: openssl genrsa -out rsa_private_key.pem 2048 生成内容: -----BEGIN RSA PRIVATE KE ...

  5. openlayers-1 下载及安装使用

    javascript - Import from in Openlayers - Geographic Information Systems Stack Exchange 在浏览器中运行开放层示例 ...

  6. Docker系列--容器编排工具Docker Compose详解

    1.Docker Compose 概述 Compose是一个用于定义和运行多容器Docker应用程序的工具.使用Compose,您可以使用Compose文件来配置应用程序的服务.然后,使用单个命令,您 ...

  7. 中值滤波 ordfilt2函数

    滤波是过滤掉信号中没用的波段 ordfilt2函数语法格式为:B=ordfilt2(A,order,domain)B=ordfilt2(A,order,domain,S)B=ordfilt2(..., ...

  8. uni-app中调用高德地图去设置点和轨迹

    盒子部分 <view style="width: 100%; height: 100%" id="busContainer"> </view& ...

  9. SQL优化:重新编译存储过程和表

    最近发现原来执行很快的存储过程,突然慢了下来,而很多存储过程每天就运行一次,所以打算把存储过程重新编译,另外,考虑到数据在不断变化,所以也要更新表的统计信息,这样能生成比较好的执行计划. 下面是具体的 ...

  10. create_generated_clock 的用法

    本文转载自:create_generated_clock_亓磊的博客-CSDN博客_create_generated_clock 文章目录 参数source和master_clock区别 create ...