Skip to content

Commit 220ca4f

Browse files
committed
ajout des fin de parties dans le rendering
1 parent 6f856a2 commit 220ca4f

10 files changed

Lines changed: 94 additions & 29 deletions

File tree

lib/chessboard/chessboard.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
#include "bitboard.h"
1111
#include "masks.h"
1212

13+
typedef struct RepetitionTable RepetitionTable;
1314

1415
// Définition de la structure Chessboard
15-
typedef struct
16+
typedef struct Chessboard
1617
{
1718
uint64_t pawns;
1819
uint64_t knights;
@@ -29,8 +30,17 @@ typedef struct
2930

3031
int white_to_play;
3132

33+
RepetitionTable *hashtable;
34+
3235
} Chessboard;
3336

37+
enum
38+
{
39+
PLAYING = 0,
40+
DRAW = 1,
41+
WIN = 2,
42+
}; // Game state
43+
3444
// Initialisation d'un échiquier avec la position de départ
3545
void init_chessboard(Chessboard *board);
3646

lib/chessboard/chessboardcontroller.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
#include "bitboards/bitboard.h"
55
#include "chessrules.h"
66
#include "move/move.h"
7+
#include "hashtables/hash_table.h"
78

89
/// @brief renvoie la liste des coups possibles pour une piece
910
/// @param piecePos la position de la piece
1011
/// @param chessboard l'echequier
1112
/// @return la liste des coups possibles
12-
int getlegalmoves(int piecePos, Chessboard *chessboard, Move piece_moves[250]);
13+
int get_legal_moves(int piecePos, Chessboard *chessboard, Move piece_moves[250]);
1314

1415
/// @brief Récupère tous les coups légaux possibles pour toutes les pièces du joueur
1516
/// @param chessboard l'échiquier
1617
/// @return Tout les coups légaux possibles
17-
int getalllegalmoves(Chessboard *chessboard, Move all_moves[250]);
18+
int get_all_legal_moves(Chessboard *chessboard, Move all_moves[250]);
1819

1920
/// @brief Joue un coup sur l'échiquier. Cette fonction met à jour l'echequier
2021
/// @param board l'echequier qui va être mis à jour
@@ -36,13 +37,21 @@ bool try_play_move(Chessboard *board, Move *move);
3637
void unplay_move(Chessboard *board, Move move);
3738

3839
/// @brief Renvoie true si il y a echec
39-
/// @param board
40-
/// @return
40+
/// @param board
41+
/// @return
4142
bool is_check(Chessboard *board);
4243

4344
/// @brief Renvoie toute les captures possibles de la position
44-
/// @param chessboard
45-
/// @param all_moves
46-
/// @return
45+
/// @param chessboard
46+
/// @param all_moves
47+
/// @return
4748
int get_all_captures(Chessboard *chessboard, Move captures[250]);
4849

50+
/// @brief Joue le coup et renvoie le "Game State"
51+
/// Playing = 0;
52+
/// Draw = 1;
53+
/// Checkmate = 2
54+
/// @param board
55+
/// @param move
56+
/// @return
57+
int play_move_check_gameover(Chessboard *board, Move move);

lib/chessboard/hashtables/hash_table.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,34 @@
44
#include <stdint.h>
55
#include <stdlib.h>
66
#include <time.h>
7-
#include "../chessboard.h"
7+
#include <stdbool.h>
88

99
typedef uint64_t ZobristKey;
1010

1111
#define HASH_TABLE_SIZE 16384
1212

