//essaye de donner une couleur a une position, si on a 2 voisin de couleurs differentes non VIDE, retourne VIDE //sinon retourne la couleur de la case d'une couleur //si c'est vide, retourne BORD pour ne pas perturber la fonction r�cursive. ECouleur couleurVoisin(SPlateau* plateau, SPosition* position) { SPosition* haut = positionHaut(position); SPosition* bas = positionBas(position); SPosition* gauche = positionGauche(position); SPosition* droite = positionDroite(position); ECouleur c1 = plateau_get(plateau, haut); ECouleur c2 = plateau_get(plateau, bas); ECouleur c3 = plateau_get(plateau, gauche); ECouleur c4 = plateau_get(plateau, droite); ECouleur retourner = UNDEFINED; if(c1==NOIR && c2!=BLANC && c3!=BLANC && c4!=BLANC) retourner = NOIR; if(c1==BLANC && c2!=NOIR && c3!=NOIR && c4!=NOIR) retourner = BLANC; if(c1!=BLANC && c2==NOIR && c3!=BLANC && c4!=BLANC)retourner = NOIR; if(c1!=NOIR && c2==BLANC && c3!=NOIR && c4!=NOIR) retourner = BLANC; if(c1!=BLANC && c2!=BLANC && c3==NOIR && c4!=BLANC)retourner = NOIR; if(c1!=NOIR && c2!=NOIR && c3==BLANC && c4!=NOIR) retourner = BLANC; if(c1!=BLANC && c2!=BLANC && c3!=BLANC && c4!=NOIR)retourner = NOIR; if(c1!=NOIR && c2!=NOIR && c3!=NOIR && c4!=BLANC) retourner = BLANC; if(c1==VIDE && c2==VIDE && c3 == VIDE && c4==VIDE) retourner = BORD; if(retourner==UNDEFINED) retourner = VIDE; detruirePosition(haut); detruirePosition(bas); detruirePosition(gauche); detruirePosition(droite); return retourner; }
SPosition* voisinBas(SPlateau* plateau, SPosition* position) { ECouleur coul = plateau_get(plateau,positionBas(position)); if(coul == VIDE || coul == BORD) return NULL; else { return positionBas(position); } }
int plateau_est_identique(SPlateau* plateau, SPlateau* ancienPlateau) { int res = 1; int taille = taille_plateau(plateau); int i,j; SPosition pos; for(i = 0 ; i < taille && res ; ++i) { for(j = 0 ; j < taille && res ; ++j) { pos.x = i; pos.y = j; if(plateau_get(plateau, &pos) != plateau_get(ancienPlateau, &pos)) { res = 0; } } } return res; }
void plateau_copie(SPlateau* from, SPlateau* to) { int taille = taille_plateau(from); int i,j; SPosition pos; for(i = 0 ; i < taille ; ++i) { for(j = 0 ; j < taille ; ++j) { pos.x = i; pos.y = j; plateau_set(to, &pos, plateau_get(from, &pos)); } } }
void plateau_affichage(SPlateau* plateau) { int taille = plateau->taille; int i,j; printf(" "); for(j = 0 ; j < taille ; ++j) { if(j < 9) { printf("%d ", j+1); } else { printf("%d", j+1); } } printf("\n"); for(i = 0 ; i < taille ; ++i) { printf("%c ", 'A'+i); for(j = 0 ; j < taille ; ++j) { SPosition pos; pos.x = i; pos.y = j; printf("%c ", couleur_to_char(plateau_get(plateau, &pos))); } printf("%c\n", 'A'+i); } printf(" "); for(j = 0 ; j < taille ; ++j) { if(j < 9) { printf("%d ", j+1); } else { printf("%d", j+1); } } printf("\n"); }
/** @ingroup test * @brief Affiche le plateau en mode "debug" * @param plateau à afficher. */ void afficher_plateau(Plateau plateau) { int taille = plateau_get_taille(plateau); // pour tester les chaines Position position_chaine = position(taille / 2, taille / 2, taille); Chaine chaine = plateau_determiner_chaine(plateau, position_chaine); Libertes libertes = determiner_libertes(plateau, chaine); Territoire territoire = determiner_territoire(plateau, position(taille - 1, taille - 1, taille)); const char* couleur_territoire = NULL; switch (ensemble_colore_couleur(territoire)) { case BLANC: couleur_territoire = C_YELLOW; break; case NOIR: couleur_territoire = C_BLUE; break; case VIDE: couleur_territoire = C_GREY; break; } for (int y = 0; y < taille; y++) { for (int x = 0; x < taille; x++) { Couleur couleur = plateau_get(plateau, x, y); // highlight de la chaine const char* ansi = C_NORMAL; if (chaine != NULL && gosh_appartient(chaine, position(x, y, taille))) ansi = C_RED; else if (libertes != NULL && gosh_appartient(libertes, position(x, y, taille))) ansi = C_GREEN; else if (territoire != NULL && gosh_appartient(territoire, position(x, y, taille))) ansi = couleur_territoire; printf("%s%d "C_NORMAL, ansi, couleur); } printf("\n"); } if (chaine) detruire_chaine(chaine); if (libertes) detruire_libertes(libertes); if (territoire) detruire_territoire(territoire); }
/** @ingroup test * @brief Teste les plateaux * */ void test_plateau(void) { int taille = 19; Plateau plateau = creer_plateau(taille); assert(plateau_get_taille(plateau) == taille); for (int y = 0; y < taille; y++) { for (int x = 0; x < taille; x++) { Couleur c = rand() % 3; plateau_set(plateau, x, y, c); assert(plateau_get(plateau, x, y) == c); } } afficher_plateau(plateau); detruire_plateau(plateau); }
/** @ingroup test * @brief Teste les opérations d'accès aux cases du tableau */ void test_get_set(void) { Plateau p = creer_plateau(9); const uint32_t * data = plateau_data(p); puts("debut"); for (size_t i = 0; i < plateau_data_size(9) / sizeof(uint32_t); ++i) printf("%x\n", data[i]); puts("fin"); plateau_set(p, 0, 1, BLANC); printf("%d %d\n", plateau_get(p, 0, 1), BLANC); puts("debut"); data = plateau_data(p); for (size_t i = 0; i < plateau_data_size(9) / sizeof(uint32_t); ++i) printf("%x\n", data[i]); puts("fin"); detruire_plateau(p); }
static void afficher_jouer(struct state* state, SDL_Surface* surface) { struct jouerdata* jouer = state->data; Partie partie = jouer->partie; int taille = jouer->taille; int x1 = W * .2; int y1 = H * .2; int w = MAX(W * .8 - x1, H * .8 - y1); w -= w % (taille); int pixel_par_case = w / (taille); int interbordure = 2; int bordure = (w - pixel_par_case * (taille - 1)) / 2; // bois set_color(240, 174, 95); draw_rect(surface, x1, y1, w, w); // dessin des interbordures set_color(30, 30, 30); for (int x = 0; x < taille; x++) { draw_rect(surface, x1 + bordure + x * pixel_par_case - interbordure / 2, y1 + bordure, interbordure, pixel_par_case * (taille - 1)); } for (int y = 0; y < taille; y++) { draw_rect(surface, x1 + bordure, y1 + bordure + y * pixel_par_case - interbordure / 2, pixel_par_case * (taille - 1), interbordure); } int taille_stone = taille == 9 ? 30 : taille == 13 ? 24 : 16; int taille_stone2 = taille_stone / 4; Position hov = jouer->hovered; Chaine chaine = position_est_valide(hov) ? plateau_determiner_chaine(partie->plateau, hov) : NULL; Libertes libertes = chaine ? determiner_libertes(partie->plateau, chaine) : NULL; EnsemblePosition yeux = chaine ? lesYeuxDeLaChaine(chaine, partie->plateau) : NULL; for (int x = 0; x < taille; x++) { for (int y = 0; y < taille; y++) { Position pos = position(x, y, taille); Couleur c = plateau_get(partie->plateau, x, y); bool draw = false; if (c != VIDE) { if (c == BLANC) { set_color(210, 210, 210); } else { set_color(40, 40, 40); } draw = true; } else if (position_est_valide(hov) && hov.x == x && hov.y == y) { if (partie->joueur_courant == JOUEUR_BLANC) { set_color(240, 240, 240); } else { set_color(20, 20, 20); } draw = true; } int marge = (taille == 9 ? 2 : 3); if (draw) { // affichage de la pierre int sx, sy; get_position_vers_ecran(jouer, x, y, &sx, &sy); sx -= taille_stone / 2; sy -= taille_stone / 2; draw_rect(surface, sx, sy, taille_stone, taille_stone); } else if ((get_marge(x, taille) == marge || x == taille / 2) && (get_marge(y, taille) == marge || y == taille / 2)) { // affichage du hoshi int sx, sy; get_position_vers_ecran(jouer, x, y, &sx, &sy); set_color(0, 0, 0); draw_rect(surface, sx - 3, sy - 3, 6, 6); } // affichage d'indicateurs d'aide draw = false; if (chaine && gosh_appartient(chaine, pos)) { set_color(120, 120, 120); draw = true; } else if (yeux && gosh_appartient(yeux, pos)) { set_color(255, 130, 130); draw = true; } else if (libertes && gosh_appartient(libertes, pos)) { set_color(200, 40, 40); draw = true; } if (draw) { int sx, sy; get_position_vers_ecran(jouer, x, y, &sx, &sy); sx -= taille_stone2 / 2; sy -= taille_stone2 / 2; draw_rect(surface, sx, sy, taille_stone2, taille_stone2); } } } if (chaine) detruire_chaine(chaine); if (libertes) detruire_libertes(libertes); if (yeux) detruire_ensemble_position(yeux); if (!partie->finie) { if (partie_en_cours_de_handicap(partie)) { afficher_label(surface, jouer->handicap); } else { afficher_label(surface, jouer->au_tour_de[partie->joueur_courant]); } } else { afficher_label(surface, jouer->partie_finie); afficher_label(surface, jouer->score); } afficher_bouton(surface, jouer->retour_menu); afficher_bouton(surface, jouer->passer_son_tour); afficher_textinput(surface, jouer->nom_partie); afficher_bouton(surface, jouer->sauvegarder); }