编程高手之路—代码集萃—猜数字游戏——TC新手的一个程序 本个学TC不久,前几天玩文曲星上的猜数字,就想到用TC2.0来实现这个游戏。代码如下: #include #include #include #define MAXTIMES 8 #define winer 1 #define loser 0 int main() { int aid[4],num[4],i,j,times,key,key2,A,B; char ch; time_t now; Initialize:now=time(0); srand(now); aid[0]=(rand()+90)%10;/*生成目标数组*/ delay(20); aid[1]=(rand()+90)%10; delay(20); aid[2]=(rand()+90)%10; delay(20); aid[3]=(rand()+90)%10; for(i=0;i<4;i++) /*判断是否产生相同的数字*/ {for(j=0;j<4;j++) {if((i!=j)&&(aid[i]==aid[j])) goto Initialize; } } printf(nnn); times=1; while(times<=MAXTIMES) /*程序主体部分*/ { key=0; key2=0; printf(NO.%d:ntPlease input 4 different int numbers to play!nt,times); scanf(%d,%d,%d,%d,&num[0],&num[1],&num[2],&num[3]); for(i=0;i<4;i++) /*判断所输数字是否合法则*/ { if(!((num[i]<=9)&&(num[i]>=0))) { printf(tError!The numbers you input must be int numbers which from 0 to 9n); key=1; break; } } if(key==1) continue; for(i=0;i<4;i++) /*判断是否输入了相同的数字*/ { for(j=i+1;j<4;j++) { if(num[i]==num[j]) {printf(tError!The numbers you input must be different!n); key2=1; } break; } } if(key2==1) continue; A=0; B=0; for(i=0;i<4;i++) /*比较两组数字*/ { if(num[i]==aid[i]) A++; } for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(num[i]==aid[j]) B++; } } B=B-A; printf(tA%dB%dn,A,B); if(A==4) /*游戏成功,提示并返回*/ { printf(Congratulations!n); printf(Press any key to exit.!na); getch(); return(winer); } else /*这次不完全对,允许次数内重新猜*/ { printf(ttPress ENTER to continue,or press A to see about answer and exit.n); /*选择是否放弃,看答案*/ ch=getch(); if(ch=='n') { continue; } if((ch=='a')||(ch=='A')) { printf(The four numbers:%d,%d,%d,%dn,aid[0],aid[1],aid[2],aid[3]); getch(); return(loser); } } times++; } printf(Sorry!You have lost all the chances!nPress any key to exit!n); getch(); } 本文来源:https://www.wddqw.com/doc/c12351bec77da26925c5b0e0.html