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: 126


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 mastmind.c 4 5 (P) 1993 Laurentiu Cristofor (laur72_98@yahoo.com) 6 7 plain implementation of Mastermind game; 8 9 one of my first C programs 10 (has goto's so it must be the first!), 11 it's been originally built for MS-DOS 12 using a Borland C compiler and 13 non-ANSI I/O functions in 1993, 14 and transfered to Unix in 1997 15 16 ------------------------------------------------*/ 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <time.h> 21 22 /* high level clear screen procedure :-) */ 23 void clrscr(void) 24 { 25 int i; 26 27 for(i=0;i<=80;i++) puts(""); 28 } 29 30 void randomize(void) 31 { 32 time_t tt; 33 char buf[64], hundreds[3]; 34 35 /* Get current hundredth of second in variable hundreds */ 36 tt = time(NULL); 37 strcpy(buf, ctime(&tt)); 38 hundreds[0]=tolower(buf[17]); 39 hundreds[1]=tolower(buf[18]); 40 hundreds[2]='\0'; 41 srandom(atoi(hundreds)); 42 } 43 44 int main(int argc,char **argv) 45 { 46 char c=0, // c keeps track of which key was last pressed 47 m=0, // m keeps track of each of the four numbers in each guess 48 n=21, // n keeps track of which guess number this is 49 isPlay; // keeps track of whether or not we are in the middle of a game 50 int i=0,j,p,t,hidden[4],guess[4]; 51 52 randomize(); 53 /* 54 for(i=0;i<4;i++) 55 { 56 hidden[i] = random()%6+1; 57 }*/ 58 printf("\n"); 59 clrscr(); 60 printf( "***********************\n" 61 "* M A S T E R M I N D *\n" 62 "***********************\n" 63 "\n" 64 "\n" 65 "You must guess 4 numbers out of 1 , 2 , 3 , 4 , 5 and 6 with possible repetitions.\n" 66 "\n" 67 "The computer evaluates each of your guesses with:\n" 68 "\t* = number guessed in right position\n" 69 "\t+ = guessed in wrong position\n" 70 "\tx = all guesses are wrong\n" 71 "\n" 72 "To quit press q.\n" 73 "To start a new game press n.\n\n"); 74 75 while (c!='q') 76 { 77 c=getchar(); 78 79 switch(c) 80 { 81 case 'n': 82 // reset variables for games 83 m=0, 84 n=1; 85 86 for(i=0;i<4;i++) 87 { 88 hidden[i] = random()%6+1; 89 } 90 printf( "\tGuess no. %d. Enter 4 numbers " 91 "then press <Enter>: ",n); 92 break; // end of case and 'n' 93 case '\n': 94 case '\r': 95 case 'q': 96 break; 97 case '1': 98 case '2': 99 case '3': 100 case '4': 101 case '5': 102 case '6': 103 if(21>n) 104 { 105 guess[m++] = c-48; 106 break; 107 } 108 c='z'; 109 default: 110 printf( "\nWha'chu ta'kin' about Willis? \n" 111 "You have to type \"1\", \"2\", \"3\", " 112 "\"4\", \"5\", or \"6\" to guess, \n" 113 "\"N\" for new game or \"Q\" for quit\n"); 114 break; 115 } // end of switch(c) 116 if(4==m) 117 { 118 t=p=c=m=0; 119 120 for(i=0;i<4;i++) 121 // count up how many were correct and in the right place 122 { 123 if (guess[i] == hidden[i]) 124 { 125 t++; 126 hidden[i] = hidden[i] - 8; 127 guess[i] = guess[i] - 16; 128 } 129 } 130 if(4==t) 131 // if the player got 4 right, then pick a compliment or insult 132 { 133 if(n <= 4) 134 printf( "EXCELLENT!!! " 135 "You needed only %d guesses",n); 136 if(n > 4 && n <= 7) 137 printf( "Congratulations! " 138 "You needed %d guesses",n); 139 if(n > 7 && n <= 10) 140 printf( "Good! You needed %d guesses",n); 141 if(n > 10 && n <= 15) 142 printf( "Poor! You needed %d guesses; " 143 "you need to practice more!",n); 144 if(n > 15) 145 printf( "CATATROPHIC!!! " 146 "You needed %d guesses...\n",n); 147 printf( "\a\n\n" 148 "You can always try another time: [New game/Quit]"); 149 n+=21; // make n equal to or greater than 21 150 // so that the computer will think the game is over-- 151 // well...it is over, isn't it? 152 } 153 else // not all numbers are correct 154 { 155 for(i=0;i<4;i++) 156 // count up how many were right but in the wrong place 157 { 158 for(j=0;j<4;j++) 159 { 160 if (guess[i] == hidden[j]) 161 { 162 p++; 163 hidden[j] = hidden[j] - 8; 164 guess[i] = guess[i] - 16; 165 } 166 } 167 } 168 169 if(t != 0) 170 { 171 for(i=0;i<t;i++) 172 { 173 printf("* "); // we put a space after each star 174 // to make it display nicer 175 } 176 } 177 if(p != 0) 178 { 179 for(i=0;i<p;i++) 180 { 181 if(0!=i) 182 // we have to put the space in ***FRONT*** of the 183 // dash, so that it won't push the following text to 184 // the next tab stop. to accomplish this, we still have 185 // to keep it evenly spaced. to keep it evenly spaced, 186 // we avoid putting a space in front of the first dash. 187 // once we figure out how to put the stars and dashes 188 // immediately after the guess without line wrapping, 189 // we will get rid of this if statement, and place the 190 // space right after the dash, just as it is with the 191 // star 192 { 193 printf(" "); 194 } 195 printf("+"); 196 } 197 } 198 if(t+p == 0) 199 printf("x"); 200 201 for(i=0;i<4;i++) 202 if (hidden[i] < 0) 203 hidden[i] = hidden[i] + 8; 204 n++; 205 if(21>n) 206 // the game is not over 207 { 208 printf( "\tGuess no. %d. Enter 4 numbers " 209 "then press <Enter>: ",n); 210 } 211 else 212 // the game is over but the player lost 213 { 214 printf( "\nA-a-ah zut! You failed. :^(\n " 215 "You can always try another time: [New game/Quit]"); 216 } 217 } 218 } // end of if(4==m) or "end of marking guesses" 219 } // end of while(c!='q') 220 221 printf("\nMastermind (P) 1993 Cristofor Laurentiu Bogdan\n"); 222 return 0; 223 } 224

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS