use Makefile of TP1, reorganization, cleanup includes

This commit is contained in:
Mylloon 2023-03-10 13:47:28 +01:00
parent 46f426dfbe
commit 20499fbb48
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 134 additions and 121 deletions

View file

@ -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)

108
TP2/includes/mybt.h Normal file
View file

@ -0,0 +1,108 @@
#ifndef MYBT_H
#define MYBT_H
#include <random>
#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 */

View file

@ -1,114 +1,7 @@
#ifndef MYBT_H
#define MYBT_H
#include <cstdio>
#include <cstdlib>
#include <random>
#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 */

View file

@ -1,10 +1,6 @@
#include "mybt.h"
#include "../includes/mybt.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <string>
bt_t B;
int boardwidth = 0;