From 8feb400da479f10c7333e0c45e23a9d3581e7cfd Mon Sep 17 00:00:00 2001 From: Josue KONAN Date: Sun, 24 Nov 2024 18:56:06 +0100 Subject: [PATCH] =?UTF-8?q?T=C3=A9l=C3=A9verser=20les=20fichiers=20vers=20?= =?UTF-8?q?"/"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blocus.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 blocus.c diff --git a/blocus.c b/blocus.c new file mode 100644 index 0000000..efb0c3b --- /dev/null +++ b/blocus.c @@ -0,0 +1,59 @@ +#include "blocus.h" +#include + +void initialize_game(Game *game, int size) { + game->size = size; + for (int i = 0; i < size; i++) + for (int j = 0; j < size; j++) + game->grid[i][j] = EMPTY; + + game->player1_x = 0; + game->player1_y = 0; + game->player2_x = size - 1; + game->player2_y = size - 1; + + game->grid[0][0] = PLAYER1; + game->grid[size - 1][size - 1] = PLAYER2; + game->current_player = PLAYER1; + game->winner = 0; +} + +int is_valid_move(Game *game, int x, int y) { + return x >= 0 && x < game->size && y >= 0 && y < game->size && game->grid[x][y] == EMPTY; +} + +void move_player(Game *game, int x, int y) { + if (game->current_player == PLAYER1) { + game->grid[game->player1_x][game->player1_y] = EMPTY; + game->player1_x = x; + game->player1_y = y; + game->grid[x][y] = PLAYER1; + } else { + game->grid[game->player2_x][game->player2_y] = EMPTY; + game->player2_x = x; + game->player2_y = y; + game->grid[x][y] = PLAYER2; + } +} + +void block_cell(Game *game, int x, int y) { + if (is_valid_move(game, x, y)) { + game->grid[x][y] = BLOCKED; + game->current_player = (game->current_player == PLAYER1) ? PLAYER2 : PLAYER1; + } +} + +int has_lost(Game *game) { + int x = (game->current_player == PLAYER1) ? game->player1_x : game->player2_x; + int y = (game->current_player == PLAYER1) ? game->player1_y : game->player2_y; + + for (int dx = -1; dx <= 1; dx++) { + for (int dy = -1; dy <= 1; dy++) { + if (is_valid_move(game, x + dx, y + dy)) + return 0; + } + } + + game->winner = (game->current_player == PLAYER1) ? PLAYER2 : PLAYER1; + return 1; // Le joueur est bloqué +}