@@ -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
215216static 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
230231void 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