APL/APL1.1/SAE11_2021/taquin.c

98 lines
2.5 KiB
C
Raw Normal View History

2021-11-29 00:31:21 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include <string.h>
#include "utils.h"
#include "taquin.h"
#include "graph_sup.h"
2021-12-04 00:08:27 +01:00
/* La marge en pixel entre chaque pièce du Taquin */
#define MARGIN 1
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
int OffsetX, OffsetY;
int ImageX, ImageY, PieceX, PieceY;
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
int Lines, Columns;
unsigned char* Taquin;
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
char Filename[100];
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
void DrawPiece(int index, couleur color) {
int CoordX = (index % Columns) * PieceX;
int CoordY = (index / Columns) * PieceY;
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
int ImCoordX = (Taquin[index] % Columns) * PieceX;
int ImCoordY = (Taquin[index] / Columns) * PieceY;
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
ChargerImage(Filename, CoordX + OffsetX, CoordY + OffsetY, ImCoordX, ImCoordY, PieceX, PieceY);
SetColorC(color);
DessinerRectangle(CoordX + OffsetX, CoordY + OffsetY, PieceX - 1, PieceY - 1);
2021-11-29 00:31:21 +01:00
}
2021-12-04 00:08:27 +01:00
void UpdateTaquin() {
int i;
couleur color = GetColor(0, 0, 0);
for (i = 0; i < Lines * Columns; i++) DrawPiece(i, color);
2021-11-29 00:31:21 +01:00
}
2021-12-04 00:08:27 +01:00
/* Vérifie la validité des mouvements et met à jour le taquin graphiquement
si l'argument should_update est égal à 1.*/
int MovePiece(int index, int should_update) {
int moves[4] = {
index + 1,
index - 1,
index + Columns,
index - Columns
};
int i;
couleur color = GetColor(0, 0, 0);
for (i = 0; i < 4; i++) {
if (moves[i] >= 0 && moves[i] < Columns * Lines) {
if (Taquin[moves[i]] == 0) {
Taquin[moves[i]] = Taquin[index];
Taquin[index] = 0;
if (should_update) {
DrawPiece(moves[i], color);
DrawPiece(index, color);
2021-11-29 00:31:21 +01:00
}
2021-12-04 00:08:27 +01:00
return 1;
2021-11-29 00:31:21 +01:00
}
}
}
2021-12-04 00:08:27 +01:00
return 0;
2021-11-29 00:31:21 +01:00
}
2021-12-04 00:08:27 +01:00
/* Crée le Taquin et lance les fonctions liées à la logique et au graphisme du Taquin */
void CreateTaquin(char * filename, int image_width, int image_height, int lines, int columns) {
/* On externalise les valeurs données afin de pouvoir facilement les utiliser plus tard. */
int i;
Lines = lines;
Columns = columns;
strcpy(Filename, filename);
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
ImageX = image_width;
ImageY = image_height;
PieceX = ImageX / Columns;
PieceY = ImageY / Lines;
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
OffsetX = WIDTH - 50 - ImageX;
OffsetY = (HEIGHT - ImageY) / 2 - 50;
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
/* On alloue l'espace mémoire nécéssaire au Taquin et on l'initialise en premier résolu. */
Taquin = calloc(Lines * Columns, sizeof(unsigned char));
for (i = 0; i < Lines * Columns; i++) Taquin[i] = i;
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
SetColor(0, 0, 0);
RemplirRectangle(48, OffsetY - 2, ImageX + 4, ImageY + 4);
RemplirRectangle(OffsetX - 1, OffsetY - 1, ImageX + 2, ImageY + 1);
ChargerImage(Filename, 50, OffsetY, 0, 0, ImageX, ImageY);
2021-11-29 00:31:21 +01:00
2021-12-04 00:08:27 +01:00
/*RandomizeTaquin()*/
2021-11-29 00:31:21 +01:00
UpdateTaquin();
2021-12-04 00:08:27 +01:00
/*TaquinRenderLoop()*/
/*ShowVictoryScreen()*/
2021-11-29 00:31:21 +01:00
}