Skip to content

Commit 21103c2

Browse files
committed
ajout des positions d'ouvertures
1 parent 3b63faf commit 21103c2

6 files changed

Lines changed: 129 additions & 5 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
r1bqk1nr/pppp1ppp/2n5/1B2p3/4P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4
2+
r1bqk1nr/pppp1ppp/2n5/1B2p3/1B2Pp2/5N2/PPPP1PPP/RN1QK2R b KQkq b3 0 4
3+
rnbqkb1r/1p1p1ppp/p2p1n2/8/3NP3/2N5/PPP2PPP/R1BQKB1R w KQkq - 6 6
4+
rnbqk2r/pp1p1ppp/3p1np1/8/3NP3/2N5/PPP2PPP/R1BQKB1R w KQkq - 6 6
5+
rnbqkbnr/ppp2ppp/4p3/3p4/3P4/8/PPP2PPP/RNBQKBNR w KQkq - 0 4
6+
rnbqkbnr/ppp2ppp/4p3/3p4/3P4/8/PPPN1PPP/R1BQKBNR b KQkq - 2 3
7+
rnbqkbnr/pp1ppppp/2p5/8/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3
8+
rnbqkbnr/pp2pppp/2pp4/8/3PP3/5N2/PPP2PPP/RNBQKB1R w KQkq - 0 4
9+
rnbqkbnr/ppp1pppp/8/q7/8/2N5/PPPP1PPP/R1BQKBNR w KQkq - 4 4
10+
rnbqkb1r/pppp1ppp/5n2/4p3/3PP3/8/PPP2PPP/RNBQKBNR b KQkq - 2 3
11+
rnbqkb1r/pppppppp/8/8/3nP3/8/PPP2PPP/RNBQKBNR w KQkq - 0 3
12+
rnbqkb1r/ppp2ppp/4pn2/3p4/2PP4/2N5/PP2PPPP/R1BQKBNR w KQkq - 4 4
13+
rnbqkb1r/ppp2ppp/4pn2/8/2pP4/2N5/PP2PPPP/R1BQKBNR w KQkq - 0 5
14+
r1bqkbnr/pppp1ppp/2n5/4p3/3PP3/2N5/PPP2PPP/R1BQKBNR b KQkq - 2 4
15+
rnb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4
46+
rnbqkb1r/ppp1pppp/5n2/3p4/1P2P3/2P2N2/P2P1PPP/RNBQKB1R b KQkq - 0 3
47+
rnbqkb1r/pp3ppp/4pn2/3p4/1P2P3/2P2N2/P2P1PPP/RNBQKB1R w KQkq - 2 4
48+
r1bqkb1r/pp3ppp/2n1pn2/3p4/1P2P3/2P2N2/P2P1PPP/RNBQKB1R w KQkq - 2 4
49+
rnbqkb1r/ppp2ppp/4pn2/3p4/1P2P3/2P1BN2/P2P1PPP/RN1QKB1R b KQkq - 3 4

lib/tools/openings.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef OPENINGS_H
2+
#define OPENINGS_H
3+
4+
#include <stddef.h>
5+
6+
#define MAX_FEN_LENGTH 256
7+
#define MAX_OPENINGS 512 // largement suffisant
8+
9+
typedef struct {
10+
char positions[MAX_OPENINGS][MAX_FEN_LENGTH];
11+
size_t count;
12+
} OpeningBook;
13+
14+
int load_opening_book(const char *filename, OpeningBook *book);
15+
char *get_random_opening(OpeningBook *book);
16+
char *get_opening(OpeningBook *book, size_t index);
17+
18+
#endif

lib/ui/rendering.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "ai/ai.h"
66
#include "ai/evaluation.h"
77
#include "api_connector.h"
8+
#include "tools/openings.h"
89

910
#include <SDL2/SDL.h>
1011
#include <SDL2/SDL_image.h>
@@ -31,4 +32,4 @@ typedef struct
3132

