/* * 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: * Print splash screen [Function 1] * Gernerate MAX-1 random numbers (from 1 to 4) [Function 2] * * * * * * * 1 2 3 4 5 * Input user sequence. User adds a space between each number [Function 3] * Test validity of input, Ask for re-input if invalid. [Function 4] * Calculate how many are correct [Function 5] * 1 = correct number and postion * -1 = corerect number wrong position * 0 = number not in answer list. * Display Results to the user [Function 6] * Calculate if we have a winner (all match ) [Function 7] * Congradulate user, and offer a scoreboard entery [Function 8] * Open scoreboard and enter user info [Function 9] * * ====To Build==== * gcc -c question9_mastermind_functions.c -o question9_mastermind_functions.o * gcc -c question9_mastermind_main.c -o question9_mastermind_main.o * gcc question9_mastermind_main.o question9_mastermind_functions.o -lm -o question9_mastermind * */ #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 ///////////////////////////////////////// /// Prototypes ///////////////////////////////////////// void get_random_numbers( int * ); // Generate random numbers: takes int pointer, returns nothing void intro_to_user(void); // Splash Screen: takes nothing, returns nothing. void get_user_guess( int * ); // takes int pointer, returns nothing int good_guess(int * ); // takes int pointer, returns int. 0=fail, 1=pass void how_many_are_correct( int * , int * , int * ); // takes pointers to guess, answer, and correct arrays. void calculate_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); // takes int void store_data(int); int main(void) { int answer[NUMBERS+1]={}; // declare arrya of NUMBERS elements (add one for '\0') // Computer's answer int guess[NUMBERS+1]={}; // declare arrya of NUMBERS elements(add one for '\0') // Users guesses int correct[NUMBERS+1]={}; // declare arrya of NUMBERS elements(add one for '\0') // 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 populate 'answer' array intro_to_user(); // Output instrucitons for the user for( ii=0; !(all_right) && ii<=MAX_GUESSES ; ii++) // Ask user for 5 rounds of input. // OR all_right=1 // This means all numbers are in correct position! { printf("------Round %2d.------\n", ii); 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 // and backup curser get_user_guess( guess ); // pass address of guess array // to populate 'guess' array how_many_are_correct( answer, guess, correct ); // pass address of first answer, guess, correct arrays // to populate 'correct' array calculate_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 } // They either won or lost if( all_right ){ // if allright is 1, all correct number, correct position winner(ii); }else{ printf("Sorry! You Lost.\nGames of chance do not rrefelct on your intelligence\n"); } return 0; } /* *############################################################## * Sample output With DEBUG=1 *############################################################## * * * * Mastermind (but different) * * * 1) I pick 4 numbers, from 0 -> 4 (no repeats). 2) Enter 4 numbers with a space between each number. guessing the right numbers, in the right order. You get 10 chances. After each guess, I tell you how you did, with 2 columns: CN+CP = coorect number + correct position (the goal CN+WP = coorect number + wrong position (frustrating 0 1 2 3 |CN+CP|CN+WP| * * * * | | | ------Round 0.------ 2 3 4 5 | | | Invalid number input. Reenter: 1 2 3 ` | | | Invalid number input. Reenter: 1 2 3 4 | | | | 1 | 2 |ANSWER:2 1 3 0, CORRECT:-1 -1 1 0 ------Round 1.------ 2 1 3 0 | | | | 4 | 0 |ANSWER:2 1 3 0, CORRECT:1 1 1 1 You Win! 2 tries! Sign scoreboard?(y/n)y Enter first name: John Enter last name: Stile Writing your score to file high_scores.txt ******************* *** HIGH SCORES *** ******************* Score: 2 Name:Homer Simpson Score: 2 Name:John Stile Score: 3 Name:high_scores.txt John Score: 3 Name:John Stile Score: 5 Name:Joe Shmow Score: 1 Name:fomaz mogan Score: 4 Name:John Stile Score: 2 Name:John Stile *############################################################## * Sample output With DEBUG=1 *############################################################## * * * * Mastermind (but different) * * * 1) I pick 4 numbers, from 0 -> 4 (no repeats). 2) Enter 4 numbers with a space between each number. guessing the right numbers, in the right order. You get 10 chances. After each guess, I tell you how you did, with 2 columns: CN+CP = coorect number + correct position (the goal CN+WP = coorect number + wrong position (frustrating 0 1 2 3 |CN+CP|CN+WP| * * * * | | | ------Round 0.------ 0 2 1 3 | | | | 1 | 2 | ------Round 1.------ 0 1 3 2 | | | | 2 | 1 | ------Round 2.------ 0 1 4 3 | | | | 2 | 1 | ------Round 3.------ 0 1 3 2 | | | | 2 | 1 | ------Round 4.------ 0 1 2 4 | | | | 4 | 0 | You Win! 5 tries! Sign scoreboard?(y/n)y Enter first name: Homer Enter last name: Simpson Writing your score to file high_scores.txt ******************* *** HIGH SCORES *** ******************* Score: 2 Name:Homer Simpson Score: 2 Name:John Stile Score: 3 Name:high_scores.txt John Score: 3 Name:John Stile Score: 5 Name:Joe Shmow Score: 1 Name:fomaz mogan Score: 4 Name:John Stile Score: 2 Name:John Stile Score: 5 Name:Homer Simpson ############################################################## */