lundi 28 novembre 2016

Mastermind game: program didn't calculate right perfect and imperfect match

I'm coding the Mastermind game. And the rules doesn't like any of these topics:

Simple guessing game in c

C number guessing game with isdigit() verification

MasterMind game in C

Mastermind Game Algorithm

Here are rules:

  1. You are given a random of 4 numbers, (all numbers in range between 0 and 5);

  2. You guess those numbers by inputting 4 numbers, separated by a space, (condition are the same with the random);

  3. After input, you will be told how many perfect and imperfect match you've got (perfect match means player guess any of 4 which correct both in value and position, imperfect match means correct in value but not correct in position)

  4. You have 10 chances, if you fail after 10 guess attemps, the program stop and print out the random board. Otherwise, if you guess 4 numbers correctly, the program stop and print out the number of attemps and time taken when the game run and after your guess correct.

Here are my codes:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <math.h>

void MastemindRandom(int random[]);
int * getGuess();
int isValidNumbers(int a, int b, int c, int d);
void run(int * count);

int main(int argc, char** argv) {
    time_t begin, end;
    int count;
    double timeTaken;
    time(&begin); //count the time when the program start
    run(&count);
    time(&end); 
    if (count != 10) {    //if player wins, print out the time taken 
       timeTaken = difftime(end, begin);
       printf("time taken: %f", timeTaken);
    }
    return (EXIT_SUCCESS);
}

void run(int * count) {
    int random[4], *guess = malloc(4);
    int imperfect, perfect, flag[4] = { 0, 0, 0, 0 };
    MastemindRandom(random);  //doing the random
    do {
        imperfect = perfect = 0;
        memset(flag, 0, sizeof(int) * 4);
        printf("input your guess: ");
        guess = getGuess();
        for (int i = 0; i < 4; i++) {
            if (random[i] == guess[i]) {
                perfect++; //if player guess correctly any of 4 numbers, both in position and value, increase the perfect count and set flag of that position
                flag[i] = 1;
                continue;
            }
            for (int j = 0; j < 4; j++) {
                if (flag[j] == 1) //if that position has been check before
                    continue;

                if (random[i] == guess[j]) {
                    imperfect++; //if the player guess correct in value but not correct in position, increase the imperfect and flag it
                    flag[j] = 1;
                    break;
                }
            }
        }
        printf("you have %d perfect match and %d imperfect match\n", perfect, imperfect);
        if (perfect == 4) {
            (*count)++;
            break;
        } else {
            printf("sorry you didn't win, better luck next time\n");
            (*count)++;
        }
    } while (*count != 10);
    if (*count == 10) {
        printf("sorry you don't win the game, the real numbers are: ");
        for (int i = 0; i < 4; i++) {
            printf("%d ", random[i]);
        }
    } else {
        printf("congratulation, you won after %d tries \n", *count);
    }
}

void MastemindRandom(int random[]) {
    srand(time(NULL));
    for (int i = 0; i < 4; i++) {
        random[i] = rand() % +6;
        printf("%d ", random[i]);
    }
    printf("\n");
}

int * getGuess() {
    int* numbers = malloc(4);
    int a, b, c, d;
    do {
        scanf("%d %d %d %d", &a, &b, &c, &d);
        while (getchar() != '\n'){}
    } while (!isValidNumbers(a, b, c, d) && printf("Input from 0 to 5 only, input again: "));
    numbers[0] = a;
    numbers[1] = b;
    numbers[2] = c;
    numbers[3] = d;
    return numbers;
}

int isValidNumbers(int a, int b, int c, int d) {
    if (a < 0 || b < 0 || c < 0 || d < 0 ||
        a > 5 || b > 5 || c > 5 || d > 5)
        return 0;
    else return 1;
}

My code run with no error, but the calculation of the perfect and imperfect match are wrong in some conditions

Here are the output: (the first four numbers of all outputs below are the random that I print out to check)

Condition when random has no equal numbers (still OK)

3 2 4 5 
input your guess: 4 4 4 4
you have 1 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 2 3 5
you have 2 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 3 2 2
you have 0 perfect match and 3 imperfect match
sorry you didn't win, better luck next time
input your guess: 5 2 3 4
you have 1 perfect match and 3 imperfect match
sorry you didn't win, better luck next time
input your guess: 5 5 5 5
you have 1 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 1 2 3 4
you have 1 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 4 3 4
you have 1 perfect match and 1 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 5 2 3
you have 1 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 3 5 5
you have 1 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 1 3 2 2
you have 0 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
sorry you don't win the game, the real numbers are: 3 2 4 5 
RUN SUCCESSFUL (total time: 1m 50s)

Condition when random has 2 equal numbers (has some incorrect)

3 2 3 5 
input your guess: 3 3 2 5
you have 2 perfect match and 2 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 3 3 3 
you have 2 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 4 3 5
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 4 4 3
you have 1 perfect match and 1 imperfect match
sorry you didn't win, better luck next time
input your guess: 3 2 4 5
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 4 2 3 5
you have 3 perfect match and 1 imperfect match //wrong, should be 3 and 0
sorry you didn't win, better luck next time
input your guess: 5 2 3 5
you have 3 perfect match and 1 imperfect match //wrong, should be 3 and 0
sorry you didn't win, better luck next time
input your guess: 1 2 3 5
you have 3 perfect match and 1 imperfect match //wrong, should be 3 and 0
sorry you didn't win, better luck next time

condition when random has 3 equal numbers (has some incorrect)

3 3 3 4 
input your guess:  3 4 3 4
you have 3 perfect match and 1 imperfect match  //wrong, should be 3 perfect and 0 imperfect  
sorry you didn't win, better luck next time
input your guess: 4 3 4 3
you have 1 perfect match and 3 imperfect match  //wrong, should be 1 perfect and 2 imperfect
sorry you didn't win, better luck next time
input your guess: 4 4 4 3
you have 0 perfect match and 2 imperfect match   
sorry you didn't win, better luck next time
input your guess: 3 4 3 4
you have 3 perfect match and 1 imperfect match   //wrong, should be 3 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 5 3 3 4
you have 3 perfect match and 1 imperfect match   //wrong, should be 3 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 2 3 3 4
you have 3 perfect match and 1 imperfect match   //wrong, should be 3 perfect and 0 imperfect 
sorry you didn't win, better luck next time
input your guess: 3 3 3 2
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
input your guess: 1 2 3 4
you have 2 perfect match and 1 imperfect match   //wrong, should be 2 perfect and 0 imperfect
sorry you didn't win, better luck next time
input your guess: 3 3 4 4
you have 3 perfect match and 0 imperfect match   
sorry you didn't win, better luck next time
input your guess: 3 3 2 4
you have 3 perfect match and 0 imperfect match
sorry you didn't win, better luck next time
sorry you don't win the game, the real numbers are: 3 3 3 4 
RUN SUCCESSFUL (total time: 4m 53s)

I don't want to print out more, because basically, my logic is totally wrong. Can you guys help me out, i would be very appreciated.




Aucun commentaire:

Enregistrer un commentaire