猜数字(C语言源代码)

猜数字(C语言源代码)
猜数字(C语言源代码)

#include

#include

#include

#include

#include

int x=0,n,ts=0;

static sp;

int score(int p)

{ static sw=0;

int scor;

switch(p)

{

case 1: scor=10;break;

case 2: scor=8;break;

case 3: scor=7;break;

case 4: scor=6;break;

case 5: scor=5;break;

case 6: scor=4;break;

case 7: scor=3;break;

case 8: scor=2;break;

case 9: scor=0;break;

default:scor=-1;return 0;

}

if(!scor) x++;

else if(scor==-1) return -1;

else{

printf(" ");

printf("你本局得分:%d \n",scor);

printf(" ");

x++;

printf("***************************\n");

printf(" ");

return scor;}

}

void cmp(char a[] )

{ int m;

char b[5],t[50]="***************************";

int i,j,k=0,r=0,p=0;

if(x==0){

system("cls");

loop: printf(" ");

}

if(x!=0) printf("\n ***************************\n");

if(x==n-1)printf("\n 》最后一局《\n",x++);

else

printf(" 》第%d 局《\n",x+1);

printf(" ");

printf("***************************\n");

printf(" ");

printf("****请输入你的猜测数字:****\n");

printf(" ");

puts(t);////////////////////////////////////////

if(x!=0)

printf(" ");

while(p<=9&&k!=4){

gets(b);

p++;

k=0;r=0;

for(i=0;i<4;i++)

for(j=0;j<4;j++)

if(a[i]==b[j])

if(i==j) k++;

else r++;

if(k!=4&&p!=1)

{

printf(" ");

printf(" %dA %dB 还有%d次机会\n",k,r,10-p);

printf(" ");

printf("***************************\n");}

printf(" ");

}

if(p<=9&&(m=score(p-1)))sp+=m;

/*if(p>9||(m=score(p-1))==0)*/else {printf(" 最后一局得%d 分!\n",sp+=m);x=n;}

//记录每次得分

//printf("You win!!!\n");

//if(p<=9) printf(" 总分:%d\n",ts+);

}

//*********************生成随机数*******************

void programme()

{

int i,j;

int a,b,c,d;

char s[5]={'\0'};

srand((int) time(0));

while(1)

{

j=1022+rand()%(9876-1022+1);

a=j%10;

b=j/10%10;

c=j/100%10;

d=j/1000%10;

if(a==b||b==c||c==d||b==d||a==d||a==c);

else break;

}

s[0]=d+'0';

s[1]=c+'0';

s[2]=b+'0';

s[3]=a+'0';

cmp(s);

}

//************显示规则*************************************

void rule()

{ system("cls");

printf("\n 《猜数字游戏规则》\n\n");

printf(" ");

printf("1. 一方出数字,另一方来猜这个数字\n");

printf(" ");

printf("2. 出数字方要先想好一个没有重复数字的4位数作为目\n");

printf(" ");

printf(" 标数字(不能让猜数字方知道)\n");

printf(" ");

printf("3. 猜数字方提供一个4位猜测数字\n");

printf(" ");

printf("4. 出数字方要根据猜测数字和目标数字计算出几A几B,作为\n");

printf(" ");

printf(" 反馈提供给猜数字方,其中A前面的数字表示位置正确的数的\n");

printf(" ");

printf(" 个数,B前的数字表示数字正确但位置不正确的数的个数.\n");

printf(" ");

printf(" 如目标数字为“5234”,猜测数字为“5346”,则反馈是1A2B:\n");

printf(" ");

printf(" '5'的数字和位置都正确,记为1A;“3”和“4”的数字正确\n");

printf(" 但是位置错误,记为2B.\n");

printf(" ");

printf("5. 如猜测数字和目标数字完全相同(反馈为4A0B),则猜测成功,\n");

printf(" 游戏结束\n 6. 如猜测数字和目标数字不一样(反馈中A的个数小于4),猜数字\n");

printf(" 的一方要根据已有反馈信息,重复3~4步,直至猜出目标数字\n");

printf(" 7. 如猜数字方在限定次数内未猜出目标数字,则猜测失败,游戏结束\n");

printf(" 本游戏中,出数字方由电脑来代替.\n");

// getchar();

}

//********************设置属性*****************************

void color()

{ system("cls");

printf(" ");

printf("请选择设置(按y确定):\n");

printf(" 1--蓝字黄底\n 2--红字蓝底\n 3--红字黄底\n 4--绿字黑底\n");

while(1)

{

switch(getch())

{

case '1':system("color 61");break;

case '2':system("color 14");break;

case '3':system("color e4");break;

case '4':system("color 02");break;

case 'y':

case 'Y':return;

}

}

}

//*********************************************************

void meu()

{

if(x==0){ system("cls");

loop: printf("\n ***欢迎使用***\n");

printf("\n");

printf(" ");

printf("********************\n");

printf(" ");

printf("* 1--开始游戏*\n");

printf(" ");

printf("* 2--查看游戏规则*\n");

printf(" ");

printf("* 3--设置背景*\n");

printf(" ");

printf("* 4--退出游戏*\n");

printf(" ");

printf("********************\n");

printf(" ");

switch(getch())

{

case '1':programme();break;//

case '2':rule();break;//

case '4':exit(1);

case '3':color();break;//

default: system("cls");printf(" ");printf("**输入有误,请重新选择**");goto loop;

}

}

else{

printf(" ");

programme();

}

}

void js()

{ int i,s;

printf("请输入你要玩的局数:");

scanf("%d",&n);

while(x

{ //else printf("第%d 局\n",x+=1);

meu();

}

}

void main()

{

/*char s1[80],s2[80];

int i,j;

printf("请输入用户名:");

scanf("%s",s1);

for(i=0;i

s1[i]=(s1[i]-'\0')%10+'a';

//puts(s1);

printf("请输入密码:\n");

scanf("%s",s2);

if(!strcmp(s1,s2))

{*/

lp: js();

printf(" ");

printf("总分:%d\n",sp);

printf(" ");

printf("继续(y/n)");

switch(getch())

{

case 'Y':

case 'y':goto lp;

case 'N':

case 'n':exit(1);

default:break;

}

// }

// else printf("无权使用,谢谢!!\n");

// getch();

}

相关主题
相关文档
最新文档