mmind.c (3337B)
1 /* simple text mode mastermind game for lccwin-32 2 compiled also without any changes under cc65 6502 cross compiler (www.cc65.org) 3 (c) 2002-2006 Peter Sieg, Rabishauerstr.9, D-37603 Holzminden, peter.sieg1@gmx.de 4 program under GNU GPL 5 greeting to my family: Heike, Robin and Janis 6 7 Source: ftp://www.musoftware.de/pub/uz/cc65/contrib/mmind-1.0.0.zip 8 9 2021: Minor changes by Gerd Beuster (gb) to get it run on Eris 2010 10 */ 11 12 #include <conio.h> 13 #include <string.h> 14 #include <stdarg.h> 15 #include <time.h> 16 #include <stdlib.h> 17 18 #define WORD int 19 20 21 // gb: Changed I/O from stdio to conio. 22 23 /*--------------------------------------------------------------------------- 24 // Main programm 25 //-------------------------------------------------------------------------*/ 26 27 // gb: Not available in conio, therefore added. 28 void gets(char *s) { 29 char c; 30 while ((c = cgetc()) != '\r') { 31 cputc(c); 32 *s = c; 33 s++; 34 } 35 *s = '\0'; 36 cputs("\r\n"); 37 }; 38 39 void main() 40 { 41 unsigned ret; 42 WORD a; 43 WORD statusm,apid; 44 char ad; 45 int bv; 46 char ch[20],my[20]; 47 WORD done; 48 WORD i,j,count,offcnt; 49 WORD z1,z2,z3,z4; /* 4 digits for creation of my 4-digit char array */ 50 WORD echo_xy[4]; 51 unsigned int seed; 52 53 count = 1; offcnt = 0; done = 0; 54 /* create random 4-digit string in char array my */ 55 /* strcpy(my,"1234"); */ 56 57 // gb: Clock not available, therefore we measure the 58 // time until the user presses a key. 59 // srand((unsigned int)time(NULL)); 60 cputs("\r\nPress a key.\r\n"); 61 while(!kbhit()){ 62 ++seed; 63 } 64 cgetc(); 65 srand(seed); 66 67 z1 = (rand() % 9) + 1; /* 1..9 */ 68 do { z2 = (rand() % 9) + 1; } while (z2==z1); 69 do { z3 = (rand() % 9) + 1; } while ((z3==z1) || (z3==z2)); 70 do { z4 = (rand() % 9) + 1; } while ((z4==z1) || (z4==z2) || (z4==z3)); 71 my[0] = z1+48; /* int 1..9 + ascii(48)='0' => '1'..'9' */ 72 my[1] = z2+48; 73 my[2] = z3+48; 74 my[3] = z4+48; 75 my[4] = 0; /* terminate string */ 76 77 cprintf("Guess my 4-digit number...(%s); Use 0 to abort.\r\n","****"); /* insert my to see code */ 78 // cprintf("Guess my 4-digit number...(%s); Use 0 to abort.\r\n",my); /* insert my to see code */ 79 // gb: Additional explanation 80 cputs("Hint:\r\n"); 81 cputs("+ for each correct digit\r\n"); 82 cputs("# for each correct digit in the correct position\r\n"); 83 do { 84 85 cprintf("\r\nGuess %d:",count+offcnt*10); 86 gets(ch); 87 ch[4] = 0; 88 // gb: Cancel condition missing from original version. 89 if ((ch[0] == '0') && (ch[1] == '\0')) { 90 break; 91 } 92 if (strcmp(my,ch)==0) done = 1; /* success; code broken */ 93 /* check how near guess it and print hint */ 94 /* for right digit at right position print # */ 95 /* for right digit but wrong position print + */ 96 if (done==0) { 97 for (i=0;i<4;i++) { 98 if (ch[i]==my[i]) cprintf("# "); 99 else for (j=0;j<4;j++) if (ch[i]==my[j]) cprintf("+ "); 100 } 101 } 102 count++; 103 if (count==11) { 104 count = 1; 105 offcnt++; 106 } 107 } while (done==0); 108 if (done==1) 109 cprintf("\r\n\r\nYou broke the code!"); 110 else 111 cprintf("\r\n\r\nYou gave up.."); 112 cprintf("\r\nEnter any chars and press return to exit.."); 113 gets(ch); 114 } 115