猜数字游戏

游戏说明:
电脑随机产生一个数字不重复的四位数。电脑会将您提交的数与它自动产生的数进行比较,结果显示成"*A*B"。A代表位置正确数字也正确,B代表数字正确但位置不正确,比如:"2A2B"表示您有2个数字的位置正确且数值也正确,除此以外,您还猜对了2个数字,但位置不对。
 
程序源码:
/*
	主题:猜数字游戏
	作者:karl
	邮箱:nixindong@hotmail.com
	开发环境:Visual Studio
	开发语言:C
	日期:2012-06-20
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>

void guess (int n);
void gotoxy(int x, int y);

int main(void)
{
	int i, n;
	while(1)
	{
		system("cls");
		gotoxy(15, 6);
		printf("I start game?(y/n)");
		gotoxy(15, 8);
		printf("2.Rule");
		gotoxy(15, 10);
		printf("3.exit\n");
		gotoxy(25, 15);
		printf("please choose:");
		scanf("%d", &i);
		//功能选择
		switch(i)
		{
		case 1:
			system("cls");
			printf("please input n:\n");
			scanf("%d", &n);
			guess(n);
			Sleep(5);
			break;
		
		case 2:
			system("cls");
			printf("\t\tThe Rules Of The Game\n");
			printf("step1:input the number of digits\n");
			printf("step2:input the number, separated by a space between two numbers\n");
			printf("step3:A represent location and data are collect\n");
			printf("\tB repreent location is correct but data is wrong!\n");
			Sleep(10);
			break;
		case 3:
			exit(0);
		default:
			break;
		}

	}

	return 0;
}

/*
	函数名称:guess
	函数功能:实现猜数字的功能
	函数参数:
		n:需要猜的数字个数
	返回值:类型(void)
*/
void guess (int n)
{
	int acount, bcount;
	int i, j, k = 0, flag; 
	/*
		数组a用来存放系统随机生成的数字
		数组b用来存放用户输入的数字
	*/
	int a[10], b[10];

	memset(a, 0, sizeof(a));
	memset(b, 0, sizeof(b));

	/*
		系统生成随机数
	*/
	do 
	{
		flag = 0;
		/*
			利用系统时间作为随机数种子
		*/
		srand((unsigned)time(NULL));
		//产生的随机数的个数根据输入的n来决定
		for (i = 0; i < n; i++)
		{
			//每次产生0~9范围内任意的一个随机数并存到数组中
			a[i] = rand() % 10;
		}

		for (i = 0; i < n-1; i++)
		{
			for (j = i+1; j < n; j++)
			{
				//判断数组a中是否有相同的数字
				if (a[i] == a[j])
				{
					flag = 1;
					continue;
				}
			}
		}
	} while (1 == flag);
	/*
		用户开始猜
	*/
	do 
	{
		k++;						//记录猜数字的次数
		acount = 0;
		bcount = 0;
		printf("guess");
		
		for (i = 0; i < n; i++)
		{
			scanf("%d", &b[i]);
		}
		
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < n; j++)
			{
				if (a[i] == b[i])
				{
					acount++;		//如果位置相同,且数字也相同,则acount++
					break;
				}
				if (a[i] == b[j] && i != j)
				{
					bcount++;		//如果位置不同,但有数字也相同,则acount++
					break;
				}
			}
		}
			printf("clue on:%d A %d B\n\n", acount, bcount);
			if (acount == n)									//判断是否在正确的位置猜中正确的数字
			{
				if (1 == k)
				{
					printf("you are the topmost rung of Fortune's ladder!\n\n");
				}
				else if (k <= 5)
				{
					printf("your are genius!!\n\n");
				}
				else if (k <= 10)
				{
					printf("you are clerver!!\n\n");
				}
				else
				{
					printf("you need try hard!!\n\n");
				}
				break;								//用来跳出整个do....while()循环
		}
	} while (1);
}


/*
	函数名称:gotoxy
	函数功能:将光标移动到指定位置
	函数参数:
			x, y为文本屏幕的坐标
	返回值:类型(void)
*/
void gotoxy(int x, int y)//x为列坐标,y为行坐标
{
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值