/* Compiled 12/26/89 with Turbo C 2.0 */ #include #include #include #define FALSE 0 #define TRUE !FALSE #define SIZE 4 #define ISDIGIT(c) ((c >= '0')&&(c <= '9')) char *copyright = "Copyright 1989, Michael D. Hofer\n" "Turbo C runtime portions Copyright Borland International\n" "This Copyrighted Product is NOT PUBLIC DOMAIN!\n" "It may be distributed ONLY if:\n" " 1. No fees whatsoever are charged \n" " 2. This notice is included in any distribution or derivative work\n" " 3. The source code is included or made available at no charge to\n" " subsequent users\n" "\n"; void copyright_notice(void){ printf("%s",copyright); } char *instructions = "\n" "Moo is a guessing game, a subset of the popular MasterMind game. The\n" "computer will select a four-unique-digit number and your task is to\n" "guess that number. The computer will respond to your guess by telling\n" "you the number of Bulls and Cows (well, the game IS called moo...) in\n" "your guess, a Bull means that some digit is correct AND in the right\n" "position, a Cow means that some digit is correct but NOT in the right\n" "position. When you guess the number correctly (or give up...) the\n" "computer will tell you the number of guesses you took and immediately\n" "select another number. \n" " \n" "You many enter ? or q to the \"Your Guess: \" prompt, ? says \"I give\n" "up, what is it?\" and q says \"Quit\". Leading Zeroes ARE REQUIRED,\n" "type 0045 not 45 for a guess. That's it, have fun...\n" " \n" "Michael D. Hofer\n" "SysOp, The Ancient Pond BBS 516-935-2027\n" "December, 1989\n\n"; void help(void){ printf("%s",instructions); } int moo_select(void){ /* selects four different digits, and returns them as an integer */ int digit[SIZE]; int i,j,cand,found; int retval; digit[0] = random(10); #ifdef DEBUG printf("Selected digit %d\n",digit[0]); #endif for (i = 1; i < SIZE; i++) { do { found = FALSE; cand = random(10); #ifdef DEBUG printf("Selected digit %d\n",cand); #endif for (j = 0; j < i; j++) if (cand == digit[j]) found = TRUE; } while (found); digit[i] = cand; } retval = 0; for (i = 0; i < SIZE; i++) retval = 10*retval + digit[i]; return (retval); } int moo_guess(int *guess){ /* gets a guess from standard input. If it is a valid guess, it returns TRUE, with the guess number in the variable *guess, if invalid, then returns FALSE and either 'q' or '?' in *guess. */ char in[80]; for (;;){ printf("your guess: "); gets(in); if (in[0] == '?') { guess[0] = '?'; return(0); } else if ((in[0] == 'q') || (in[0] == 'Q')) { guess[0] = 'q'; return(0); } else if (strlen(in) == 4){ if (ISDIGIT(in[0]) && ISDIGIT(in[1]) && ISDIGIT(in[2]) && ISDIGIT(in[3])) { *guess = atoi(in); return(1); } } else printf("Invalid guess.\n"); } } int getyn(char *prompt){ /* prints simple string prompt, and waits for a y or an n, nothing else */ char resp; int oops; do { printf("%s [y/n] ",prompt); resp = tolower(getchar()); if (oops = !((resp == 'y') || (resp == 'n'))) printf("Please type Y or N.\n"); } while (oops); getchar(); return (resp == 'y'); } void main(void){ copyright_notice(); randomize(); if (getyn("Instructions?")) help(); while(run_a_game()); } int run_a_game(){ int number = 0, guess = 0, guesses = 0; printf("New Game.\n"); number = moo_select(); do { if (!moo_guess(&guess)) { printf("You took %d guess",guesses); if (guesses > 1); printf("es"); printf(". The number was %04.4d\n", number); return (guess == '?'); } guesses++; if (bulls(number,guess) == 4) { printf("Correct. %d guesses.\n",guesses); return(TRUE); } printf("Bulls: %d Cows: %d\n",bulls(number,guess), cows(number,guess)); } while (TRUE); } int bulls(int number, int guess){ /* returns the number of EXACTLY correct digits */ char numstr[5], guestr[5]; int i, count; sprintf(numstr,"%04.4d",number); sprintf(guestr,"%04.4d",guess); count = 0; for (i = 0; i < SIZE; i++) count += (numstr[i] == guestr[i]); return(count); } int cows(int number, int guess){ /* returns the number of digits in guess that are present in number LESS the number of Bulls present... */ char numstr[SIZE+1],guestr[SIZE+1]; int i, j, count; sprintf(numstr,"%04.4d",number); sprintf(guestr,"%04.4d",guess); count = 0; for (i = 0; i < SIZE; i++) for (j = 0; j < SIZE; j++) if (guestr[j] == numstr[i]) count ++; return (count - bulls(number,guess)); }