Skip to content

Commit fbb78bf

Browse files
committed
fix bug about game endings
1 parent 220ca4f commit fbb78bf

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

src/ui/rendering.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -460,16 +460,36 @@ static void try_connect_bot(BotConnector *bot1, char *bot_path)
460460
printf("erreur de connection au bot...\n");
461461
}
462462

463-
static void init_bots(BotConnector *bot1, BotConnector *bot2, int color_ai)
463+
static void init_bot(BotConnector *bot, char *bot_path, char *fen)
464+
{
465+
try_connect_bot(bot, bot_path);
466+
if (bot_set_fen(bot, fen) != 0)
467+
printf("Connexion error with the bot... function bot_set_fen\n");
468+
}
469+
470+
static void init_bots(GameEnvironement env, int color_ai)
464471
{
465472
if (color_ai == -1)
466473
return;
467474

475+
char fen[256];
476+
return_fen_code(env.board, fen);
477+
468478
if (color_ai == WHITE || color_ai == 2)
469-
try_connect_bot(bot1, bot1_path);
479+
init_bot(env.bot1, bot1_path, fen);
470480

471481
if (color_ai == BLACK || color_ai == 2)
472-
try_connect_bot(bot2, bot2_path);
482+
init_bot(env.bot2, bot2_path, fen);
483+
}
484+
485+
static void print_winner(GameEnvironement env)
486+
{
487+
if (*env.GameState == DRAW)
488+
printf("Tie\n\n\n");
489+
if (env.board->white_to_play)
490+
printf("Black wins\n\n\n");
491+
else
492+
printf("White wins\n\n\n");
473493
}
474494

475495
static void game_loop(char *startpos, SDL_Renderer *renderer, int color_ai, SDL_Window *window)
@@ -479,20 +499,22 @@ static void game_loop(char *startpos, SDL_Renderer *renderer, int color_ai, SDL_
479499
bool running = true;
480500

481501
ui_refresh_board(env);
482-
init_bots(env.bot1, env.bot2, color_ai);
502+
init_bots(env, color_ai);
483503

484504
while (running)
485505
{
486-
if (*env.GameState != PLAYING)
487-
running = false;
488-
489-
while (SDL_PollEvent(env.event))
506+
while (SDL_PollEvent(env.event) && *env.GameState == PLAYING)
490507
{
491508
if (!handle_SDL_events(color_ai, &clicked_square, env))
492509
running = false;
493510
}
494-
handle_ai_turn(color_ai, env);
511+
if (*env.GameState == PLAYING)
512+
handle_ai_turn(color_ai, env);
513+
514+
if (*env.GameState != PLAYING)
515+
running = false;
495516
}
517+
print_winner(env);
496518
cleanup_ui(renderer, window, env);
497519
}
498520

0 commit comments

Comments
 (0)