猜数字游戏C语言编程
源码如下:
/* File: guess.c */
#include /* standard input & output support */
#include /* srand() rand() */
#include /* time() */
/* 宏定义 */
#define NUMBER_LENGTH 5 /* 随机数长度 */
#define NUMBER_LIMIT 10 /* 随机数限制, 每一位0-9 */
#define INPUT_LENTH 128 /* 输入缓冲区大小 */
char goal[NUMBER_LENGTH] = {0}; /* 保存随机数 */
char flag[NUMBER_LIMIT] = {0}; /* 保存随机数标志, 保证不重复 */
char input[INPUT_LENTH] = {0}; /* 保存输入 */
/* 初始化用于保存数据的数组 */
void initData()
{
int i = 0;
while (i < NUMBER_LENGTH)
goal[i++] = 0;
i = 0;
while (i < NUMBER_LIMIT)
{
flag[i++] = 0;
}
}
/* 初始化用于保存缓冲区的数组 */
void initBuffer()
{
int i = 0;
while (i < INPUT_LENTH)
input[i++] = 0;
}
/* 显示猜测结果 */
void display()
{
int count = 0;
int i = 0;
while (i < NUMBER_LENGTH)
{
if (input[i] == goal[i])
{
printf("%c", 'o');
count++;
}
else
{
printf("%c", 'x');
}
i++;
}
printf("
RIGHT: %d bit(s)
", count);
if (count == NUMBER_LENGTH)
{
printf("You win! The number is %s.
", goal);
exit(0);
}
}
/* 生成随机数 */
void general()
{
/* 以时间作为时间种子保证生成的随机数真正具有随机性质 */
srand((unsigned int)time(NULL));
int i = 0;
while (i < NUMBER_LENGTH)
{
char tmp;
do
{
tmp = '0' + ((i != 0) ? (rand() % 10) : (1 + rand() % 9));
} while (flag[tmp] != 0);
flag[tmp] = 1;
goal[i++] = tmp;
}
}
/* 输入方法,用于猜测 */
void guess()
{
printf("Please input the number you guessed:
");
scanf("%s", input);
display();
initBuffer();
}
/* 主函数,程序主框架 */
int main (int argc, const char * argv[])
{
initData();
initBuffer();
general();
while (1) guess();
return 0;
}
==============================================
运行结果见附图,希望我的回答能够对你有所帮助。
终端编码问题,我用了英文,但功能是完整的,图一乐:
#include
#include
#include
int main() {
int key, input;
srand(time(NULL));
key = rand() % 5 + 1;
printf("Guess who am I? (from 1 to 5, 0 for exit)
");
printf("Your answer: ");
while (1) {
scanf("%d%*c", &input);
if (input == 0) {
break;
} else if (input == key) {
printf("Yes! Congratulations!");
break;
} else {
printf("No... Try again!
Your answer: ");
}
}
return 0;
}
#include <math.h>
#include <time.h>
void main()
{
int magic,guess,k;
char over,c;
printf("退出游戏请输入z\n");
do
{
srand (time(NULL));
magic=rand()%100+1;
printf("请输入猜测的数字1~100\n");
for(k=0;guess!=magic;k++)
{
scanf("%d",&guess);
if (guess==magic)printf("恭喜,你猜对了!\n");
scanf("%c",&over);
if (over=='z')
{
printf("感谢你的参与!!\n");
exit(0);
}
if(guess<magic)printf("太小了!\n");
if(guess>magic)printf("太大了!\n");
}
printf("你猜了%d次\n",k);
printf("是否继续游戏y/n\n");
scanf("%c",&c);
}while(c=='y');
if(c=='n'||c=='z')
{
printf("感谢你的参与!!\n");
exit(0);
}
getch();
}
#include<stdio.h>
#include<math.h>
#include<time.h>
void main()
{
int i,j;
srand((unsigned)time(NULL));
i=rand()%100;
print("猜电脑出的一个100内的数:/n");
while(true)
{
scanf("%d/n",&j);
if(j==i) {print("猜中了,游戏结束/n");break;}
print("没猜中。请继续猜。/n");
}
}
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int i, j, life, maxrand;
char c;
void Start ();
void GetResults ();
void Start (){
i = 0;
j = 0;
life = 0;
maxrand = 6;
cout << "Select difficulty mode:\n";
cout << "1 : Easy (0-15)\n";
cout << "2 : Medium (0-30)\n";
cout << "3 : Difficult (0-50)\n";
cout << "or type another key to quit\n";
c = 30;
cin >> c;
cout << "\n";
switch (c){
case '1' : maxrand = 15;
break;
case '2' : maxrand = 30;
break;
case '3' : maxrand = 50;
break;
default : exit(0);
break;
}
life = 5;
srand( (unsigned)time( NULL ) );
j = rand() % maxrand;
GetResults();
}
void GetResults (){
if (life <= 0){
cout << "You lose !\n\n";
Start();
}
cout << "Type a number: \n";
cin >> i;
if ((i>maxrand) || (i<0)){
cout << "Error : Number not between 0 and \n" << maxrand;
GetResults();
}
if (i == j){
cout << "YOU WIN !\n\n";
Start();
}
else if (i>j){
cout << "Too BIG\n";
life = life - 1;
cout << "Number of remaining life: " << life << "\n\n";
GetResults();
}
else if (i<j){
cout << "Too SMALL\n";
life = life - 1;
cout << "Number of remaining life:\n" << life << "\n\n";
GetResults();
}
}
int main ()
{
cout << "** Jackpot game **\n";
cout << "The goal of this game is to guess a number. You will be ask to type\n";
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 find\n\n";
Start();
return 0;
}
有个几年前编好的猜数字,但是代码找不到了,没有没有记忆成绩的功能,楼主要不?
如果要求界面就要用控件
C语言——猜数字小游戏 如何用rand,srand,time来完成随机数发生
在C语言中实现猜数字小游戏,关键在于生成随机数。实现这一功能,需要使用三个核心函数:rand,srand,以及time。rand函数用于生成随机数。当未调用srand函数时,rand默认以1播种。srand函数用于播种随机数生成器,每次调用时,必须提供一个种子值,这样可以生成相同的随机数序列。rand函数返回一个介于0到RAND...
用C语言编写一个“猜数字游戏”的程序
void main(){ int Win = rand() % 90 + 10; \/\/随机赋值 int i = 0;int n;char ch;printf("please input n\\n");scanf("%d",&n);while(1){ if(n > Win) \/\/猜的数字大了 { printf("you guess number big\\n");i++;scanf("%d",&n);} if(n < Win) \/\/猜的...
C语言 游戏 代码
void GameBegain(void);\/*游戏开始画面*\/ void DrawSmile(void);\/*画笑脸*\/ void DrawRedflag(int,int);\/*显示红旗*\/ void DrawEmpty(int,int,int,int);\/*两种空格子的显示*\/ void GameOver(void);\/*游戏结束*\/ void GameWin(void);\/*显示胜利*\/ int MineStatistics(int,int);\/*统计...
【c语言】编写一个猜数字游戏
include<stdio.h> include<stdlib.h> include void circle(int i){ int n ;printf("请输入您所猜的数字(0-100) \\n");scanf("%d",&n);if(i<n){ printf("the number is larger,input agine.\\n");circle(i);} if(i>n){ printf("the number is smaller.input agine \\n");circle...
c语言打数字游戏c语言小游戏
数字游戏是一个1~100的猜数游戏。程序会随机生成一个1~100的数字,然后玩家需要根据提示来猜测数字,直到猜中为止。程序将会输出“比它小”或“比它大”提示,直到数字被猜中。接着,我们需要为程序生成随机数。在C语言中,我们可以使用rand()函数来生成随机数。为了使每次生成的随机数不同,我们需要...
c语言编程 编一个猜数字游戏
printf("%c", 'x');} i++;} printf("\\nRIGHT: %d bit(s)\\n", count);if (count == NUMBER_LENGTH){ printf("You win! The number is %s.\\n", goal);exit(0);} } \/* 生成随机数 *\/ void general(){ \/* 以时间作为时间种子保证生成的随机数真正具有随机性质 *\/ srand((...
用C语言编写一个具有简单界面的猜数字游戏
分析:先产生一个随机数N。然后输入数I,如果i大于N,则提示大于信息。如果I小于N,则提示小于信息。直到I==N,则输出成功信息。这是我用C语言写的。环境:WIN-C ,TORBO C,如果是C++环境把倒数第二排getch();删掉!已经调试成功:main(){ int i=0,n;srand(time(0));n=rand()%100+1;w...
C语言题目 编程实现数字猜谜游戏!
然后询问是否继续进行猜数字游戏 下面是一个简单的代码 include <stdio.h>#include void clean_input_buf(void){ while(getchar() != '\\n');}int make_num(int num){ srand(num); return 1+rand()%20;}int guess_num(void){ int i=3; int n; int num; num=m...
猜数字游戏C语言编程
{ int magic,guess,k;char over,c;printf("退出游戏请输入z\\n");do { srand (time(NULL));magic=rand()%100+1;printf("请输入猜测的数字1~100\\n");for(k=0;guess!=magic;k++){ scanf("%d",&guess);if (guess==magic)printf("恭喜,你猜对了!\\n");scanf("%c",&over);if (...
用C语言编写的小游戏代码是什么?
void start( ); \/\/开始游戏 int main( ){ csh( );start( );} void csh( ) \/\/初始化 { printf("\\n\\n 猜 数 字 小 游 戏\\n\\n");printf(“ 猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.\\n”);} void start( ) \/\/开始游戏 {int m,n;...