【猜数字】

【游戏描述】:

随机产生一个由0~9构成的各位不重复的n位数,由你来猜测,每一次猜测的结果以类似2A1B的形式反馈,表示你的猜测中有2个数字是完全正确的,有1个只是数值正确,但是没有出现在正确的位置上,来试试吧。


【输入】

根据程序提示。


【输出】

根据题意描述。


【输入的鲁棒性】

即支持包含空格在内的有且仅有n个数字的长度不超过100的字符串,如果数字不够或者出现非数字也非空格的字符,根据程序提示重新输入。


【随机两两不重复的n位数的构造】

使用rand(),用一个数组num[10]存放0~9,每次随机产生[ i , 9 ]的下标x,再把num[x]和num[i]交换,i从0开始自增,到i=n-1时停止。


【代码】

/*猜数字小游戏*/ 
#include<stdio.h>
#include<string.h>
#include<iostream> 
#include<stdlib.h>
#include<time.h>
#define random(x,y) (x+rand()%(y-x+1))//构造属于区间[x,y]的随机数 
using namespace std;
int num[10]={0,1,2,3,4,5,6,7,8,9};
int guess[20]; //记录输入的数字 
char s[100]; //输入行 
int both_right = 0,half_right = 0;
int over=0; //游戏是否结束 
int input_num ; //记录输入的数字个数 
int step=0; //记录猜测的次数
int level; //猜测的位数 

/**
* temp为从 i 到 9 选取的随机数  
* 交换下标为 i 和 temp 的数 
* 则第 i 个数确定 
*/ 
void create()
{
	srand((int)time(0));
	int temp;
	for(int i = 0;i < level;i++)
	{
		temp=random(i,9);
		swap(num[temp],num[i]);
	}
//	for(int i = 0;i < level;i++)
//		printf("%d ",num[i]);
	printf("告诉我你猜的数字吧!\n");
}

/**
*一行输入,空格随意,但是能且仅能输入level个数字,否则需要重新输入 
*/
void sf()
{
	gets(s);
	input_num=0;
	for(int i = 0;i < strlen(s);i++)
	{
		if(s[i] == ' ')  
			continue;
		else if('0' <= s[i] && s[i] <= '9')
		{
			guess[input_num] = s[i]-48;
			input_num++;
		}
		else 
		{
			printf("叫你好好输入数字,重新来!\n");
			input_num = 0;
			sf();
			break;
		}
	}
	if(input_num != level)
	{
		printf("只能输入%d个数字,重新来!\n",level);
		input_num = 0;
		sf();
	}
}

/**
*both_right的数值表示有多少个数字是完全正确的,half_right的数值表示有多少个数字是仅仅数值正确但是不在正确位置上 
*/
void solve()
{
	sf();
	both_right = half_right = 0;
	for(int i = 0;i < level;i++)
	{
		for(int j = 0;j < level;j++)
		{
			if(num[i]==guess[j])
			{
				if(i==j)
					both_right++;
				else
					half_right++;
			}
		}
	}
	if(both_right == level)
	{
		over=1;
		printf("STEP %d : You Win !\n",++step);
	}
	else
		printf("STEP %d : Ooops,please try again...  %d A %d B \n",++step,both_right,half_right);
}
int main()
{
	printf("选择猜测的位数吧!\n");
	scanf("%d",&level);
	getchar();
	create();
	while(over == 0)
		solve();
	return 0;
} 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#编写的GUI游戏 源代码 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace gn { public partial class Form1 : Form { string ready; int counti = 1; public Form1() { InitializeComponent(); this.label1.Hide(); this.button2.Hide(); this.button1.Hide(); this.button3.Hide(); this.button4.Hide(); this.textBox1.Hide(); this.pictureBox1.Hide(); this.pictureBox2.Hide(); } private void button1_Click(object sender, EventArgs e) { string number; int x, y; int count = 0; number = textBox1.Text; if (number.Length != 4) { MessageBox.Show("请输入4位数字"); } else { if (counti <=8) { if (count <= 3) { x = 0; y = 0; for (int i = 0; i <= 3; i++) { for (int j = 0; j <= 3; j++) { if (number[i] == ready[j]) { if (i == j) ++x; else ++y; } } } label2.Text = "你已经猜过" + counti + "次"; ++count; label3.Text = x.ToString() + "A" + y.ToString() + "B"; if (number == ready) { this.label2.Text = "答对了"; this.pictureBox1.Show(); } } if (counti == 8) { this.button1.Enabled = false; this.pictureBox2.Show(); } } else { this.button1.Enabled = false; this.pictureBox2.Show(); } ++counti; } } private void button2_Click(object sender, EventArgs e) { MessageBox.Show(ready); } private void button3_Click(object sender, EventArgs e) { //this.button2.Hide(); this.textBox1.Text = ""; this.label3.Text = ""; this.label2.Text = ""; this.button1.Enabled = true; counti = 1; this.pictureBox1.Hide(); this.pictureBox2.Hide(); Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; } private void label1_Click(object sender, EventArgs e) { } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void button4_Click(object sender, EventArgs e) { Close(); } private void button5_Click(object sender, EventArgs e) { Random r = new Random(); string[] a; a = new string[8]; a[0] = "1263"; a[1] = "6598"; a[2] = "9654"; a[3] = "5986"; a[4] = "2915"; a[5] = "2046"; a[6] = "9035"; a[7] = "1057"; int z; z = r.Next(8); ready = a[z]; this.textBox1.Show(); this.button1.Show(); this.button3.Show(); this.button4.Show(); this.label1.Show(); this.button2.Show(); this.button5.Hide(); } private void label4_Click(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void pictureBox1_Click_1(object sender, EventArgs e) { } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值