13+
typedef struct Chessboard Chessboard;
14+
1315
// Structure pour une entrée de la table de répétition
14-
typedef struct {
16+
typedef struct
17+
{
1518
ZobristKey key;
1619
int count;
1720
} HashEntry;
1821

1922
// Table de répétition
20-
typedef struct {
23+
typedef struct RepetitionTable
24+
{
2125
HashEntry entries[HASH_TABLE_SIZE];
2226
ZobristKey history[512];
2327
int history_count;
2428
} RepetitionTable;
2529

2630
// Tables Zobrist globales
27-
extern ZobristKey zobrist_pieces[12][64]; // 12 types de pièces × 64 cases
28-
extern ZobristKey zobrist_castling[16]; // 16 combinaisons de droits de roque
29-
extern ZobristKey zobrist_en_passant[64]; // En passant par case
30-
extern ZobristKey zobrist_side_to_move; // Trait aux blancs/noirs
31+
extern ZobristKey zobrist_pieces[12][64]; // 12 types de pièces × 64 cases
32+
extern ZobristKey zobrist_castling[16]; // 16 combinaisons de droits de roque
33+
extern ZobristKey zobrist_en_passant[64]; // En passant par case
34+
extern ZobristKey zobrist_side_to_move; // Trait aux blancs/noirs
3135

3236
// Fonctions d'initialisation
3337
void init_zobrist(void);

lib/ui/rendering.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef struct
2424
GenericList *colored_squares;
2525
BotConnector *bot1;
2626
BotConnector *bot2;
27+
int *GameState;
2728
} GameEnvironement;
2829

2930
#pragma once

src/ai/ai.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static int alpha_beta(Chessboard *board, int depth, int alpha, int beta, bool ma
2121
}
2222

2323
Move moves[250];
24-
int nb_moves = getalllegalmoves(board, moves);
24+
int nb_moves = get_all_legal_moves(board, moves);
2525

2626
if (nb_moves == 0)
2727
return evaluate_position(board);
@@ -74,7 +74,7 @@ static int alpha_beta(Chessboard *board, int depth, int alpha, int beta, bool ma
7474
static SearchResult search_best_move_alpha_beta(Chessboard *board, int depth)
7575
{
7676
Move moves[250];
77-
int nb_moves = getalllegalmoves(board, moves);
77+
int nb_moves = get_all_legal_moves(board, moves);
7878

7979
SearchResult result;
8080
result.best_move = moves[0];

src/chessboard/chessboard.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "chessboard.h"
2+
#include "hashtables/hash_table.h"
23

34
#include "stdlib.h"
45

@@ -28,6 +29,12 @@ void init_chessboard(Chessboard *board)
2829
board->castling = create_1bit_board(casttling_squares[0]) + create_1bit_board(casttling_squares[1]) + create_1bit_board(casttling_squares[2]) + create_1bit_board(casttling_squares[3]);
2930

3031
board->white_to_play = true;
32+
33+
RepetitionTable *t = malloc(sizeof(RepetitionTable));
34+
init_repetition_table(t);
35+
board->hashtable = t;
36+
37+
init_zobrist();
3138
}
3239

3340
void print_chessboard(const Chessboard *board)
@@ -310,7 +317,12 @@ void init_chessboard_from_fen(Chessboard *board, const char *fen)
310317
ptr += 2;
311318
}
312319

320+
RepetitionTable *t = malloc(sizeof(RepetitionTable));
321+
init_repetition_table(t);
322+
board->hashtable = t;
323+
313324
init_bitboards();
325+
init_zobrist();
314326
}
315327

316328
void copy_chessboard(const Chessboard *src, Chessboard *dst)

src/chessboard/chessboardcontroller.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static int get_captures_piece(int piecePos, Chessboard *chessboard, Move piece_m
207207
return nbmoves;
208208
}
209209

210-
int getlegalmoves(int piecePos, Chessboard *chessboard, Move piece_moves[250])
210+
int get_legal_moves(int piecePos, Chessboard *chessboard, Move piece_moves[250])
211211
{
212212
uint64_t playerPieces = chessboard->white_to_play ? chessboard->occupied_white : chessboard->occupied_black;
213213

@@ -264,7 +264,7 @@ int get_all_captures(Chessboard *chessboard, Move all_moves[250])
264264
return icoup;
265265
}
266266

267-
int getalllegalmoves(Chessboard *chessboard, Move all_moves[250])
267+
int get_all_legal_moves(Chessboard *chessboard, Move all_moves[250])
268268
{
269269
uint64_t playerPieces = chessboard->white_to_play ? chessboard->occupied_white : chessboard->occupied_black;
270270
int icoup = 0;
@@ -274,7 +274,7 @@ int getalllegalmoves(Chessboard *chessboard, Move all_moves[250])
274274
int piecePos = get_lsb_index(playerPieces);
275275
Move piece_moves[256];
276276

277-
int nb_coups = getlegalmoves(piecePos, chessboard, piece_moves);
277+
int nb_coups = get_legal_moves(piecePos, chessboard, piece_moves);
278278

279279
// Ajoute les mouvements de cette pièce à la liste globale
280280
for (int i = 0; i < nb_coups; i++)
@@ -408,7 +408,7 @@ static void update_bitboard_promotion(int promotion_flag, Chessboard *board, uin
408408
static bool is_legal_move(Chessboard *board, Move *move)
409409
{
410410
Move legal_moves[250];
411-
int nbcoups = getalllegalmoves(board, legal_moves);
411+
int nbcoups = get_all_legal_moves(board, legal_moves);
412412

413413
for (int i = 0; i < nbcoups; i++)
414414
{
@@ -468,6 +468,7 @@ inline void play_move(Chessboard *board, Move move)
468468
}
469469

470470
board->white_to_play = !board->white_to_play;
471+
add_position(board->hashtable, compute_hash(board));
471472
}
472473

473474
inline void unplay_move(Chessboard *board, Move move)
@@ -505,4 +506,22 @@ inline void unplay_move(Chessboard *board, Move move)
505506
}
506507
board->enpassant = move.en_passant;
507508
board->white_to_play = !board->white_to_play;
509+
pop_position(board->hashtable);
510+
}
511+
512+
int play_move_check_gameover(Chessboard *board, Move move)
513+
{
514+
play_move(board, move);
515+
516+
if (is_threefold_repetition(board->hashtable, compute_hash(board)))
517+
return DRAW;
518+
519+
Move moves[250];
520+
if (get_all_legal_moves(board, moves) != 0)
521+
return PLAYING;
522+
523+
if (is_check(board))
524+
return WIN;
525+
526+
return DRAW;
508527
}

