This commit is contained in:
Mylloon 2023-02-16 20:46:54 +01:00
parent 89dce59af6
commit e8ad0d3364
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 235 additions and 156 deletions

33
TP2/mk_stats.sh Normal file → Executable file
View file

@ -30,18 +30,23 @@ fi
pike run_many_games.pike -f ${PRG_1} -s ${PRG_2} -o ${GAME_DIR} -n ${NB_GAMES_PER_SIDE} -l ${NBL} -c ${NBC} 2> ${GAME_DIR}/log1.txt 1>&2 pike run_many_games.pike -f ${PRG_1} -s ${PRG_2} -o ${GAME_DIR} -n ${NB_GAMES_PER_SIDE} -l ${NBL} -c ${NBC} 2> ${GAME_DIR}/log1.txt 1>&2
pike run_many_games.pike -f ${PRG_2} -s ${PRG_1} -o ${GAME_DIR} -n ${NB_GAMES_PER_SIDE} -l ${NBL} -c ${NBC} 2> ${GAME_DIR}/log2.txt 1>&2 pike run_many_games.pike -f ${PRG_2} -s ${PRG_1} -o ${GAME_DIR} -n ${NB_GAMES_PER_SIDE} -l ${NBL} -c ${NBC} 2> ${GAME_DIR}/log2.txt 1>&2
echo " ================" > ${GAME_DIR}/resume.txt {
echo " NB_GAMES_PER_SIDE ${NB_GAMES_PER_SIDE}" >> ${GAME_DIR}/resume.txt echo " ================"
echo " NBL ${NBL}" >> ${GAME_DIR}/resume.txt echo " NB_GAMES_PER_SIDE ${NB_GAMES_PER_SIDE}"
echo " NBC ${NBC}" >> ${GAME_DIR}/resume.txt echo " NBL ${NBL}"
echo "" >> ${GAME_DIR}/resume.txt echo " NBC ${NBC}"
echo " number of wins" >> ${GAME_DIR}/resume.txt echo ""
echo " ================" >> ${GAME_DIR}/resume.txt echo " number of wins"
STR_WIN1=$(echo ${PRG_1} win) echo " ================"
NB_WIN1=$(grep "${STR_WIN1}" ${GAME_DIR}/scores.txt | wc -l) } > ${GAME_DIR}/resume.txt
echo " "${PRG_1}" is "${NB_WIN1} >> ${GAME_DIR}/resume.txt
STR_WIN2=$(echo ${PRG_2} win) STR_WIN1=$(${PRG_1} win)
NB_WIN2=$(grep "${STR_WIN2}" ${GAME_DIR}/scores.txt | wc -l) NB_WIN1=$(grep -c "${STR_WIN1}" ${GAME_DIR}/scores.txt)
echo " "${PRG_2}" is "${NB_WIN2} >> ${GAME_DIR}/resume.txt echo " ${PRG_1} is ${NB_WIN1}" >> ${GAME_DIR}/resume.txt
echo " ================" >> ${GAME_DIR}/resume.txt
STR_WIN2=$(${PRG_2} win)
NB_WIN2=$(grep -c "${STR_WIN2}" ${GAME_DIR}/scores.txt)
{
echo " ${PRG_2} is ${NB_WIN2}"
echo " ================"
} >> ${GAME_DIR}/resume.txt

View file

