SAE11_2024/blocus.c

60 lines
1.8 KiB
C

#include "blocus.h"
#include <stdio.h>
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é
}