src/chessboard/hashtables/hash_table.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "hash_table.h"
2+
#include "../chessboard.h"
23

34
ZobristKey zobrist_pieces[12][64];
45
ZobristKey zobrist_castling[16];

src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static int run_test(Chessboard *board, int profondeur)
3333
return 1;
3434

3535
Move liste_coups[250];
36-
int nbmoves = getalllegalmoves(board, liste_coups);
36+
int nbmoves = get_all_legal_moves(board, liste_coups);
3737

3838
int total = 0;
3939

@@ -64,7 +64,7 @@ static uint64_t run_test_mt(Chessboard *board, int profondeur)
6464
return 1;
6565

6666
Move liste_coups[250];
67-
int nbmoves = getalllegalmoves(board, liste_coups);
67+
int nbmoves = get_all_legal_moves(board, liste_coups);
6868

6969
pthread_t *threads = malloc(sizeof(pthread_t) * nbmoves);
7070
ThreadArgs *args = malloc(sizeof(ThreadArgs) * nbmoves);

src/ui/rendering.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void draw_board(SDL_Renderer *renderer)
4040
}
4141
}
4242

43-
static inline GameEnvironement create_game_environment(SDL_Event *event, Chessboard *board, SDL_Renderer *renderer, GenericList *colored_squares, BotConnector *bot1, BotConnector *bot2)
43+
static inline GameEnvironement create_game_environment(SDL_Event *event, Chessboard *board, SDL_Renderer *renderer, GenericList *colored_squares, BotConnector *bot1, BotConnector *bot2, int *GameState)
4444
{
4545
GameEnvironement env;
4646
env.event = event;
@@ -49,6 +49,7 @@ static inline GameEnvironement create_game_environment(SDL_Event *event, Chessbo
4949
env.colored_squares = colored_squares;
5050
env.bot1 = bot1;
5151
env.bot2 = bot2;
52+
env.GameState = GameState;
5253

5354
return env;
5455
}
@@ -214,7 +215,7 @@ static void render_fen(SDL_Renderer *renderer, const char *fen)
214215

215216
static int set_moves(Chessboard *board, Move moves[250], int pos_piece, GenericList *colored_squares)
216217
{
217-
int nbmoves = getlegalmoves(pos_piece, board, moves);
218+
int nbmoves = get_legal_moves(pos_piece, board, moves);
218219
if (nbmoves == 0)
219220
return 0;
220221

@@ -229,7 +230,7 @@ static int set_moves(Chessboard *board, Move moves[250], int pos_piece, GenericL
229230

230231
void play_move_all(GameEnvironement env, Move move)
231232
{
232-
play_move(env.board, move);
233+
*env.GameState = play_move_check_gameover(env.board, move);
233234

234235
char movestring[MOVE_SIZE];
235236
move_to_string(&move, movestring);
@@ -312,7 +313,7 @@ static void handle_player_event(GameEnvironement env, int *clicked_square)
312313
{
313314
if (is_in_list(env.colored_squares, &square))
314315
{
315-
int nbmoves = getlegalmoves(*clicked_square, env.board, list_moves);
316+
int nbmoves = get_legal_moves(*clicked_square, env.board, list_moves);
316317
render_play_move(env, list_moves, square, nbmoves);
317318
}
318319
reset_colored_squares(env.colored_squares);
@@ -331,8 +332,10 @@ static GameEnvironement init_game_environment(char *startpos, SDL_Renderer *rend
331332

332333
BotConnector *bot1 = malloc(sizeof(BotConnector));
333334
BotConnector *bot2 = malloc(sizeof(BotConnector));
335+
int *GameState = malloc(sizeof(int));
336+
*GameState = 0;
334337

335-
GameEnvironement env = create_game_environment(event, board, renderer, colored_squares, bot1, bot2);
338+
GameEnvironement env = create_game_environment(event, board, renderer, colored_squares, bot1, bot2, GameState);
336339
return env;
337340
}
338341

@@ -351,6 +354,9 @@ static void cleanup_ui(SDL_Renderer *renderer, SDL_Window *window, GameEnvironem
351354
if (env.bot2)
352355
free(env.bot2);
353356

357+
if (env.GameState)
358+
free(env.GameState);
359+
354360
for (int i = 0; i < 12; i++)
355361
if (piece_textures[i])
356362
SDL_DestroyTexture(piece_textures[i]), piece_textures[i] = NULL;
@@ -425,7 +431,7 @@ static int get_and_play_best_move(int color, GameEnvironement env)
425431

426432
if (bot_get_best_move(bot, best_move) != 0)
427433
return 1;
428-
434+
429435
Move move = get_move(best_move);
430436
play_move_all(env, move);
431437

@@ -477,6 +483,9 @@ static void game_loop(char *startpos, SDL_Renderer *renderer, int color_ai, SDL_
477483

478484
while (running)
479485
{
486+
if (*env.GameState != PLAYING)
487+
running = false;
488+
480489
while (SDL_PollEvent(env.event))
481490
{
482491
if (!handle_SDL_events(color_ai, &clicked_square, env))

0 commit comments

Comments
 (0)