我是靠谱客的博主 火星上铅笔,最近开发中收集的这篇文章主要介绍猜大小(游戏),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include <stdlib.h>
#include <time.h>

using namespace std;

void Start ();
void GetResults ();

double i,j,life;
int  maxrand;
char c;


void Start ()
{
     i = 0;
     j = 0;
     life = 0;
     maxrand = 6;

     cout << "Select difficulty mode:n"; // the user has to select a difficutly level
     cout << "1 : Easy (0-15)n";
     cout << "2 : Medium (0-30)n";
     cout << "3 : Difficult (0-50)n";
     cout << "or type another key to quitn";
     c = 30;

     cin >> c;                   // read the user's choice
     cout << "n";

     switch (c)
     {
        case '1' : maxrand = 15;  // the random number will be between 0 and maxrand
        break;
        case '2' : maxrand = 30;
        break;
        case '3' : maxrand = 50;
        break;
        default : exit(0);
        break;
     }

     life = 5;         // number of lifes of the player
     srand( (unsigned)time( NULL ) ); // init Rand() function
     j = rand() % maxrand;  // j get a random value between 0 and maxrand

     GetResults();

}


void
GetResults ()
{
     if (life <= 0)
        // if player has no more life then he lose
     {
        cout << "You lose !nn";
        Start();
     }

     cout << "Type a number: n";
     cin >> i;          // read user's number

     if ((i>maxrand) || (i<0)) // if the user number isn't correct, restart
     {
        cout << "Error : Number not between 0 and n" << maxrand;
        GetResults();
     }

     if (i == j)
     {
        cout << "YOU WIN !nn"; // the user found the secret number
        Start();
     }

     else if (i>j)
     {
        cout << "Too BIGn";
        life = life - 1;    // -1 to the user's "life"
        cout << "Number of remaining life: " << life << "nn";
        GetResults();
     }

     else if (i<j)
     {
        cout << "Too SMALLn";
        life = life - 1;
        cout << "Number of remaining life:n" << life << "nn";
        GetResults();
     }
}


int main ()
{
     cout << "** Jackpot game **n";
     cout << "The goal of this game is to guess a number. You will be ask to typen";
     cout << "a number (you have 5 guess)n";
     cout << "Jackpot will then tell you if this number is too big of too small compared to the secret number to findnn";
     Start();
     return 0;
}

转载于:https://www.cnblogs.com/Juruo-HJQ/p/9306975.html

最后

以上就是火星上铅笔为你收集整理的猜大小(游戏)的全部内容,希望文章能够帮你解决猜大小(游戏)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(87)

评论列表共有 0 条评论

立即
投稿
返回
顶部