Ok I got it able to calculate numiperfectmatches now, but for some reason the do-while look that this function is in in main isn't executing. Is there something that I need to add to the function for this loop to execute?
Loop in main:
Code:
// Loop through each turn, ending at 10 moves or a win.
do {
// Read in the user's guess.
getUserGuess(board);
// Figure out the number of matches.
int num_perfect = numPerfectMatches(board, answer);
int num_imperfect = numWrongPlaceMatches(board, answer);
// Update for this turn.
printf("You have %d perfect matches and %d imperfect matches.\n", num_perfect, num_imperfect);
num_turns++;
} while (num_turns < MAX_TURNS && numPerfectMatches(board, answer) < NUM_PEGS);
Working function:
Code:
// Pre-conditions: board and answer are of length NUM_PEGS and store the player's
// guess and the correct board, respectively.
// Post-conditions: returns the number of pegs that match at INCORRECT spots
// after "erasing" those pegs that match at correct spots.
int numWrongPlaceMatches(int board[], int answer[]) {
// Create temporary copies of both boards, since we'll need to make changes
// to each during this process and we don't want to ruin the original
// state of either board.
int i, temp1[5], temp2[5];
for(i=0; i<NUM_PEGS; i++){
temp1[i] = board[i];
temp2[i] = answer[i];
}
// Mark out the spots on the board and answer that match up perfectly.
for(i=0; i<NUM_PEGS; i++){
if(board[i] == answer [i]){
markOutCorrect(board, answer);
}
}
// Go through the board and try to match each peg on the board to
// a spot on the answer. Hint: A double loop is needed.
// Note: Remember to "mark" an answer when a match is found.
int right, wrong;
for (i=0; i<NUM_PEGS; i++){
for(i=0; i<NUM_PEGS; i++){
if(board[i] == MARKED_BOARD){
right++;
}
board[i]= temp1[i];
answer[i] = temp2[i];
}
}
wrong = NUM_PEGS - right;
return wrong;
}