*/ --------------------------------------------------------------------------------------
*/ 出自: 编程中国 http://www.bc-cn.net
*/ 作者: andyzhshg
*/ 时间: 2007-9-1 编程论坛首发
*/ 声明: 尊重作者劳动,转载请保留本段文字
*/ --------------------------------------------------------------------------------------
/*这是一个模仿文曲星之类电子辞典上的猜数字游戏的小程序。
即由系统随即生成一个四位数字,然后由玩家输入四个自己猜测的数字,系统经判断给出你猜对了几个数字,包括数字大小和位置都与随机数相同的荷只有大小正确但位置不正确的。*/
//此程序在Bland C++ 6.0 下编译通过
//因为初学,所以显得很幼稚,多谢大家指教
//多谢7楼指点,声明语句中的numbercompare已改为numCompare
//我对这个程序进行了重写,详情请看“猜数字游戏重写版”,希望大家继续提出意见。
// NumberGuess.c
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define CRAET_SEED_A 1000
#define CRAET_SEED_B 2000
#define CRAET_SEED_C 3000
#define CRAET_SEED_D 4000
#define RIGHT_0_PLACE_0 0
#define RIGHT_1_PLACE_0 1
#define RIGHT_2_PLACE_0 2
#define RIGHT_3_PLACE_0 3
#define RIGHT_4_PLACE_0 4
#define RIGHT_0_PLACE_1 5
#define RIGHT_1_PLACE_1 6
#define RIGHT_2_PLACE_1 7
#define RIGHT_3_PLACE_1 8
#define RIGHT_0_PLACE_2 9
#define RIGHT_1_PLACE_2 10
#define RIGHT_2_PLACE_2 11
#define RIGHT_0_PLACE_3 12
#define RIGHT_1_PLACE_3 13
#define RIGHT_0_PLACE_4 14
#define TRUE 1
#define FALSE 0
int main (void)
{
int rndProduce( int ); //随机生成四位数字
int dialog ( int, int, int, int ); //输入对话
int numCompare(int, int, int, int, int, int, int, int); //数值比较
int num1, num2, num3, num4;
int numGuess1, numGuess2, numGuess3, numGuess4, numBackD, numBackC;
int cnt1, cnt2, cnt3, cnt4;
int numRight, numPlace;
int goOnConfirm ;
printf ( "***********************************************************\n" );
printf ( "欢迎你玩猜数字游戏!\n" ) ;
printf ( "***********************************************************\n" );
printf ( "版本: 1.0\n作者: Andyzhshg\n制作日期: 2007.08.14\n" );
printf ( "***********************************************************\n" );
NEXT : num1 = rndProduce ( CRAET_SEED_A ) ;
num2 = rndProduce ( CRAET_SEED_B ) ;
num3 = rndProduce ( CRAET_SEED_C ) ;
num4 = rndProduce ( CRAET_SEED_D ) ;
//printf("%i %i %i %i \n",num1, num2, num3, num4 ); //测试语句
printf ( "***********************************************************\n" );
printf ( "系统已经生成了一个随机的四位数字\n" ) ;
HERE: printf("请输入你预测的四位数字(每输入一个数字按一次回车键):\n");
printf ( "***********************************************************\n" );
scanf("%i %i %i %i",&numGuess1, &numGuess2, &numGuess3, &numGuess4);
//printf("\n%i%i%i%i",numGuess1, numGuess2, numGuess3, numGuess4); //测试语句
numBackD = dialog(numGuess1, numGuess2, numGuess3, numGuess4);
if ( numBackD == 1 )
{
numBackC = numCompare(num1, num2, num3, num4, numGuess1, numGuess2, numGuess3, numGuess4);
if ( numBackC == RIGHT_0_PLACE_0 )
{
numRight = 0;
numPlace = 0;
}
if ( numBackC == RIGHT_1_PLACE_0 )
{
numRight = 1;
numPlace = 0;
}
if ( numBackC == RIGHT_2_PLACE_0 )
{
numRight = 2;
numPlace = 0;
}
if ( numBackC == RIGHT_3_PLACE_0 )
{
numRight = 3;
numPlace = 0;
}
if ( numBackC == RIGHT_4_PLACE_0 )
{
numRight = 4;
numPlace = 0;
}
if ( numBackC == RIGHT_0_PLACE_1 )
{
numRight = 0;
numPlace = 1;
}
if ( numBackC == RIGHT_1_PLACE_1 )
{
numRight = 1;
numPlace = 1;
}
if ( numBackC == RIGHT_2_PLACE_1 )
{
numRight = 2;
numPlace = 1;
}
if ( numBackC == RIGHT_3_PLACE_1 )
{
numRight = 3;
numPlace = 1;
}
if ( numBackC == RIGHT_0_PLACE_2 )
{
numRight = 0;
numPlace = 2;
}
if ( numBackC == RIGHT_1_PLACE_2 )
{
numRight = 1;
numPlace = 2;
}
if ( numBackC == RIGHT_2_PLACE_2 )
{
numRight = 2;
numPlace = 2;
}
if ( numBackC == RIGHT_0_PLACE_3 )
{
numRight = 0;
numPlace = 3;
}
if ( numBackC == RIGHT_1_PLACE_3 )
{
numRight = 1;
numPlace = 3;
}
if ( numBackC == RIGHT_0_PLACE_4 )
{
numRight = 0;
numPlace = 4;
}
printf ( "***********************************************************\n" );
printf ( "你猜测的数字中:\n有 %i 个位置和数值都正确,\n有 %i 个数值正确但位置不正确。\n", numRight, numPlace);
printf ( "***********************************************************\n" );
if ( numRight == 4 )
{
printf ( "恭喜你,你答对了!\n正确打案就是 %i %i %i %i \n",numGuess1, numGuess2, numGuess3, numGuess4);
printf ( "***********************************************************\n" );
printf ( "继续游戏吗? 继续请按1,退出请按2.\n" );
scanf ( "%i" , &goOnConfirm ) ;
printf ("\n") ;
printf ( "***********************************************************\n" );
if ( goOnConfirm == 1 )
goto NEXT ;
if ( goOnConfirm == 2 )
{
printf ( "***********************************************************\n" );
printf ( "感谢你玩本游戏,请按回车键退出。\n" );
printf ( "***********************************************************\n" );
}
getchar ( );
}
if ( numRight !=4 )
{
printf ( "***********************************************************\n" );
printf ( "还要继续吗?继续请按1,显示正确答案请按2。\n" );
scanf ( "%i" , &goOnConfirm ) ;
printf ("\n") ;
printf ( "***********************************************************\n" );
if ( goOnConfirm == 1 )
goto HERE;
if ( goOnConfirm == 2 )
{
printf ( "***********************************************************\n" );
printf ( "正确答案是: %i %i %i %i ,再接再厉哦!\n" , num1, num2, num3, num4 ) ;
printf ( "***********************************************************\n" );
printf ( "继续游戏吗? 继续请按1,退出请按2.\n" );
scanf ( "%i" , &goOnConfirm ) ;
printf ("\n") ;
printf ( "***********************************************************\n" );
if ( goOnConfirm == 1 )
goto NEXT ;
if ( goOnConfirm == 2 )
{
printf ( "***********************************************************\n" );
printf ( "感谢你玩本游戏,请按回车键退出。\n" );
printf ( "***********************************************************\n" );
}
getchar ( );
}
}
}
if ( numBackD == 2 )
goto HERE;
getchar();
return 0;
}
int rndProduce(int seedProduce)
{
float seed;
//种子的初始化,调用时间函数
srand( (unsigned)time( NULL ) + seedProduce );
seed=( (float)rand() ) / ( (float)RAND_MAX );
if (seed >= 0 && seed < 0.1)
return 0;
if (seed >= 0.1 && seed < 0.2)
return 1;
if (seed >= 0.2 && seed < 0.3)
return 2;
if (seed >= 0.3 && seed < 0.4)
return 3;
if (seed >= 0.4 && seed < 0.5)
return 4;
if (seed >= 0.5 && seed < 0.6)
return 5;
if (seed >= 0.6 && seed < 0.7)
return 6;
if (seed >= 0.7 && seed < 0.8)
return 7;
if (seed >= 0.8 && seed < 0.9)
return 8;
if (seed >= 0.9)
return 9;
}
int dialog ( int value1, int value2, int value3, int value4 )
{
int putin;
printf ( "***********************************************************\n" );
printf ("你输入的数字是: %i %i %i %i \n确认请按1,重新输入请按2\n",value1, value2, value3, value4);
printf ( "***********************************************************\n" );
scanf("%i",&putin);
if ( putin == 1 )
{
printf ( "***********************************************************\n" );
printf("你已经确认了你的输入值,它是 %i %i %i %i \n",value1, value2, value3, value4);
printf ( "***********************************************************\n" );
return 1;
}
if ( putin == 2 )
{
printf ( "***********************************************************\n" );
printf("你放弃了这组预测值:\n");
printf ( "***********************************************************\n" );
return 2;
}
}
int numCompare(int val_a1, int val_a2, int val_a3, int val_a4, int val_b1, int val_b2, int val_b3, int val_b4)
{
int place = 0, right = 0;
int check_a1 = TRUE, check_a2 = TRUE, check_a3 = TRUE, check_a4 = TRUE, check_b1 = TRUE, check_b2 = TRUE, check_b3 = TRUE, check_b4 = TRUE;
// 检查数字和位置都正确的数字个数
if ( check_a1 == TRUE && check_b1 == TRUE && val_a1 == val_b1 )
{
right = right +1 ;
check_a1 = FALSE ;
check_b1= FALSE ;
}
if ( check_a2 == TRUE && check_b2 == TRUE && val_a2 == val_b2 )
{
right = right +1 ;
check_a2 = FALSE ;
check_b2= FALSE ;
}
if ( check_a3 == TRUE && check_b3 == TRUE && val_a3 == val_b3 )
{
right = right +1 ;
check_a3 = FALSE ;
check_b3= FALSE ;
}
if ( check_a4 == TRUE && check_b4 == TRUE && val_a4 == val_b4 )
{
right = right +1 ;
check_a4 = FALSE ;
check_b4= FALSE ;
}
//检查数字正确但位置错误的数字个数
if ( check_a1 == TRUE )
{
if ( check_b2 == TRUE && val_a1 == val_b2 )
{
place = place + 1 ;
check_a1 = FALSE ;
check_b2 = FALSE ;
}
if ( check_b3 == TRUE && val_a1 == val_b3 )
{
place = place + 1 ;
check_a1 = FALSE ;
check_b3 = FALSE ;
}
if ( check_b4 == TRUE && val_a1 == val_b4 )
{
place = place + 1 ;
check_a1 = FALSE ;
check_b4 = FALSE ;
}
}
if ( check_a2 == TRUE )
{
if ( check_b1 == TRUE && val_a2 == val_b1 )
{
place = place + 1 ;
check_a2 = FALSE ;
check_b1 = FALSE ;
}
if ( check_b3 == TRUE && val_a2 == val_b3 )
{
place = place + 1 ;
check_a2 = FALSE ;
check_b3 = FALSE ;
}
if ( check_b4 == TRUE && val_a2 == val_b4 )
{
place = place + 1 ;
check_a2 = FALSE ;
check_b4 = FALSE ;
}
}
if ( check_a3 == TRUE )
{
if ( check_b1 == TRUE && val_a3 == val_b1 )
{
place = place + 1 ;
check_a3 = FALSE ;
check_b1 = FALSE ;
}
if ( check_b2 == TRUE && val_a3 == val_b2 )
{
place = place + 1 ;
check_a3 = FALSE ;
check_b2 = FALSE ;
}
if ( check_b4 == TRUE && val_a3 == val_b4 )
{
place = place + 1 ;
check_a3 = FALSE ;
check_b4 = FALSE ;
}
}
if ( check_a4 == TRUE )
{
if ( check_b1 == TRUE && val_a4 == val_b1 )
{
place = place + 1 ;
check_a4 = FALSE ;
check_b1 = FALSE ;
}
if ( check_b2 == TRUE && val_a4 == val_b2 )
{
place = place + 1 ;
check_a4 = FALSE ;
check_b2 = FALSE ;
}
if ( check_b3 == TRUE && val_a4 == val_b3 )
{
place = place + 1 ;
check_a4 = FALSE ;
check_b3 = FALSE ;
}
if ( check_b4 == TRUE && val_a4 == val_b4 )
{
place = place + 1 ;
check_a4 = FALSE ;
check_b4 = FALSE ;
}
}
if ( right == 0 && place == 0 )
return RIGHT_0_PLACE_0;
if ( right == 1 && place == 0 )
return RIGHT_1_PLACE_0;
if ( right == 2 && place == 0 )
return RIGHT_2_PLACE_0;
if ( right == 3 && place == 0 )
return RIGHT_3_PLACE_0;
if ( right == 4 && place == 0 )
return RIGHT_4_PLACE_0;
if ( right == 0 && place == 1 )
return RIGHT_0_PLACE_1;
if ( right == 1 && place == 1 )
return RIGHT_1_PLACE_1;
if ( right == 2 && place == 1 )
return RIGHT_2_PLACE_1;
if ( right == 3 && place == 1 )
return RIGHT_3_PLACE_1;
if ( right == 0 && place == 2 )
return RIGHT_0_PLACE_2;
if ( right == 1 && place == 2 )
return RIGHT_1_PLACE_2;
if ( right == 2 && place == 2 )
return RIGHT_2_PLACE_2;
if ( right == 0 && place == 3 )
return RIGHT_0_PLACE_3;
if ( right == 1 && place == 3 )
return RIGHT_1_PLACE_3;
if ( right == 0 && place == 4 )
return RIGHT_0_PLACE_4;
}
[此贴子已经被作者于2007-9-1 23:53:27编辑过]