/****************************************************************************** *Steven Heller * *ECE 190 AD1 * *27 March 2007 * ****************************************************************************** *MP 3.2 - Mastermind * *This is a smiple game of Mastermind that uses numbers instead of colors for * *guesses. The numbers range from 1-8 and there are four pegs. The user simply* *tries to guess what the pegs are. * * * *This MP is built upon the core of MP 3.1. Features were added to this * *version. This MP still gets a seed value, generate pegs, get guesses, * *validate inputs, compare guesses, and print results. This verison, however, * *allows the user to guess the pegs for up to 12 tries. It also reports back * *to the player how many of their guesses were misplaced but still "valid". * *The program reamins array based and maintains goode use of functions and * *loops. * * * *I choose to use arrays as it greatly reduces the complexity of conditional * *statements. Instead of having to do a lot of comparing in if statements, I * *can simply use a loop with logically simpiler conditional statements. Arrays* *also allow me to make functions easier as all i have to do is pass the array* *instead of multiple references. * *****************************************************************************/#include <stdio.h>#include <stdlib.h>#define LENGTH 4 /*define the max length of the guesses and pegs*/void generatePegs(int aPegs[]);int checkInput(int aGuesses[]);int compareGuessPeg(int bGuesses[], int bPegs[]);int mismatchGuessPeg(int aGuesses[], int aPegs[]);int playRound(int aPegs[], int tryNum);/*The main function of this program drives the input and responses from/to *the user. First it asks for a seed for the ran. num. gen. Afterwards *it generates pegs, and then prompts the user for their guesses. It then *prints the result of their guesses or an error if they had invalid inputs. *Input: none *Output: 0 on success and -1 for failure*/intmain(){ int amntScanned; int seed; int pegs[LENGTH]; int i; int playRoundResult; printf("Please enter a seed: "); amntScanned = scanf("%d", &seed); if(amntScanned == 0) { printf("Error: invalid input. \n"); return -1; /*return an error*/ } srand(seed); generatePegs(pegs); /*play up to 12 rounds*/ for (i=1;i <= 12;i++) { printf("Guess %d\n", i); playRoundResult = playRound(pegs, i); if (playRoundResult == -1) /*an error occoured?*/ return -1; if (playRoundResult == 1) return 0; /*no need to play longer as they have won*/ } return 0;} /*This function performs all the neseecary operations to play one guess round *with the user. This includes asking for guesses, checking for valid guesses, *and doing various comparisons with the pegs and guesses. * *Input: int aPegs[] - the array containing the pegs * int tryNum - what guess number the program is on *Output: int data type * -1 - if an error occoured somehow * 0 - if the guess was invalid * 1 - if the guess was valid */intplayRound(int aPegs[], int tryNum){ int guesses[LENGTH]; int inputCheckResult; int compareGuessPegResult; int mismatchGuessPegResult; int amntScanned; printf("Enter your guess (#1 #2 #3 #4): "); amntScanned = scanf("%d %d %d %d", &guesses[0], &guesses[1], &guesses[2], &guesses[3]); /*This block of code makes sure that there were at least 4 valid *integer inputs. If so it then checks to see of thoose integers *are valid guessses. If the guesses were valid, it then compares *the guesses with the values stored in the pegs. After that, it *prints the results.*/ if (amntScanned == 4) { inputCheckResult = checkInput(guesses); if(inputCheckResult == 1) /*we have valid guesses*/ { compareGuessPegResult = compareGuessPeg(guesses, aPegs); if (compareGuessPegResult == 4) { printf("You guessed correctly in %d guesses. ", tryNum); printf("The solution was %d %d %d %d. \n", aPegs[0], aPegs[1], aPegs[2], aPegs[3]); return 1; } else { mismatchGuessPegResult = mismatchGuessPeg(guesses, aPegs); printf("Incorrect, you had %d perfect matches and %d misplaced matches. \n", compareGuessPegResult, mismatchGuessPegResult); if (tryNum == 12) /*since 12 has a special output if wrong*/ { printf("You failed to guess correctly in 12 guesses. "); printf("The solution was %d %d %d %d. \n", aPegs[0], aPegs[1], aPegs[2], aPegs[3]); } return 0; } } else { printf("Error: invalid input. \n"); return -1; } } else { printf("Error: invalid input. \n"); return -1; }}/*This function generates the random pegs with the values 1-8 andstores in them in an array. It generates a random number from 1-8and then after making sure no other peg has the same value, it will storethe value in the array. It repeates the same process for each of the fourpegs.Input: an array that will be used to store the pegs.Output: none, but the array passed in will have its values changed to represent the generated pegs.*/voidgeneratePegs (int aPegs[]){ int randomNum; int i = 0; int j; int found; /*use a do loop since i may not always be incremented*/ do { /*generate a random number, mod it by 8 and then add one*/ randomNum = rand(); randomNum = randomNum % 8; randomNum++; if (i == 0) /*at first position, no other pegs will match yet*/ { aPegs[i] = randomNum; i++; continue; } else { /*check to see if a number has already been assigned*/ found = 0; /*since we load left to right, start at the left for comparison and stop at the position one before i*/ for (j=0;j < (i);j++) { if (aPegs[j] == randomNum) found = 1; } if(found == 1) /*match found, must make a new number*/ continue; else { aPegs[i] = randomNum; i++; } } }while(i < LENGTH);}/*This function checks if the inputed integer guessesare actually valid. Valid input is when all the guesses arebetween 1 - 8 inclusive and that no two guesses match each other.This returns 1 if the input is validand 0 if it is invalid.*/intcheckInput(int aGuesses[]){ int i; int j; int k; /*check to see if one of the guesses is out of range (under one over 8)*/ for (i=0;i < LENGTH; i++) { if (aGuesses[i] < 1 || aGuesses[i] > 8) return 0; } /*check to see if there are two of the same guesses*/ for (i=0; i < LENGTH; i++) { k = aGuesses[i]; /*holds the current value for comparison*/ /*check all places greater than i for a potential match w/ k*/ for (j = i+1;j < LENGTH; j++) { if (aGuesses[j] == k) /*found a match*/ return 0; } } /*the input was valid if it gets here*/ return 1;}/*This function compares the value of the guesseswith the value of the pegs. In order to be a match,the value at each peg must match the guess. It doesnot account for offsetted guesses.Input: the guesses and pegs arrayOutput: the number of pegs that matched the guesses*/intcompareGuessPeg(int bGuesses[], int bPegs[]){ int matches = 0; int i; for (i=0;i < LENGTH; i++) { if (bGuesses[i] == bPegs[i]) matches++; } return matches;}/*This function finds out if a guess is correct put misplaced in order. *It loops through the pegs and guesses arrays at certain points in order *to see if a peg matches a guess somewhere else. * *Input: int aGuesses[] - the guesses array * int aPegs[] - the pegs array *Ouput: the amount of misplaced guesses */intmismatchGuessPeg(int aGuesses[], int aPegs[]){ /*check to see if curr val is equal if so, don't check for mismatches use two loops, one before current place and one after current place and check to see if anything equals.*/ int numMismatches = 0; int i; int j; for (i = 0;i < LENGTH; i++) { if (!(aGuesses[i] == aPegs[i])) { /*check for mismatches below the current peg. if at 0 the code will still properly work.*/ for (j = 0; j < i; j++) { if (aGuesses[i] == aPegs[j]) numMismatches++; } /*check for mismatches at places above i*/ for (j = (i+1); j < LENGTH; j++) { if (aGuesses[i] == aPegs[j]) numMismatches++; } } } return numMismatches;}