@ -9,25 +9,31 @@
char *cboard = (char *)"o@."; char *cboard = (char *)"o@.";
struct bt_piece_t { struct bt_piece_t {
int line; int col; int line;
int col;
}; };
struct bt_move_t { struct bt_move_t {
int line_i; int col_i; int line_i;
int line_f; int col_f; int col_i;
int line_f;
int col_f;
// all moves are printed without ambiguity // all moves are printed without ambiguity
// white in its color // white in its color
// black in red color // black in red color
void print(FILE *_fp, bool _white, int _nbl) { void print(FILE *_fp, bool _white, int _nbl) {
if (_white) { if (_white) {
fprintf(_fp, "%d%c%d%c", _nbl-line_i, 'a'+col_i, _nbl-line_f, 'a'+col_f); fprintf(_fp, "%d%c%d%c", _nbl - line_i, 'a' + col_i, _nbl - line_f,
'a' + col_f);
} else { } else {
fprintf(_fp, "\x1B[31m%d%c%d%c\x1B[0m", _nbl-line_i, 'a'+col_i, _nbl-line_f, 'a'+col_f); 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) { std::string tostr(int _nbl) {
char ret[16]; char ret[16];
snprintf(ret, sizeof(ret), "%d%c%d%c", _nbl-line_i, 'a'+col_i, _nbl-line_f, 'a'+col_f); snprintf(ret, sizeof(ret), "%d%c%d%c", _nbl - line_i, 'a' + col_i,
_nbl - line_f, 'a' + col_f);
return std::string(ret); return std::string(ret);
} }
}; };
@ -91,8 +97,10 @@ struct bt_t {
bt_move_t nrpa(double _sec); bt_move_t nrpa(double _sec);
void add_move(int _li, int _ci, int _lf, int _cf) { 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_i = _li;
moves[nb_moves].line_f = _lf; moves[nb_moves].col_f = _cf; moves[nb_moves].col_i = _ci;
moves[nb_moves].line_f = _lf;
moves[nb_moves].col_f = _cf;
nb_moves++; nb_moves++;
} }
}; };
@ -102,7 +110,8 @@ void bt_t::init(int _nbl, int _nbc) {
fprintf(stderr, "ERROR : MAX_LINES or MAX_COLS exceeded\n"); fprintf(stderr, "ERROR : MAX_LINES or MAX_COLS exceeded\n");
exit(0); exit(0);
} }
nbl = _nbl; nbc = _nbc; nbl = _nbl;
nbc = _nbc;
turn = 0; turn = 0;
turn_of_last_moves_update = -1; turn_of_last_moves_update = -1;
for (int i = 0; i < nbl; i++) for (int i = 0; i < nbl; i++)
@ -161,63 +170,88 @@ void bt_t::print_turn_and_moves(FILE* _fp = stderr) {
fprintf(_fp, "\n"); fprintf(_fp, "\n");
} }
void bt_t::update_moves() { void bt_t::update_moves() {
if(turn%2 == 0) update_moves(WHITE); if (turn % 2 == 0)
else update_moves(BLACK); update_moves(WHITE);
else
update_moves(BLACK);
} }
void bt_t::update_moves(int _color) { void bt_t::update_moves(int _color) {
if(turn_of_last_moves_update == turn) return; // MAJ ever done if (turn_of_last_moves_update == turn)
return; // MAJ ever done
turn_of_last_moves_update = turn; turn_of_last_moves_update = turn;
nb_moves = 0; nb_moves = 0;
if (_color == WHITE) { if (_color == WHITE) {
for (int i = 0; i < nb_white_pieces; i++) { for (int i = 0; i < nb_white_pieces; i++) {
int li = white_pieces[i].line; int li = white_pieces[i].line;
int ci = white_pieces[i].col; int ci = white_pieces[i].col;
if(white_can_move_right(li, ci)) add_move(li, ci, li-1, ci+1); if (white_can_move_right(li, ci))
if(white_can_move_forward(li, ci)) add_move(li, ci, li-1, ci); add_move(li, ci, li - 1, ci + 1);
if(white_can_move_left(li, ci)) add_move(li, ci, li-1, ci-1); if (white_can_move_forward(li, ci))
add_move(li, ci, li - 1, ci);
if (white_can_move_left(li, ci))
add_move(li, ci, li - 1, ci - 1);
} }
} else if (_color == BLACK) { } else if (_color == BLACK) {
for (int i = 0; i < nb_black_pieces; i++) { for (int i = 0; i < nb_black_pieces; i++) {
int li = black_pieces[i].line; int li = black_pieces[i].line;
int ci = black_pieces[i].col; int ci = black_pieces[i].col;
if(black_can_move_right(li, ci)) add_move(li, ci, li+1, ci+1); if (black_can_move_right(li, ci))
if(black_can_move_forward(li, ci)) add_move(li, ci, li+1, ci); add_move(li, ci, li + 1, ci + 1);
if(black_can_move_left(li, ci)) add_move(li, ci, li+1, ci-1); if (black_can_move_forward(li, ci))
add_move(li, ci, li + 1, ci);
if (black_can_move_left(li, ci))
add_move(li, ci, li + 1, ci - 1);
} }
} }
} }
bool bt_t::white_can_move_right(int _line, int _col) { bool bt_t::white_can_move_right(int _line, int _col) {
if(_line == 0) return false; if (_line == 0)
if(_col == nbc-1) return false; return false;
if(board[_line-1][_col+1] != WHITE) return true; if (_col == nbc - 1)
return false;
if (board[_line - 1][_col + 1] != WHITE)
return true;
return false; return false;
} }
bool bt_t::white_can_move_forward(int _line, int _col) { bool bt_t::white_can_move_forward(int _line, int _col) {
if(_line == 0) return false; if (_line == 0)
if(board[_line-1][_col] == EMPTY) return true; return false;
if (board[_line - 1][_col] == EMPTY)
return true;
return false; return false;
} }
bool bt_t::white_can_move_left(int _line, int _col) { bool bt_t::white_can_move_left(int _line, int _col) {
if(_line == 0) return false; if (_line == 0)
if(_col == 0) return false; return false;
if(board[_line-1][_col-1] != WHITE) return true; if (_col == 0)
return false;
if (board[_line - 1][_col - 1] != WHITE)
return true;
return false; return false;
} }
bool bt_t::black_can_move_right(int _line, int _col) { bool bt_t::black_can_move_right(int _line, int _col) {
if(_line == nbl-1) return false; if (_line == nbl - 1)
if(_col == nbc-1) return false; return false;
if(board[_line+1][_col+1] != BLACK) return true; if (_col == nbc - 1)
return false;
if (board[_line + 1][_col + 1] != BLACK)
return true;
return false; return false;
} }
bool bt_t::black_can_move_forward(int _line, int _col) { bool bt_t::black_can_move_forward(int _line, int _col) {
if(_line == nbl-1) return false; if (_line == nbl - 1)
if(board[_line+1][_col] == EMPTY) return true; return false;
if (board[_line + 1][_col] == EMPTY)
return true;
return false; return false;
} }
bool bt_t::black_can_move_left(int _line, int _col) { bool bt_t::black_can_move_left(int _line, int _col) {
if(_line == nbl-1) return false; if (_line == nbl - 1)
if(_col == 0) return false; return false;
if(board[_line+1][_col-1] != BLACK) return true; if (_col == 0)
return false;
if (board[_line + 1][_col - 1] != BLACK)
return true;
return false; return false;
} }
bt_move_t bt_t::get_rand_move() { bt_move_t bt_t::get_rand_move() {
@ -227,20 +261,31 @@ bt_move_t bt_t::get_rand_move() {
} }
bool bt_t::can_play(bt_move_t _m) { bool bt_t::can_play(bt_move_t _m) {
int dx = abs(_m.col_f - _m.col_i); int dx = abs(_m.col_f - _m.col_i);
if(dx > 1) return false; if (dx > 1)
return false;
int dy = abs(_m.line_f - _m.line_i); int dy = abs(_m.line_f - _m.line_i);
if(dy > 1) return false; if (dy > 1)
if(_m.line_i < 0 || _m.line_i >= nbl) return false; return false;
if(_m.line_f < 0 || _m.line_f >= nbl) return false; if (_m.line_i < 0 || _m.line_i >= nbl)
if(_m.col_i < 0 || _m.col_i >= nbc) return false; return false;
if(_m.col_f < 0 || _m.col_f >= nbc) return false; if (_m.line_f < 0 || _m.line_f >= nbl)
return false;
if (_m.col_i < 0 || _m.col_i >= nbc)
return false;
if (_m.col_f < 0 || _m.col_f >= nbc)
return false;
int color_i = board[_m.line_i][_m.col_i]; int color_i = board[_m.line_i][_m.col_i];
int color_f = board[_m.line_f][_m.col_f]; int color_f = board[_m.line_f][_m.col_f];
if(color_i == EMPTY) return false; if (color_i == EMPTY)
if(color_i == color_f) return false; return false;
if(turn%2==0 && color_i == BLACK) return false; if (color_i == color_f)
if(turn%2==1 && color_i == WHITE) return false; return false;
if(_m.col_i == _m.col_f && color_f != EMPTY) return false; if (turn % 2 == 0 && color_i == BLACK)
return false;
if (turn % 2 == 1 && color_i == WHITE)
return false;
if (_m.col_i == _m.col_f && color_f != EMPTY)
return false;
return true; return true;
} }
@ -251,7 +296,8 @@ void bt_t::play(bt_move_t _m) {
board[_m.line_i][_m.col_i] = EMPTY; board[_m.line_i][_m.col_i] = EMPTY;
if (color_i == WHITE) { if (color_i == WHITE) {
for (int i = 0; i < nb_white_pieces; i++) { for (int i = 0; i < nb_white_pieces; i++) {
if(white_pieces[i].line == _m.line_i && white_pieces[i].col == _m.col_i) { if (white_pieces[i].line == _m.line_i &&
white_pieces[i].col == _m.col_i) {
white_pieces[i].line = _m.line_f; white_pieces[i].line = _m.line_f;
white_pieces[i].col = _m.col_f; white_pieces[i].col = _m.col_f;
break; break;
@ -259,7 +305,8 @@ void bt_t::play(bt_move_t _m) {
} }
if (color_f == BLACK) { if (color_f == BLACK) {
for (int i = 0; i < nb_black_pieces; i++) { for (int i = 0; i < nb_black_pieces; i++) {
if(black_pieces[i].line == _m.line_f && black_pieces[i].col == _m.col_f) { if (black_pieces[i].line == _m.line_f &&
black_pieces[i].col == _m.col_f) {
black_pieces[i] = black_pieces[nb_black_pieces - 1]; black_pieces[i] = black_pieces[nb_black_pieces - 1];
nb_black_pieces--; nb_black_pieces--;
break; break;
@ -290,17 +337,21 @@ void bt_t::play(bt_move_t _m) {
} }
int bt_t::endgame() { int bt_t::endgame() {
for (int i = 0; i < nbc; i++) { for (int i = 0; i < nbc; i++) {
if(board[0][i] == WHITE) return WHITE; if (board[0][i] == WHITE)
return WHITE;
} }
for (int i = 0; i < nbc; i++) { for (int i = 0; i < nbc; i++) {
if(board[nbl-1][i] == BLACK) return BLACK; if (board[nbl - 1][i] == BLACK)
return BLACK;
} }
return EMPTY; return EMPTY;
} }
double bt_t::score(int _color) { double bt_t::score(int _color) {
int state = endgame(); int state = endgame();
if(state == EMPTY) return 0.0; if (state == EMPTY)
if(_color == state) return 1.0; return 0.0;
if (_color == state)
return 1.0;
return -1.0; return -1.0;
} }

View file

@ -1,9 +1,9 @@
#include "mybt.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <string.h>
#include <iostream> #include <iostream>
#include <string.h>
#include <string> #include <string>
#include "mybt.h"
bt_t B; bt_t B;
int boardwidth = 0; int boardwidth = 0;
@ -26,18 +26,18 @@ void help() {
fprintf(stderr, " play <L0C0L1C1>\n"); fprintf(stderr, " play <L0C0L1C1>\n");
fprintf(stderr, " showboard\n"); fprintf(stderr, " showboard\n");
} }
void name() { void name() { printf("= rand_player\n\n"); }
printf("= rand_player\n\n");
}
void newgame() { void newgame() {
if((boardheight < 1 || boardheight > 10) && (boardwidth < 1 || boardwidth > 10)) { if ((boardheight < 1 || boardheight > 10) &&
(boardwidth < 1 || boardwidth > 10)) {
fprintf(stderr, "boardsize is %d %d ???\n", boardheight, boardwidth); fprintf(stderr, "boardsize is %d %d ???\n", boardheight, boardwidth);
printf("= \n\n"); printf("= \n\n");
return; return;
} }
B.init(boardheight, boardwidth); B.init(boardheight, boardwidth);
white_turn = true; white_turn = true;
if(verbose) fprintf(stderr, "ready to play on %dx%d board\n", boardheight, boardwidth); if (verbose)
fprintf(stderr, "ready to play on %dx%d board\n", boardheight, boardwidth);
printf("= \n\n"); printf("= \n\n");
} }
void showboard() { void showboard() {
@ -48,8 +48,10 @@ void genmove() {
int ret = B.endgame(); int ret = B.endgame();
if (ret != EMPTY) { if (ret != EMPTY) {
fprintf(stderr, "game finished\n"); fprintf(stderr, "game finished\n");
if(ret == WHITE) fprintf(stderr, "white player wins\n"); if (ret == WHITE)
else fprintf(stderr, "black player wins\n"); fprintf(stderr, "white player wins\n");
else
fprintf(stderr, "black player wins\n");
printf("= \n\n"); printf("= \n\n");
return; return;
} }
@ -76,34 +78,55 @@ void play(char a, char b, char c, char d) {
} }
white_turn = !white_turn; white_turn = !white_turn;
} else { } else {
fprintf(stderr, "CANT play %d %d %d %d ?\n", m.line_i, m.col_i, m.line_f, m.col_f); fprintf(stderr, "CANT play %d %d %d %d ?\n", m.line_i, m.col_i, m.line_f,
m.col_f);
} }
if(showboard_at_each_move) showboard(); if (showboard_at_each_move)
showboard();
printf("= \n\n"); printf("= \n\n");
} }
int main(int _ac, char **_av) { int main(int _ac, char **_av) {
bool echo_on = false; bool echo_on = false;
setbuf(stdout, 0); setbuf(stdout, 0);
setbuf(stderr, 0); setbuf(stderr, 0);
if(verbose) fprintf(stderr, "rand_player started\n"); if (verbose)
fprintf(stderr, "rand_player started\n");
char a, b, c, d; // for play cmd char a, b, c, d; // for play cmd
for (std::string line; std::getline(std::cin, line);) { for (std::string line; std::getline(std::cin, line);) {
if(verbose) fprintf(stderr, "rand_player receive %s\n", line.c_str()); if (verbose)
if(echo_on) if(verbose) fprintf(stderr, "%s\n", line.c_str()); fprintf(stderr, "rand_player receive %s\n", line.c_str());
if(line.compare("quit") == 0) { printf("= \n\n"); break; } if (echo_on)
else if(line.compare("echo ON") == 0) echo_on = true; if (verbose)
else if(line.compare("echo OFF") == 0) echo_on = false; fprintf(stderr, "%s\n", line.c_str());
else if(line.compare("help") == 0) help(); if (line.compare("quit") == 0) {
else if(line.compare("name") == 0) name(); printf("= \n\n");
else if(sscanf(line.c_str(), "newgame %d %d\n", &boardheight, &boardwidth) == 2) newgame(); break;
else if(line.compare("genmove") == 0) genmove(); } else if (line.compare("echo ON") == 0)
else if(sscanf(line.c_str(), "play %c%c%c%c\n", &a,&b,&c,&d) == 4) play(a,b,c,d); echo_on = true;
else if(line.compare("showboard") == 0) showboard(); else if (line.compare("echo OFF") == 0)
else if(line.compare(0,2,"//") == 0) ; // just comments echo_on = false;
else fprintf(stderr, "???\n"); else if (line.compare("help") == 0)
if(echo_on) printf(">"); help();
else if (line.compare("name") == 0)
name();
else if (sscanf(line.c_str(), "newgame %d %d\n", &boardheight,
&boardwidth) == 2)
newgame();
else if (line.compare("genmove") == 0)
genmove();
else if (sscanf(line.c_str(), "play %c%c%c%c\n", &a, &b, &c, &d) == 4)
play(a, b, c, d);
else if (line.compare("showboard") == 0)
showboard();
else if (line.compare(0, 2, "//") == 0)
; // just comments
else
fprintf(stderr, "???\n");
if (echo_on)
printf(">");
} }
if(verbose) fprintf(stderr, "bye.\n"); if (verbose)
fprintf(stderr, "bye.\n");
return 0; return 0;
} }