/* * question9.c * FINAL * By: John Stile * Purpose: * a) Create a file consisting of at least 8 functions derived from programs * you completed in this couse( function deos math, makes uppercase, returns number * of spaces from a file, or whatever). * * b) then create the header file * * c) do appropriate compilations * * d) and lastly write a short program that includes the header file and links the * functions, employing several of them. * * * e) briefly document the code and steps. *********************************************** * This File is for part d, and e * * Guesing game: * Gernerate 5 random numbers (from 1 to 4) [Function 1] * * * * * * * 1 2 3 4 5 * * Input user sequence. User adds a space between each number [Function 2] * * Test validity of input [Function 3] * * Calculate how many are correct [Function 4] * * Repeat. User is granted chances. * * If ALL correct, * Calculate score Based on number of guesses. [Function 5] * Input users Name (allow 20 chars for first, 20 for last)[Function 6] * Append score to file. [Function 7] * */ #include #include // gets srand() & rand() #include // gets isdigit() #include // gets time() #define MAX 4 // possible numbers are 0 to 3 #define NUMBERS 5 // Number of items to guess on #define MAX_GUESSES 5 // number of guesses allowed #define DEBUG 1 // Prototypes void get_random_numbers( int * ); // takes int pointer, returns nothing void intro_to_user(void); // output directions to user void get_user_guess( int * ); // takes int pointer, returns nothing int good_guess(int); // takes int, returns int. 0=fail, 1=pass void how_many_are_correct( int * , int * , int * ); // takes pointers to guess, answer, and correct arrays. void print_results( int * , int * , int * ); // takes pointers to guess, answer, and correct arrays, returns nothing int are_all_elements_correct( int * ) ;// takes pointers to correct array, returns int if all are correct. void winner(int); void store_data(int); int main(void) { int answer[NUMBERS+1]={}; // declare arrya of NUMBERS elements // contains the right answer int guess[NUMBERS+1]={}; // declare arrya of NUMBERS elements // contains the users input int correct[NUMBERS+1]={}; // declare arrya of NUMBERS elements // contains how many are correct. int ii; // use ii as counter in loops. int all_right=0; // becomes 1 when all are correct get_random_numbers( answer ); // pass address answer array to function intro_to_user(); // Output instrucitons for the user /// BEGIN MAIN LOOP ///////////////////////////////////////////// for( ii=0; !(all_right) && ii<=MAX_GUESSES ; ii++) // Loop asks user for input // until they reach MAX_GUESSES // Or until all_right=1 { printf("------------Round %2d.------------\n", ii); get_user_guess( guess ); // pass address of first element // of answer array to function // to populate array // with users guesses how_many_are_correct( answer, guess, correct ); // pass address of first element // of answer, guess, correct arrays // to function to populate // correct array print_results( answer, guess, correct ); // pass address of first element // of answer, guess, correct arrays // to function to output results all_right=are_all_elements_correct(correct ); // pass address of first element // of correct to function // to check if all are set to 1. // if true, return 1 // and assign to all_right // else, retutn 0 // and assign to all_right }// END MAIN LOOP /////////////////////////////////////////////// // They either won or lost if( all_right ){ winner(ii); }else{ printf("Sorry! You Lost.\nGames of chance do not rrefelct on your intelligence\n"); } return 0; } /// Functions ///[Function ] ///////////////////////////////////////// // Introduction to user // void intro_to_user(void ) { int ii; printf("You are playing a guessing game like Mastermind, but different.\n"); printf("REF:http://www.gomes-mota.nome.pt/joao/www/jgcores/rules_mm.html\n"); printf("I will guess %d numbers from 0 to %d.\n", NUMBERS, MAX-1); printf("You will be given %d chances to get them all right.\n", MAX_GUESSES); printf("Each round I will tell you if you have any correct. \n"); printf("Enter each number seperated by a space.\n"); printf(" "); for (ii=0; ii<=NUMBERS; ii++) { printf("%d ", ii ); } printf("\n"); printf(" "); for (ii=0; ii<=NUMBERS; ii++) { printf("* "); } printf("\n"); return ; } ///[Function 1] ///////////////////////////////////////// // Gernerate random numbers (from 1 to MAX) // void get_random_numbers(int * answer_ptr ) { int ii; // declare int counter for for loop srand( time(NULL) ); // change the seed each time function is called. for(ii=0; ii<=NUMBERS; ii++) // initialize ii to zero // if ii lessthan or equal to NUMBERS, do block // after block increment ii by one { *(answer_ptr+ii) = rand()%MAX; // call srand which returns a psudo random number // divide by our max number and remainder is our number // assign to the address in answer_ptr shifted ii data elements. } return ; // return nothing to calling function } ///[Function 2] ///////////////////////////////////////// // Input user sequence. User adds a space between each number // void get_user_guess( int * guess_ptr ) { int ii=0; // // Not working as I would like. // I would like them to enter all the numbers on one line, separated by spaces. // In the past I have used scanf("%d%d%d", &a, &b, &c) to input 3 numbers. // This only works with a charage return...? // for (ii=0; ii<=NUMBERS; ii++) { do { printf("Enter element %d: ", ii); scanf(" %d", (guess_ptr+ii) ); getchar(); // clear input buffer } while ( !( good_guess( *(guess_ptr+ii) ) ) ); } // Also, the error chekcing is really bad // maybe I nedd to do a // // char tmp; // scanf("%d", &tmp); // isdigit(cc) // return; } //[Function 3]////////////////////////////////////////// // Test validity of input int good_guess(int number ) { if( ( number>=0 ) && ( number <= MAX-1 ) ) { // printf("Sane.\n"); return 1; } else { printf("Rediculous!, Re-"); return 0; } } //[Function 4]////////////////////////////////////////// // Calculate how many are correct void how_many_are_correct( int * answer_ptr, int * guess_ptr, int * correct_ptr ) { // Compare each element of answer_ptr and guess_ptr // If they match, set Correct_ptr element to 1 // If they don't, set Correct_ptr element to 0 int ii; for(ii=0; ii<=NUMBERS; ii++) { if( *(guess_ptr+ii) == *(answer_ptr+ii) ) { *(correct_ptr+ii)=1; } else { *(correct_ptr+ii)=0; } } return; } //[Function 5]////////////////////////////////////////// void print_results( int * answer_ptr, int * guess_ptr, int * correct_ptr ) { int ii; if (DEBUG){ printf("The Answer: "); for(ii=0; ii<=NUMBERS; ii++) { printf("%d ", *(answer_ptr+ii) ); } printf("\n"); } printf("Your Guess: "); for (ii=0; ii<=NUMBERS; ii++) printf("%d ", *(guess_ptr+ii) ); printf("\n"); printf("Correct Guess: "); for (ii=0; ii<=NUMBERS; ii++) printf("%d ", *(correct_ptr+ii) ); printf("\n"); } //[Function 5]////////////////////////////////////////// // Calculate score Based on number of guesses. int are_all_elements_correct(int * correct_ptr ) { int ii; for(ii=0; ii<=NUMBERS; ii++) { if( *(correct_ptr+ii) == 0) { return 0; } } return 1; } //[Function 5]////////////////////////////////////////// // winner: Allow the user to store score in file void winner(int score) { char store='n'; printf("Congradulations!\nYou won in %d tries!\n", score ); printf("Would you like to post your score in the database?(y/n)"); scanf("%c", &store ); if (store=='y') { store_data(score); }else{ printf("OK. Thank you for playing.\n"); } return; } //[Function 5]////////////////////////////////////////// // 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("HIGH SCORES:\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; }