|
|
1.1 root 1: /*
2: * moo -- play the game of moo,
3: * also known as mastermind.
4: */
5:
6: #include <stdio.h>
7: #include <sys/types.h>
8:
9: char entdiff[] = "Please enter a string of %d digits, all different\n";
10:
11: main(argc, argv)
12: char *argv[];
13: {
14: char moo[10];
15: int nmoo;
16: char line[256];
17: int i, j;
18: int nbull, ncow;
19: union {
20: time_t utime;
21: int ubuf[2];
22: } un;
23:
24: time(&un.utime);
25: srand(un.ubuf[0] + un.ubuf[1]);
26: if(argc<2)
27: nmoo=4;
28: else
29: nmoo=atoi(argv[1]);
30: if(nmoo<1 || 10<nmoo){
31: printf("Usage: moo [n]\n\twhere 1<=n<=10, n defaults to 4\n");
32: exit(1);
33: }
34: for(;;){
35: printf("New game.\n");
36: for(i=0;i!=nmoo;i++){
37: Again:
38: moo[i]=rand()/100%10+'0';
39: for(j=0;j!=i;j++)
40: if(moo[i]==moo[j])
41: goto Again;
42: }
43: for(;;){
44: if(gets(line)==NULL)
45: exit(0);
46: if(badline(line, nmoo))
47: printf(entdiff, nmoo);
48: else{
49: nbull=0;
50: ncow=0;
51: for(i=0;i!=nmoo;i++)
52: for(j=0;j!=nmoo;j++)
53: if(line[i]==moo[j])
54: if(i==j)
55: nbull++;
56: else
57: ncow++;
58: if(nbull==nmoo){
59: printf("Right!\n");
60: break;
61: }
62: printf("%d bull%c, %d cow%c\n",
63: nbull, nbull!=1?'s':'\0',
64: ncow, ncow!=1?'s':'\0');
65: }
66: }
67: }
68: }
69:
70: badline(s, l)
71: register char *s;
72: {
73: register i, j;
74: if(strlen(s)!=l)
75: return(1);
76: for(i=0;i!=l;i++){
77: if(s[i]<'0' || '9'<s[i])
78: return(1);
79: for(j=0;j!=i;j++)
80: if(s[i]==s[j])
81: return(1);
82: }
83: return(0);
84: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.