Esercizio carcassone

16 February 2017

Views: 206

carcassonne.h

#ifndef CARCASSONE_H
#define CARCASSONE_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct casella {
int x, y;
char lati[4]; //0:nord, 1:est, 2:sud,3:ovest
};

extern bool controlla_casella(const struct casella *c, const struct casella *tabellone, size_t dim);
#endif

carcassonne.c

#include "carcassonne.h"

bool controlla_casella(const struct casella *c, const struct casella *tabellone, size_t dim)
{
for (int i = 0; i < dim; i++) {
if ((tabellone[i].x == c->x) && (tabellone[i].y == c->y))
return false;
}
bool pres = false;
bool combacia = false;
for (int i = 0; i < dim; i++) {
if (((tabellone[i].x) == (c->x - 1)) && (tabellone[i].y == c->y)) {
pres = true;
if (tabellone[i].lati[1] == c->lati[3])
combacia = true;
else {
combacia = false;
break;
}
}
if (((tabellone[i].x) == (c->x + 1)) && (tabellone[i].y == c->y)) {
pres = true;
if (tabellone[i].lati[3] == c->lati[1])
combacia = true;
else {
combacia = false;
break;
}
}
if (((tabellone[i].y) == (c->y - 1)) && (tabellone[i].x == c->x)) {
pres = true;
if (tabellone[i].lati[0] == c->lati[2])
combacia = true;
else {
combacia = false;
break;
}
}
if (((tabellone[i].y) == (c->y + 1)) && (tabellone[i].x == c->x)) {
pres = true;
if (tabellone[i].lati[2] == c->lati[0])
combacia = true;
else {
combacia = false;
break;
}
}

}
if (pres == true && combacia == true)
return true;
else
return false;
}

Share