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


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 created by Laurentiu Cristofor (laur72_98@yahoo.com) 6 modified by Eugene T.S. Wong (eugenetswong at yahoo dot com) 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 #define noOfDigits 4 // number of digits that the player must guess 23 #define noOfTurns 20 // number of turns that the player gets 24 25 /* high level clear screen procedure :-) */ 26 void clrscr(void) 27 { 28 // printing \n 50 times is probably more efficient and simpler than 29 // creating a variable and using a loop. 50 times should be enough 30 // to clear any screen. 31 printf( "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" 32 "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); 33 } 34 35 void randomize(void) 36 { 37 time_t tt; 38 char buf[64], hundreds[3]; 39 40 /* Get current hundredth of second in variable hundreds */ 41 tt = time(NULL); 42 strcpy(buf, ctime(&tt)); 43 hundreds[0]=tolower(buf[17]); 44 hundreds[1]=tolower(buf[18]); 45 hundreds[2]='\0'; 46 srandom(atoi(hundreds)); 47 } 48 49 int main(int argc,char **argv) 50 { 51 char c=0, // c keeps track of which key was last pressed 52 m=0, // m keeps track of each of the four numbers in each guess 53 n=1, // n keeps track of which guess number this is 54 isPlay, // keeps track of whether or not we are in the middle of a game 55 hidden[noOfDigits], 56 guess[noOfDigits+1]; 57 int i=0,j,p,turn; 58 59 randomize(); 60 printf("\n"); 61 for(i=0;i<noOfDigits;i++) 62 { 63 // printf("- %c -", hidden[i] = random()%6+1+48); 64 // the above line is similar to the one below, but is used for 65 // debugging in that it displays the numbers so that you can see where 66 // the problems might be 67 // 68 hidden[i] = random()%6+1+48; 69 // 70 // I added +1 and +48 seperately because it helps to clarify that 71 // the random digits must be raised to character versions. At least 72 // in my opinion it clarifys things. For a simple game like this, 73 // people aren't going to notice the split second of inefficiency. 74 } 75 printf("\n"); 76 // clrscr(); 77 printf( "***********************\n" 78 "* M A S T E R M I N D *\n" 79 "***********************\n" 80 "\n" 81 "\n" 82 "You must guess %d numbers out of 1 , 2 , 3 , 4 , 5 and 6. It is\n" 83 "possible for the numbers to repeat. After each guess, press\n" 84 "<Ctrl><D> twice. To quit, type <Ctrl><C>.\n" 85 "\n" 86 "The computer evaluates each of your guesses with:\n" 87 "\t* = number guessed in right position\n" 88 "\t+ = guessed in wrong position\n" 89 "\tx = all guesses are wrong\n" 90 "\n", noOfDigits); 91 92 while(1) 93 { 94 if(21>n) 95 // the game is not over 96 { 97 printf( "Guess no. %d. Enter %d numbers " 98 "then press <Enter>: ",n,noOfDigits); 99 } 100 else 101 // the game is over but the player lost 102 { 103 printf( "\nA-a-ah zut! You failed. :^( " 104 "You can always try another time.\n"); 105 return 0; 106 } 107 fgets(guess, 100, stdin); 108 109 turn=p=0; 110 for(i=0;i<noOfDigits;i++) 111 // count up how many were correct and in the right place 112 { 113 if (guess[i] == hidden[i]) 114 { 115 turn++; 116 hidden[i] = hidden[i] - 48; 117 guess[i] = guess[i] + 10; 118 printf(" *"); // we put a space before each star 119 // to make it display nicer 120 } 121 } 122 if(noOfDigits==turn) 123 // if the player got noOfDigits right, then pick a compliment or insult 124 { 125 printf("\n"); 126 if(n <= noOfDigits) 127 printf( "EXCELLENT!!! " 128 "You needed only %d guesses",n); 129 if(n > noOfDigits && n <= 7) 130 printf( "Congratulations! " 131 "You needed %d guesses",n); 132 if(n > 7 && n <= 10) 133 printf( "Good! You needed %d guesses",n); 134 if(n > 10 && n <= 15) 135 printf( "Poor! You needed %d guesses; " 136 "you need to practice more!",n); 137 if(n > 15) 138 printf( "CATATROPHIC!!! " 139 "You needed %d guesses...\n",n); 140 printf("\a\n\n"); 141 return 0; 142 } 143 else 144 // not all numbers are correct 145 { 146 for(i=0;i<noOfDigits;i++) 147 // count up how many were right but in the wrong place 148 { 149 for(j=0;j<noOfDigits;j++) 150 { 151 if (guess[i] == hidden[j]) 152 { 153 p++; 154 hidden[j] = hidden[j] - 48; 155 guess[i] = guess[i] + 16; 156 printf(" +"); // we put a space before each plus sign 157 // to make it display nicer 158 } 159 } 160 } 161 162 if(turn+p == 0) 163 printf(" x"); 164 165 printf("\n"); 166 for(i=0;i<noOfDigits;i++) 167 if(hidden[i] < 48) 168 hidden[i] = hidden[i] + 48; 169 n++; 170 } 171 } // end of while(1) 172 173 return 0; 174 }

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS