/* * question9a.c * FINAL * By: John Stile * Purpose: * HEADER FILE for quesiton 9 part c. * Contains 8 or more fucntion definitions */ #include "question9_mastermind_functions.h" // my function header file #include #include // gets srand() & rand() #include // gets isdigit() #include // gets time() #define MAX 5 // possible numbers from 0 to MAX-1 #define NUMBERS 3 // Number of items to guess on #define MAX_GUESSES 10 // number of guesses allowed #define DEBUG 1 ///////////////////////////////////////// /// Functions ///////////////////////////////////////// ///[Function 1] ///////////////////////////////////////// // Introduction to user // void intro_to_user(void ) { int ii; printf("\n"); printf("*\n*\n*\n* Mastermind (but different) \n*\n*\n*\n"); // printf("REF:http://www.gomes-mota.nome.pt/joao/www/jgcores/rules_mm.html\n"); printf("1) I pick %d numbers, between 0 -> %d (no repeats).\n", NUMBERS+1, MAX-1); printf("2) Enter %d numbers with a space between each number.\n", NUMBERS+1); printf(" guessing the right numbers, in the right order.\n"); printf("\n You get %d chances.\n", MAX_GUESSES); printf("\n After each guess, I tell you how you did, with 2 columns:\n"); printf(" CN+CP = coorect number + correct position (the goal\n"); printf(" CN+WP = coorect number + wrong position (frustrating\n\n"); for (ii=0; ii<=NUMBERS; ii++) { printf("%1d ", ii ); } printf("|CN+CP|CN+WP|\n"); // borders for (ii=0; ii<=NUMBERS; ii++) { printf("* "); } printf("| | |\n"); // borders return ; } ///[Function 2] ///////////////////////////////////////// // Gernerate random numbers (from 1 to MAX) // populate array answer // each element must be unique. void get_random_numbers(int * answer_ptr ) { int ii; // declare int counter for for loop int used[MAX]; // declare int array to represent picked numbers int tmp=0; // declare int tmp to hold random number before testing. srand( time(NULL) ); // change the seed each time function is called. for(ii=0; ii<=(MAX-1); ii++) // initialize 'used' array elements to zero { used[ii]=0; } for(ii=0;ii<=NUMBERS ; ii++) // NUMBERS elements must all get a random number // so they are on the outer most loop { do{ // we must do the loop once before // evaluating the exit condiction. tmp = rand()%MAX; // generate a randome number } while( used[tmp] ); // exit loop if number is unique // if element contains 1, it is not unique // do loop again, to get a new random number. // if element contains 0, number is unique // and we can exit loop. *(answer_ptr+ii) = tmp; // assign the unique random number to answer array element used[tmp]=1; // Mark number as used. // by setting used[tmp] element to 1 } // go to next iteration of loop // until all elemnets of answer array contain a number. return ; // return nothing to calling function // because we used pointers. } ///[Function 3] ///////////////////////////////////////// // Input user sequence. // User adds a space between each number // void get_user_guess( int * guess_ptr ) // takes pointer to guess array { // to store user response int ii=0; // counter for loops do { // At end of while, validate input for (ii=0; ii<=NUMBERS; ii++) // populate elements of array with user guesses { if ( ! (scanf("%d", (guess_ptr+ii))) ) // read user input // If return from scanf is bad, // sets the value of this guess elemthat // to an invalid number, //and the validator funciton (good_guess) will kicks back // the array for reinput. { *(guess_ptr+ii)=-1; // set guess element to invalid number. } getchar(); // clear buffer after input } } while ( !( good_guess( guess_ptr ) ) ); // send guess array to validator function // If good_guess() function returns 0, // Input Invalid (for some reason) // Repeat Input of entire array. // If good_guess() function returns 1, // Return to main. return; // return nothing, thanks to pointers } //[Function 4]////////////////////////////////////////// // Test validity of input in get_user_guess // Returns 0 if Input Invalid, calling fucntion asks for new input. // Returns 1 if Input Good. // int good_guess(int * numbers ) // assign guessed number from get_user_guess { int ii; // counter for loops int sane=0; // sane initialized to zero (FALSE) for (ii=0; ii<=NUMBERS; ii++) // Loop through all elements of array. { if( (*(numbers+ii) >= 0) && ( *(numbers+ii) <= MAX-1 ) ) // if input reasonable // greater than 0 // less than equal to max { sane++; // Count number of sane inputs, (perfect=MAX-1) } } if ( sane == (MAX-1) ) // If sane is as high as it can be, all are perfect. { return 1; // all values sane }else{ printf("Invalid number input. Reenter:\n"); printf(" | | |\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");// this draws the grid return 0; // one or more input values invalid } } //[Function 5]////////////////////////////////////////// // Calculate how many are correct void how_many_are_correct( int * answer_ptr, int * guess_ptr, int * correct_ptr ) { // Compare each element of answer to guess // // 1. If number correct & posiition incorrect, set Correct_ptr element to -1. // 2. If number correct & position correct, set Correct_ptr element to 1. // 3. If number number is not in anser list, set Correct_ptr element to 0. // // Once a number has been found to be in the list, further occurences // of that number are not counted as // int ii; // counter for loops int jj; // counter for nested loop for(ii=0; ii<=NUMBERS; ii++) // loop through all guess[] array elements { *(correct_ptr+ii)=0; // Initialize correct[] element to 0. for(jj=0; jj<=NUMBERS; jj++) // loop through all answer[] array elements { if( *(guess_ptr+ii) == *(answer_ptr+jj) ) // If any answer[] element matches this guess[] element { *(correct_ptr+ii)=-1; // CORRECT_NUMBER (at least) // set correct=-1 } } // END maches any answer element if( *(guess_ptr+ii) == *(answer_ptr+ii) ) // perfect match { *(correct_ptr+ii)=1; // CORRECT_POSITION! // set correct=1 } } // End loop through all elements of array return; } //[Function 6]////////////////////////////////////////// // Show the user how they did in with 2 columns: // CN+CP = coorect number + correct position (the goal) // CN+WP = coorect number + wrong position (frustrating) // void calculate_results( int * answer_ptr, int * guess_ptr, int * correct_ptr ) { int ii; int correct_number_correct_position=0; // # with correct position and number int correct_number_wrong_position=0; // # with correct num, wrong position for (ii=0; ii<=NUMBERS; ii++) // Loop throught our correct array { if ( *(correct_ptr+ii) >0 ) correct_number_correct_position++; // Tabulate number of negetives else if ( *(correct_ptr+ii) <0 ) correct_number_wrong_position++; // Tabulate number of positives } printf(" | %d | %d |",correct_number_correct_position, correct_number_wrong_position);// this draws the grid if (DEBUG){ printf("ANSWER:"); for(ii=0; ii<=NUMBERS; ii++) { printf("%1d ", *(answer_ptr+ii) ); } printf("\b, CORRECT:"); for (ii=0; ii<=NUMBERS; ii++) printf("%1d ", *(correct_ptr+ii) ); /* printf("Your Guess: "); for (ii=0; ii<=NUMBERS; ii++) printf("%1d ", *(guess_ptr+ii) ); printf("\n"); printf("Correct Guess: "); for (ii=0; ii<=NUMBERS; ii++) printf("%1d ", *(correct_ptr+ii) ); printf("\n"); */ } printf("\n"); } //[Function 7]////////////////////////////////////////// // Calculate score Based on number of guesses. // Returns 1 if all numbers are perfect. // Returns 0 if any number is not perfect. int are_all_elements_correct(int * correct_ptr ) { int ii; for(ii=0; ii<=NUMBERS; ii++) { if( *(correct_ptr+ii) <= 0) // if any number in the correct[] array // is less than 1, No windner. { return 0; } } return 1; } //[Function 8]////////////////////////////////////////// // winner: Allow the user to store score in file void winner(int score) { char store='n'; printf("You Win! %d tries!\n", score ); printf("Sign scoreboard?(y/n)"); scanf("%c", &store ); if (store=='y') { store_data(score); }else{ printf("OK. Thank you for playing.\n"); } return; } //[Function 9]////////////////////////////////////////// // Winner: Allow the user to store score in file void store_data(int score) { // // Collect winner data // struct Winner{ char first_name[20]; char last_name[20]; int guesses; }; struct Winner this_winner; printf("Enter first name: "); scanf("%s", this_winner.first_name); printf("Enter last name: "); scanf("%s", this_winner.last_name); this_winner.guesses=score; // // Store the score - try to open file pointer in append, fall back to write // char high_scores[]="high_scores.txt"; FILE * fp; // declare pointer of type file if( (fp=fopen( high_scores, "a+" ) )==NULL) { printf("Sorry! Can't open file %s\n", high_scores); exit(1); } printf("Writing your score to file %s\n", high_scores); fprintf(fp, "%d %s %s\n", this_winner.guesses,this_winner.first_name,this_winner.last_name); fclose(fp); // // Read the stored data from file // printf("*******************\n"); printf("*** HIGH SCORES ***\n"); printf("*******************\n"); if( (fp=fopen( high_scores, "r" ) )!=NULL) { char first[20]; char last[20]; int win_score; while(fscanf(fp, "%d %s %s", &win_score,first, last) != EOF) { printf("Score: %d\tName:%s %s\n", win_score, first,last); } } fclose(fp); return; }