You're here: Snippet Directory » C/C++ (495)
Language:

Mastermind

Language: English
Programming Language: C
Published by: laur72_98 [not registered]
Last Update: 5/15/2006
Views: 132


Description

A highly portable version of Mastermind. Don't laugh at this code, it is among my first attempts at writing C code.

Code

1 /* 2 3 This is the original code that I posted a while back. You have to 4 understand that I am aware of the quality of this code (or lack of it 5 to be more precise) but I've just posted it for fun. It's been written 6 around '93 and I keep it around to remind me how I was writing code a 7 while ago. Also note that other people have taken interest into this 8 piece of code, notably Mr. Eugene T.S. Wong who rewrote this program 9 using his own style. If you have questions about his code then please 10 direct them to him and not to me. This code was never intended as an 11 example of properly written C code, I use goto's, variables have 12 cryptic names, and other abominations that denote this was a 13 beginner's code. I know that but personally I don't intend to change 14 it for the sentimental reasons I mentioned before. You're welcome to 15 rewrite it as you wish but if you do that then please respect the 16 following request. 17 18 If you produce a new version of this code please delete my email 19 address from it and make it clear that people should send comments to 20 you and not to me. You can give me credit only if you really found 21 this code useful but make sure you take responsibility for your 22 version and that it is clear you're the author of your version. 23 24 */ 25 26 /*---------------------------------------------- 27 28 mastmind.c 29 30 (P) 1993 Laurentiu Cristofor (laur72_98@yahoo.com) 31 32 plain implementation of Mastermind game; 33 34 one of my first C programs 35 (has goto's so it must be the first!), 36 it's been originally built for MS-DOS 37 using a Borland C compiler and 38 non-ANSI I/O functions in 1993, 39 and transfered to Unix in 1997 40 41 ------------------------------------------------*/ 42 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <time.h> 46 47 /* high level clear screen procedure :-) */ 48 void clrscr(void) 49 { 50 int i; 51 52 for(i=0;i<=80;i++) puts(""); 53 } 54 55 void randomize(void) 56 { 57 time_t tt; 58 char buf[64], hundreds[3]; 59 60 /* Get current hundredth of second in variable hundreds */ 61 tt = time(NULL); 62 strcpy(buf, ctime(&tt)); 63 hundreds[0]=tolower(buf[17]); 64 hundreds[1]=tolower(buf[18]); 65 hundreds[2]='\0'; 66 srandom(atoi(hundreds)); 67 } 68 69 int main(void) 70 { 71 char c; 72 int m,n,i,j,p,t,hidden[4],guess[4]; 73 74 while (1) 75 { 76 clrscr(); 77 randomize(); 78 puts("***********************"); 79 puts("* M A S T E R M I N D *"); 80 puts("***********************"); 81 putchar('\n'); 82 putchar('\n'); 83 puts("You must guess 4 numbers out of 1 , 2 , 3 , 4 , 5 and 6 with possible repetitions."); 84 putchar('\n'); 85 puts("The computer evaluates you try with:"); 86 puts(" * = number guessed in right position"); 87 puts(" + = number guessed in wrong position"); 88 puts(" - = no number guessed"); 89 putchar('\n'); 90 puts("Wanna play (y/n) ?"); 91 92 c=getchar(); 93 94 if(c=='y') 95 { 96 again: clrscr(); 97 printf("\n \t \t \t M A S T E R M I N D \n \n"); 98 puts("To quit game press q."); 99 puts("To start a new game press n."); 100 putchar('\n'); 101 102 for(i=0;i<4;i++) 103 hidden[i] = random()%6+1; 104 105 n=0; 106 while(1) 107 { 108 n++; 109 110 if(n < 20) 111 printf("Guess no. %d : ",n); 112 else 113 printf("You can always try another time:"); 114 115 for(i=0;i<4;i++) 116 { 117 c=getchar(); 118 119 if(c=='q') 120 goto end; 121 if(c=='n') 122 goto again; 123 if(c=='1') 124 m=1; 125 if(c=='2') 126 m=2; 127 if(c=='3') 128 m=3; 129 if(c=='4') 130 m=4; 131 if(c=='5') 132 m=5; 133 if(c=='6') 134 m=6; 135 if(c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6') 136 if(c != 'q' && c != 'n') 137 { 138 i = i-1; 139 continue; 140 } 141 guess[i] = m; 142 } 143 144 t=0; 145 p=0; 146 for(i=0;i<4;i++) 147 if (guess[i] == hidden[i]) 148 { 149 t++; 150 hidden[i] = hidden[i] - 8; 151 guess[i] = guess[i] - 16; 152 } 153 for(i=0;i<4;i++) 154 for(j=0;j<4;j++) 155 if (guess[i] == hidden[j]) 156 { 157 p++; 158 hidden[j] = hidden[j] - 8; 159 guess[i] = guess[i] - 16; 160 } 161 162 putchar(' '); 163 164 fflush(stdin); 165 166 if(t != 0) 167 for(i=0;i<t;i++) 168 { 169 putchar('*'); 170 putchar(' '); 171 } 172 if(p != 0) 173 for(i=0;i<p;i++) 174 { 175 putchar('+'); 176 putchar(' '); 177 } 178 if(t+p == 0) 179 putchar('-'); 180 181 putchar('\n'); 182 183 for(i=0;i<4;i++) 184 if (hidden[i] < 0) 185 hidden[i] = hidden[i] + 8; 186 187 if(t == 4) 188 { 189 if(n <= 4) 190 printf("EXCELLENT!!! You needed only %d guesses\n",n); 191 if(n > 4 && n <= 7) 192 printf("Congratulations! You needed %d guesses\n",n); 193 if(n > 7 && n <= 10) 194 printf("Good! You needed %d guesses\n",n); 195 if(n > 10 && n <= 15) 196 printf("Poor! You needed %d guesses; you need to practice more!\n",n); 197 if(n > 15) 198 printf("CATASTROPHIC!!! You needed %d guesses...\n",n); 199 200 putchar('\n'); 201 puts("Another game (y/n) ?"); 202 c=getchar(); 203 if(c=='y') 204 goto again; 205 else 206 goto end; 207 } 208 } 209 } 210 if(c=='n') 211 goto end; 212 if(c!='y' && c!='n') 213 { 214 continue; 215 } 216 } 217 218 end: clrscr(); 219 puts("Mastermind (P) 1993 Cristofor Laurentiu Bogdan"); 220 } 221 222

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS