美文网首页
简单猜数游戏的实现

简单猜数游戏的实现

作者: 琼Kevin | 来源:发表于2016-11-23 21:32 被阅读0次

1,项目说明


猜数字的游戏,系统随机生成一个数,用户输入一个数进去,直到猜对为止用一个选项让玩家继续玩下去

2,项目流程

1,输入:rand(),随机产生的一个数,srand(time(NULL)),撒下种子
          用户输入一个数,进行比较,并且是输出猜数是大是小
2,处理:while(1);直到相等输出字符N退出循环
        for(;count>0;cont--),三次循环机会
3,输出:猜数的结果

3,项目效果图

![DW_N1){54P8]4(4WM{Q{VTJ.jpg](https://img.haomeiwen.com/i2619158/40dd73f8d88620a6.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

4,项目源码

// Ex4.5  A Modified Guessing Game
#include <stdio.h>
#include <stdlib.h>                   
#include <time.h>                           // For time() function
#include <ctype.h>                          // I added this for toupper()
//#include <stdbool.h>                        // I added this for true

int main(void)
{
  int chosen = 0;                           // The lucky number
  int guess = 0;                            // Stores a guess
  const int max_tries = 3;                  // The maximum number of tries
  int limit = 20;                           // Upper limit for pseudo-random values
  char answer = 'N';

  srand(time(NULL));                        // Use clock value as starting seed
  printf("\nThis is a guessing game.\n");

  while(true)                              
  {
    chosen = 1 + rand() % limit;             // Random int 1 to limit
    printf("I have chosen a number between 1 and 20 which you must guess.\n");

    for(int count = max_tries ; count > 0 ; --count)
    {
      printf("\nYou have %d tr%s left.", count, count == 1 ? "y" : "ies");
      printf("\nEnter a guess: ");          // Prompt for a guess 
      scanf("%d", &guess);                  // Read in a guess

      // Check for a correct guess
      if(guess == chosen)
      {
        printf("\nCongratulations. You guessed it!\n");
        break;                              // End the program
      }
      else if(guess < 1 || guess > 20)      // Check for an invalid guess
        printf("I said the number is between 1 and 20.\n ");
      else
        printf("Sorry, %d is wrong. My number is %s than that.\n",
                            guess, chosen > guess ? "greater" : "less");
    }
    printf("\nYou have had three tries and failed. The number was %ld\n", chosen);
    printf("Do you want to play again (Y or N)?");
    scanf(" %c", &answer);
    if(toupper(answer) != 'Y')
      break;

  }
  return 0;
}



相关文章

  • 简单猜数游戏的实现

    1,项目说明 猜数字的游戏,系统随机生成一个数,用户输入一个数进去,直到猜对为止用一个选项让玩家继续玩下去 2,项...

  • Python 实现猜数游戏(基础版)

    ··· Python高效编程这一节,我们介绍如何使用 Python 实现简单的猜数游戏。首先是打印菜单的功能:1....

  • NEUQ-Cpp-03-编程题

    7-1 简单的猜数字游戏[1] (4分) 简单的猜数字游戏是预先设置一个100以内的正整数作为被猜数,用户输入一个...

  • Python猜数小游戏

    今天给大家带来一个Python猜数小游戏,游戏十分简单,系统自动生成一个随机数,由玩家猜数,系统提示偏大或偏小,直...

  • DatistEQ秩事

    一次财务专业培训中,授课老师建议学员通过《数独》游戏来锻炼脑力。一款猜数字的游戏,规则很简单,在9*9的网格中猜数...

  • 猜数游戏

    #!/usr/bin/env python #_*_conding:utf-8_*_ age =22 count ...

  • 猜数游戏

    牛牛和羊羊在玩一个有趣的猜数游戏。在这个游戏中,牛牛玩家选择一个正整数,羊羊根据已给的提示猜这个数字。第i个提示是...

  • 猜数游戏

    这个游戏比较适合一二年级及其以下的孩子。 两人每人拿一张卡片,写下1-100之间的任何一个数字,把卡片叠好,互相交...

  • 2019-05-22

    python 猜数游戏

  • 猜数游戏(数独)

    【学习目标】 1.经历填数游戏活动,初步提高分析推理能 力。 2.在探索、尝试、交流等活动中,体会填数 游戏...

网友评论

      本文标题:简单猜数游戏的实现

      本文链接:https://www.haomeiwen.com/subject/lugvpttx.html