PI-ITBA / 2024_01

8 stars 0 forks source link

Ejercicio 4 (checkBoard). Parcial sin fecha. #359

Closed joseravera closed 3 months ago

joseravera commented 3 months ago

Hola, buen día. Quería consultar si este ejercicio está bien (pasa el programa de prueba del ejercicio). Gracias! Captura de pantalla 2024-06-23 100439 Captura de pantalla 2024-06-23 100539

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define DIM 4
#define CANT_COLORES 6

int checkBoard ( unsigned char board[][DIM]);
int checkFils (unsigned char fil[]);
int checkCols (unsigned char board[][DIM], int col);

typedef enum colors {black=0, white, blue, red, purple, orange};

int
main(void) {
    unsigned char t[DIM][DIM] = {
    {black, white, black, white},
    {white, black, white, black},
    {black, white, black, white},
    {white, black, white, black}};
    // Es un tablero válido
    assert(checkBoard(t) == 1);
    unsigned char t2[DIM][DIM] = {
    {black, white, black, white},
    {white, black, white, black},
    {black, white, black, white},
    {white, black, white, blue}};
    // No es válido pues se usaron más de dos colores
    assert(checkBoard(t2) == 0);
    unsigned char t3[DIM][DIM] = {
    {black, white, black, white},
    {white, black, white, black},
    {white, black, white, black},
    {black, white, black, white}};
    // No es válido pues hay dos posiciones adyacentes del mismo color
    assert(checkBoard(t3) == 0);
    return 0;
}

int checkBoard (unsigned char board[][DIM])  {
    int flag=1;
    for (int i=0; i<DIM && flag; i++) {
        if ((flag = checkFils(board[i]))) {
            flag = checkCols(board, i);
        }
    }
    return flag;
}

int checkFils (unsigned char fil[]) {
    int aux[CANT_COLORES] = {0};
    int count=0;
    for (int i=1; i<DIM; i++) {
        if (fil[i-1] == fil[i]) {
            return 0;
        }
        aux[fil[i]] = 1;
    }
    for (int i=0; i<CANT_COLORES; i++) {
        if (aux[i] != 0) {
            count++;
        }
    }
    if (count != 2) {
        return 0;
    }
    return 1;
}

int checkCols (unsigned char board[][DIM], int col) {
    for (int i=1; i<DIM; i++) {
        if (board[i-1][col] == board[i][col]) {
            return 0;
        }
    }
    return 1;
}
ImNotGone commented 3 months ago

no estas cargando en aux, fil[0] porque te lo salteas. Esto lo deberias cambiar

    if (count != 2) {
        return 0;
    }
    return 1;

por un return de la condicion negada.

return count == 2;