From 20499fbb488f7743608b9bcde72af2bf05774d30 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 10 Mar 2023 13:47:28 +0100 Subject: [PATCH] use Makefile of TP1, reorganization, cleanup includes --- TP2/Makefile | 30 +++++++-- TP2/includes/mybt.h | 108 +++++++++++++++++++++++++++++++++ TP2/{mybt.h => src/mybt.cpp} | 111 +--------------------------------- TP2/{ => src}/rand_player.cpp | 6 +- 4 files changed, 134 insertions(+), 121 deletions(-) create mode 100644 TP2/includes/mybt.h rename TP2/{mybt.h => src/mybt.cpp} (72%) rename TP2/{ => src}/rand_player.cpp (97%) diff --git a/TP2/Makefile b/TP2/Makefile index 9f74ae0..d7fd387 100644 --- a/TP2/Makefile +++ b/TP2/Makefile @@ -1,9 +1,27 @@ -CC=g++ -CFLAGS=-std=c++11 -Wall -O2 +CXX = g++ +CXXFLAGS = -std=c++11 +RM = rm -##### BREAKTHROUGH -rand_player: mybt.h rand_player.cpp - $(CC) $(CFLAGS) rand_player.cpp -o $@ +SOURCES = $(wildcard src/*.cpp) +OBJETS = $(patsubst %.cpp,%.cpp.o,$(notdir $(SOURCES))) + +EXE = rand_player + +%.cpp.o: src/%.cpp + $(CXX) -c -o $@ $< $(CXXFLAGS) $(DEVFLAGS) + +rand_player: CXXFLAGS += -Wall -O2 +rand_player: compilation + +dev: CXXFLAGS += -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Og -g +dev: CXXFLAGS += -Wold-style-cast -Wsign-conversion +dev: compilation + +compilation: $(OBJETS) + $(CXX) -o $(EXE) $(OBJETS) + +all: + rand_player clean: - rm rand_player + $(RM) $(OBJETS) $(EXE) diff --git a/TP2/includes/mybt.h b/TP2/includes/mybt.h new file mode 100644 index 0000000..973779e --- /dev/null +++ b/TP2/includes/mybt.h @@ -0,0 +1,108 @@ +#ifndef MYBT_H +#define MYBT_H + +#include + +#define WHITE 0 +#define BLACK 1 +#define EMPTY 2 + +struct bt_piece_t { + int line; + int col; +}; + +struct bt_move_t { + int line_i; + int col_i; + int line_f; + int col_f; + + // all moves are printed without ambiguity + // white in its color + // black in red color + void print(FILE *_fp, bool _white, int _nbl) { + if (_white) { + fprintf(_fp, "%d%c%d%c", _nbl - line_i, 'a' + col_i, _nbl - line_f, + 'a' + col_f); + } else { + fprintf(_fp, "\x1B[31m%d%c%d%c\x1B[0m", _nbl - line_i, 'a' + col_i, + _nbl - line_f, 'a' + col_f); + } + } + std::string tostr(int _nbl) { + char ret[16]; + snprintf(ret, sizeof(ret), "%d%c%d%c", _nbl - line_i, 'a' + col_i, + _nbl - line_f, 'a' + col_f); + return std::string(ret); + } +}; + +// alloc default 10x10 +// standard game in 8x8 +#define MAX_LINES 10 +#define MAX_COLS 10 + +// rules reminder : +// pieces moves from 1 square in diag and in front +// pieces captures only in diag +// i.e. to go forward, square must be empty +struct bt_t { + int nbl; + int nbc; + int board[MAX_LINES][MAX_COLS]; + int turn; + + bt_piece_t white_pieces[2 * MAX_LINES]; + int nb_white_pieces; + bt_piece_t black_pieces[2 * MAX_LINES]; + int nb_black_pieces; + bt_move_t moves[3 * 2 * MAX_LINES]; + int nb_moves; + + // last turn of moves update + int turn_of_last_moves_update; + + void init(int _nbl, int _nbc); + void init_pieces(); + void print_board(FILE *_fp); + void print_turn_and_moves(FILE *_fp); + void update_moves(); + void update_moves(int _color); + + bool white_can_move_right(int _line, int _col); + bool white_can_move_forward(int _line, int _col); + bool white_can_move_left(int _line, int _col); + bool black_can_move_right(int _line, int _col); + bool black_can_move_forward(int _line, int _col); + bool black_can_move_left(int _line, int _col); + + bt_move_t get_rand_move(); + bool can_play(bt_move_t _m); + void play(bt_move_t _m); + int endgame(); + double score(int _color); + void playout(bool _log); + std::string mkH1(); + std::string mkH2(); + long long int mkH3(); + + // déclarées mais non définies + double eval(); + bt_move_t minimax(double _sec); + bt_move_t alphabeta(double _sec); + bt_move_t mcts(double _sec); + bt_move_t mcts_ppa(double _sec); + bt_move_t nmcs(double _sec); + bt_move_t nrpa(double _sec); + + void add_move(int _li, int _ci, int _lf, int _cf) { + moves[nb_moves].line_i = _li; + moves[nb_moves].col_i = _ci; + moves[nb_moves].line_f = _lf; + moves[nb_moves].col_f = _cf; + nb_moves++; + } +}; + +#endif /* MYBT_H */ diff --git a/TP2/mybt.h b/TP2/src/mybt.cpp similarity index 72% rename from TP2/mybt.h rename to TP2/src/mybt.cpp index aedfb27..bfa8741 100644 --- a/TP2/mybt.h +++ b/TP2/src/mybt.cpp @@ -1,114 +1,7 @@ -#ifndef MYBT_H -#define MYBT_H - -#include -#include -#include - -#define WHITE 0 -#define BLACK 1 -#define EMPTY 2 +#include "../includes/mybt.h" char *cboard = (char *)"o@."; -struct bt_piece_t { - int line; - int col; -}; - -struct bt_move_t { - int line_i; - int col_i; - int line_f; - int col_f; - - // all moves are printed without ambiguity - // white in its color - // black in red color - void print(FILE *_fp, bool _white, int _nbl) { - if (_white) { - fprintf(_fp, "%d%c%d%c", _nbl - line_i, 'a' + col_i, _nbl - line_f, - 'a' + col_f); - } else { - fprintf(_fp, "\x1B[31m%d%c%d%c\x1B[0m", _nbl - line_i, 'a' + col_i, - _nbl - line_f, 'a' + col_f); - } - } - std::string tostr(int _nbl) { - char ret[16]; - snprintf(ret, sizeof(ret), "%d%c%d%c", _nbl - line_i, 'a' + col_i, - _nbl - line_f, 'a' + col_f); - return std::string(ret); - } -}; - -// alloc default 10x10 -// standard game in 8x8 -#define MAX_LINES 10 -#define MAX_COLS 10 - -// rules reminder : -// pieces moves from 1 square in diag and in front -// pieces captures only in diag -// i.e. to go forward, square must be empty -struct bt_t { - int nbl; - int nbc; - int board[MAX_LINES][MAX_COLS]; - int turn; - - bt_piece_t white_pieces[2 * MAX_LINES]; - int nb_white_pieces; - bt_piece_t black_pieces[2 * MAX_LINES]; - int nb_black_pieces; - bt_move_t moves[3 * 2 * MAX_LINES]; - int nb_moves; - - // last turn of moves update - int turn_of_last_moves_update; - - void init(int _nbl, int _nbc); - void init_pieces(); - void print_board(FILE *_fp); - void print_turn_and_moves(FILE *_fp); - void update_moves(); - void update_moves(int _color); - - bool white_can_move_right(int _line, int _col); - bool white_can_move_forward(int _line, int _col); - bool white_can_move_left(int _line, int _col); - bool black_can_move_right(int _line, int _col); - bool black_can_move_forward(int _line, int _col); - bool black_can_move_left(int _line, int _col); - - bt_move_t get_rand_move(); - bool can_play(bt_move_t _m); - void play(bt_move_t _m); - int endgame(); - double score(int _color); - void playout(bool _log); - std::string mkH1(); - std::string mkH2(); - long long int mkH3(); - - // déclarées mais non définies - double eval(); - bt_move_t minimax(double _sec); - bt_move_t alphabeta(double _sec); - bt_move_t mcts(double _sec); - bt_move_t mcts_ppa(double _sec); - bt_move_t nmcs(double _sec); - bt_move_t nrpa(double _sec); - - void add_move(int _li, int _ci, int _lf, int _cf) { - moves[nb_moves].line_i = _li; - moves[nb_moves].col_i = _ci; - moves[nb_moves].line_f = _lf; - moves[nb_moves].col_f = _cf; - nb_moves++; - } -}; - void bt_t::init(int _nbl, int _nbc) { if (_nbl > MAX_LINES || _nbc > MAX_COLS) { fprintf(stderr, "ERROR : MAX_LINES or MAX_COLS exceeded\n"); @@ -413,5 +306,3 @@ double bt_t::score(int _color) { } return -1.0; } - -#endif /* MYBT_H */ diff --git a/TP2/rand_player.cpp b/TP2/src/rand_player.cpp similarity index 97% rename from TP2/rand_player.cpp rename to TP2/src/rand_player.cpp index 930d974..4e5c980 100644 --- a/TP2/rand_player.cpp +++ b/TP2/src/rand_player.cpp @@ -1,10 +1,6 @@ -#include "mybt.h" +#include "../includes/mybt.h" -#include -#include #include -#include -#include bt_t B; int boardwidth = 0;