3233
int ui_main_loop(char *startpos, int color_ai);
3334

34-
int ui_bot_match(char *bot1_path, char *bot2_path, int nb_matches);
35+
int ui_bot_match(char *bot1_path, char *bot2_path);

src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ void run_bot_match(char buffer[MAX_CMD])
9595
return;
9696
}
9797

98-
ui_bot_match("", "", 2);
98+
ui_bot_match("", "");
9999
}
100100

101101
int main(void)

src/tools/openings.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "openings.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <string.h>
5+
#include <time.h>
6+
7+
int load_opening_book(const char *filename, OpeningBook *book)
8+
{
9+
FILE *f = fopen(filename, "r");
10+
if (!f) {
11+
perror("Erreur ouverture opening book");
12+
return -1;
13+
}
14+
15+
book->count = 0;
16+
char buffer[MAX_FEN_LENGTH];
17+
18+
while (fgets(buffer, sizeof(buffer), f) && book->count < MAX_OPENINGS)
19+
{
20+
buffer[strcspn(buffer, "\n")] = '\0'; // retire \n
21+
if (strlen(buffer) == 0) continue; // ignore lignes vides
22+
23+
strncpy(book->positions[book->count], buffer, MAX_FEN_LENGTH - 1);
24+
book->positions[book->count][MAX_FEN_LENGTH - 1] = '\0';
25+
book->count++;
26+
}
27+
28+
fclose(f);
29+
return 0;
30+
}
31+
32+
char *get_random_opening(OpeningBook *book)
33+
{
34+
if (book->count == 0) return NULL;
35+
size_t index = rand() % book->count;
36+
return book->positions[index];
37+
}
38+
39+
char *get_opening(OpeningBook *book, size_t index)
40+
{
41+
if (index >= book->count) return NULL;
42+
return book->positions[index];
43+
}

src/ui/rendering.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define WINDOW_WIDTH 1000
44
#define WINDOW_HEIGHT 1000
55
#define SQUARE_SIZE (WINDOW_WIDTH / 8)
6+
#define NB_MATCH 50
67

78
SDL_Color BEIGE = {238, 238, 210, 255}; // clair
89
SDL_Color DARK_BEIGE = {208, 185, 170, 255}; // foncé
@@ -427,6 +428,15 @@ static bool handle_SDL_events(int color_ai, int *clicked_square, GameEnvironemen
427428
return true;
428429
}
429430

431+
static int load_positions(OpeningBook *book)
432+
{
433+
srand(time(NULL));
434+
435+
if (load_opening_book("assets/starting_fen/starting_pos.txt", book) != 0)
436+
return 1;
437+
return 0;
438+
}
439+
430440
static int get_and_play_best_move(int color, GameEnvironement env)
431441
{
432442
char best_move[MOVE_SIZE];
@@ -554,7 +564,7 @@ int ui_main_loop(char *startpos, int color_ai)
554564
}
555565

556566
// --- Fonction pour les matchs entre bots ---
557-
int ui_bot_match(char *bot1_path, char *bot2_path, int nb_matches)
567+
int ui_bot_match(char *bot1_path, char *bot2_path)
558568
{
559569
SDL_Window *window = NULL;
560570
SDL_Renderer *renderer = NULL;
@@ -563,10 +573,13 @@ int ui_bot_match(char *bot1_path, char *bot2_path, int nb_matches)
563573

564574
load_textures(renderer);
565575
GameEnvironement env;
576+
OpeningBook book;
577+
load_positions(&book);
566578

567-
for (int match = 0; match < nb_matches; match++)
579+
for (int match = 0; match < NB_MATCH / 2; match++)
568580
{
569-
env = init_game_environment(start_pos, renderer);
581+
char *startpos = get_random_opening(&book);
582+
env = init_game_environment(startpos, renderer);
570583
bot_game_loop(env);
571584
cleanup_env(env);
572585
}

0 commit comments

Comments
 (0)