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

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

@ -27,21 +27,26 @@ then
exit 0
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_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_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
echo " ================" > ${GAME_DIR}/resume.txt
echo " NB_GAMES_PER_SIDE ${NB_GAMES_PER_SIDE}" >> ${GAME_DIR}/resume.txt
echo " NBL ${NBL}" >> ${GAME_DIR}/resume.txt
echo " NBC ${NBC}" >> ${GAME_DIR}/resume.txt
echo "" >> ${GAME_DIR}/resume.txt
echo " number of wins" >> ${GAME_DIR}/resume.txt
echo " ================" >> ${GAME_DIR}/resume.txt
STR_WIN1=$(echo ${PRG_1} win)
NB_WIN1=$(grep "${STR_WIN1}" ${GAME_DIR}/scores.txt | wc -l)
echo " "${PRG_1}" is "${NB_WIN1} >> ${GAME_DIR}/resume.txt
{
echo " ================"
echo " NB_GAMES_PER_SIDE ${NB_GAMES_PER_SIDE}"
echo " NBL ${NBL}"
echo " NBC ${NBC}"
echo ""
echo " number of wins"
echo " ================"
} > ${GAME_DIR}/resume.txt
STR_WIN2=$(echo ${PRG_2} win)
NB_WIN2=$(grep "${STR_WIN2}" ${GAME_DIR}/scores.txt | wc -l)
echo " "${PRG_2}" is "${NB_WIN2} >> ${GAME_DIR}/resume.txt
echo " ================" >> ${GAME_DIR}/resume.txt
STR_WIN1=$(${PRG_1} win)
NB_WIN1=$(grep -c "${STR_WIN1}" ${GAME_DIR}/scores.txt)
echo " ${PRG_1} is ${NB_WIN1}" >> ${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

@ -6,28 +6,34 @@
#define WHITE 0
#define BLACK 1
#define EMPTY 2
char* cboard = (char*)"o@.";
char *cboard = (char *)"o@.";
struct bt_piece_t {
int line; int col;
int line;
int col;
};
struct bt_move_t {
int line_i; int col_i;
int line_f; int col_f;
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);
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);
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);
snprintf(ret, sizeof(ret), "%d%c%d%c", _nbl - line_i, 'a' + col_i,
_nbl - line_f, 'a' + col_f);
return std::string(ret);
}
};
@ -48,19 +54,19 @@ struct bt_t {
int board[MAX_LINES][MAX_COLS];
int turn;
bt_piece_t white_pieces[2*MAX_LINES];
bt_piece_t white_pieces[2 * MAX_LINES];
int nb_white_pieces;
bt_piece_t black_pieces[2*MAX_LINES];
bt_piece_t black_pieces[2 * MAX_LINES];
int nb_black_pieces;
bt_move_t moves[3*2*MAX_LINES];
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 print_board(FILE *_fp);
void print_turn_and_moves(FILE *_fp);
void update_moves();
void update_moves(int _color);
@ -91,25 +97,28 @@ struct bt_t {
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;
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) {
if (_nbl > MAX_LINES || _nbc > MAX_COLS) {
fprintf(stderr, "ERROR : MAX_LINES or MAX_COLS exceeded\n");
exit(0);
}
nbl = _nbl; nbc = _nbc;
nbl = _nbl;
nbc = _nbc;
turn = 0;
turn_of_last_moves_update = -1;
for(int i = 0; i < nbl; i++)
for(int j = 0; j < nbc; j++) {
if(i <= 1 ) {
for (int i = 0; i < nbl; i++)
for (int j = 0; j < nbc; j++) {
if (i <= 1) {
board[i][j] = BLACK;
} else if(i < _nbl-2) {
} else if (i < _nbl - 2) {
board[i][j] = EMPTY;
} else {
board[i][j] = WHITE;
@ -121,13 +130,13 @@ void bt_t::init(int _nbl, int _nbc) {
void bt_t::init_pieces() {
nb_white_pieces = 0;
nb_black_pieces = 0;
for(int i = 0; i < nbl; i++)
for(int j = 0; j < nbc; j++) {
if(board[i][j] == WHITE) {
for (int i = 0; i < nbl; i++)
for (int j = 0; j < nbc; j++) {
if (board[i][j] == WHITE) {
white_pieces[nb_white_pieces].line = i;
white_pieces[nb_white_pieces].col = j;
nb_white_pieces++;
} else if(board[i][j] == BLACK) {
} else if (board[i][j] == BLACK) {
black_pieces[nb_black_pieces].line = i;
black_pieces[nb_black_pieces].col = j;
nb_black_pieces++;
@ -135,16 +144,16 @@ void bt_t::init_pieces() {
}
}
// again print black in red (as bg is black... black is printed in red)
void bt_t::print_board(FILE* _fp = stderr) {
void bt_t::print_board(FILE *_fp = stderr) {
fprintf(_fp, " \x1B[34m");
for(int j = 0; j < nbc; j++) {
fprintf(_fp, "%c ", 'a'+j);
for (int j = 0; j < nbc; j++) {
fprintf(_fp, "%c ", 'a' + j);
}
fprintf(_fp, "\x1B[0m\n");
for(int i = 0; i < nbl; i++) {
fprintf(_fp, "\x1B[34m%2d\x1B[0m ", (nbl-i));
for(int j = 0; j < nbc; j++) {
if(board[i][j] == BLACK)
for (int i = 0; i < nbl; i++) {
fprintf(_fp, "\x1B[34m%2d\x1B[0m ", (nbl - i));
for (int j = 0; j < nbc; j++) {
if (board[i][j] == BLACK)
fprintf(_fp, "\x1B[31m%c\x1B[0m ", cboard[board[i][j]]);
else
fprintf(_fp, "%c ", cboard[board[i][j]]);
@ -152,95 +161,131 @@ void bt_t::print_board(FILE* _fp = stderr) {
fprintf(_fp, "\n");
}
}
void bt_t::print_turn_and_moves(FILE* _fp = stderr) {
fprintf(_fp,"turn:%d\nmoves:", turn);
for(int i = 0; i < nb_moves; i++) {
moves[i].print(_fp, turn%2 == 1, nbl);
void bt_t::print_turn_and_moves(FILE *_fp = stderr) {
fprintf(_fp, "turn:%d\nmoves:", turn);
for (int i = 0; i < nb_moves; i++) {
moves[i].print(_fp, turn % 2 == 1, nbl);
fprintf(_fp, " ");
}
fprintf(_fp, "\n");
}
void bt_t::update_moves() {
if(turn%2 == 0) update_moves(WHITE);
else update_moves(BLACK);
if (turn % 2 == 0)
update_moves(WHITE);
else
update_moves(BLACK);
}
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;
nb_moves = 0;
if(_color==WHITE) {
for(int i = 0; i < nb_white_pieces; i++) {
if (_color == WHITE) {
for (int i = 0; i < nb_white_pieces; i++) {
int li = white_pieces[i].line;
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_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);
if (white_can_move_right(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) {
for(int i = 0; i < nb_black_pieces; i++) {
} else if (_color == BLACK) {
for (int i = 0; i < nb_black_pieces; i++) {
int li = black_pieces[i].line;
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_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);
if (black_can_move_right(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) {
if(_line == 0) return false;
if(_col == nbc-1) return false;
if(board[_line-1][_col+1] != WHITE) return true;
if (_line == 0)
return false;
if (_col == nbc - 1)
return false;
if (board[_line - 1][_col + 1] != WHITE)
return true;
return false;
}
bool bt_t::white_can_move_forward(int _line, int _col) {
if(_line == 0) return false;
if(board[_line-1][_col] == EMPTY) return true;
if (_line == 0)
return false;
if (board[_line - 1][_col] == EMPTY)
return true;
return false;
}
bool bt_t::white_can_move_left(int _line, int _col) {
if(_line == 0) return false;
if(_col == 0) return false;
if(board[_line-1][_col-1] != WHITE) return true;
if (_line == 0)
return false;
if (_col == 0)
return false;
if (board[_line - 1][_col - 1] != WHITE)
return true;
return false;
}
bool bt_t::black_can_move_right(int _line, int _col) {
if(_line == nbl-1) return false;
if(_col == nbc-1) return false;
if(board[_line+1][_col+1] != BLACK) return true;
if (_line == nbl - 1)
return false;
if (_col == nbc - 1)
return false;
if (board[_line + 1][_col + 1] != BLACK)
return true;
return false;
}
bool bt_t::black_can_move_forward(int _line, int _col) {
if(_line == nbl-1) return false;
if(board[_line+1][_col] == EMPTY) return true;
if (_line == nbl - 1)
return false;
if (board[_line + 1][_col] == EMPTY)
return true;
return false;
}
bool bt_t::black_can_move_left(int _line, int _col) {
if(_line == nbl-1) return false;
if(_col == 0) return false;
if(board[_line+1][_col-1] != BLACK) return true;
if (_line == nbl - 1)
return false;
if (_col == 0)
return false;
if (board[_line + 1][_col - 1] != BLACK)
return true;
return false;
}
bt_move_t bt_t::get_rand_move() {
update_moves();
int r = ((int)rand())%nb_moves;
int r = ((int)rand()) % nb_moves;
return moves[r];
}
bool bt_t::can_play(bt_move_t _m) {
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);
if(dy > 1) return false;
if(_m.line_i < 0 || _m.line_i >= nbl) 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;
if (dy > 1)
return false;
if (_m.line_i < 0 || _m.line_i >= nbl)
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_f = board[_m.line_f][_m.col_f];
if(color_i == EMPTY) return false;
if(color_i == color_f) 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;
if (color_i == EMPTY)
return false;
if (color_i == color_f)
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;
}
@ -249,37 +294,39 @@ void bt_t::play(bt_move_t _m) {
int color_f = board[_m.line_f][_m.col_f];
board[_m.line_f][_m.col_f] = color_i;
board[_m.line_i][_m.col_i] = EMPTY;
if(color_i == WHITE) {
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 (color_i == WHITE) {
for (int i = 0; i < nb_white_pieces; 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].col = _m.col_f;
break;
}
}
if(color_f == BLACK) {
for(int i = 0; i < nb_black_pieces; i++) {
if(black_pieces[i].line == _m.line_f && black_pieces[i].col == _m.col_f) {
black_pieces[i] = black_pieces[nb_black_pieces-1];
if (color_f == BLACK) {
for (int i = 0; i < nb_black_pieces; i++) {
if (black_pieces[i].line == _m.line_f &&
black_pieces[i].col == _m.col_f) {
black_pieces[i] = black_pieces[nb_black_pieces - 1];
nb_black_pieces--;
break;
}
}
}
} else if(color_i == BLACK) {
for(int i = 0; i < nb_black_pieces; i++) {
if(black_pieces[i].line == _m.line_i &&
} else if (color_i == BLACK) {
for (int i = 0; i < nb_black_pieces; i++) {
if (black_pieces[i].line == _m.line_i &&
black_pieces[i].col == _m.col_i) {
black_pieces[i].line = _m.line_f;
black_pieces[i].col = _m.col_f;
break;
}
}
if(color_f == WHITE) {
for(int i = 0; i < nb_white_pieces; i++) {
if(white_pieces[i].line == _m.line_f &&
if (color_f == WHITE) {
for (int i = 0; i < nb_white_pieces; i++) {
if (white_pieces[i].line == _m.line_f &&
white_pieces[i].col == _m.col_f) {
white_pieces[i] = white_pieces[nb_white_pieces-1];
white_pieces[i] = white_pieces[nb_white_pieces - 1];
nb_white_pieces--;
break;
}
@ -289,18 +336,22 @@ void bt_t::play(bt_move_t _m) {
turn++;
}
int bt_t::endgame() {
for(int i = 0; i < nbc; i++) {
if(board[0][i] == WHITE) return WHITE;
for (int i = 0; i < nbc; i++) {
if (board[0][i] == WHITE)
return WHITE;
}
for(int i = 0; i < nbc; i++) {
if(board[nbl-1][i] == BLACK) return BLACK;
for (int i = 0; i < nbc; i++) {
if (board[nbl - 1][i] == BLACK)
return BLACK;
}
return EMPTY;
}
double bt_t::score(int _color) {
int state = endgame();
if(state == EMPTY) return 0.0;
if(_color == state) return 1.0;
if (state == EMPTY)
return 0.0;
if (_color == state)
return 1.0;
return -1.0;
}

View file

@ -1,9 +1,9 @@
#include "mybt.h"
#include <cstdio>
#include <cstdlib>
#include <string.h>
#include <iostream>
#include <string.h>
#include <string>
#include "mybt.h"
bt_t B;
int boardwidth = 0;
@ -26,18 +26,18 @@ void help() {
fprintf(stderr, " play <L0C0L1C1>\n");
fprintf(stderr, " showboard\n");
}
void name() {
printf("= rand_player\n\n");
}
void name() { printf("= rand_player\n\n"); }
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);
printf("= \n\n");
return;
}
B.init(boardheight, boardwidth);
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");
}
void showboard() {
@ -46,16 +46,18 @@ void showboard() {
}
void genmove() {
int ret = B.endgame();
if(ret != EMPTY) {
if (ret != EMPTY) {
fprintf(stderr, "game finished\n");
if(ret == WHITE) fprintf(stderr, "white player wins\n");
else fprintf(stderr, "black player wins\n");
if (ret == WHITE)
fprintf(stderr, "white player wins\n");
else
fprintf(stderr, "black player wins\n");
printf("= \n\n");
return;
}
bt_move_t m = B.get_rand_move();
B.play(m);
if(verbose) {
if (verbose) {
m.print(stderr, white_turn, B.nbl);
fprintf(stderr, "\n");
}
@ -64,46 +66,67 @@ void genmove() {
}
void play(char a, char b, char c, char d) {
bt_move_t m;
m.line_i = boardheight-(a-'0');
m.col_i = b-'a';
m.line_f = boardheight-(c-'0');
m.col_f = d-'a';
if(B.can_play(m)) {
m.line_i = boardheight - (a - '0');
m.col_i = b - 'a';
m.line_f = boardheight - (c - '0');
m.col_f = d - 'a';
if (B.can_play(m)) {
B.play(m);
if(verbose) {
if (verbose) {
m.print(stderr, white_turn, B.nbl);
fprintf(stderr, "\n");
}
white_turn = !white_turn;
} 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");
}
int main(int _ac, char** _av) {
int main(int _ac, char **_av) {
bool echo_on = false;
setbuf(stdout, 0);
setbuf(stderr, 0);
if(verbose) fprintf(stderr, "rand_player started\n");
char a,b,c,d; // for play cmd
if (verbose)
fprintf(stderr, "rand_player started\n");
char a, b, c, d; // for play cmd
for (std::string line; std::getline(std::cin, line);) {
if(verbose) fprintf(stderr, "rand_player receive %s\n", line.c_str());
if(echo_on) if(verbose) fprintf(stderr, "%s\n", line.c_str());
if(line.compare("quit") == 0) { printf("= \n\n"); break; }
else if(line.compare("echo ON") == 0) echo_on = true;
else if(line.compare("echo OFF") == 0) echo_on = false;
else if(line.compare("help") == 0) 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, "rand_player receive %s\n", line.c_str());
if (echo_on)
if (verbose)
fprintf(stderr, "%s\n", line.c_str());
if (line.compare("quit") == 0) {
printf("= \n\n");
break;
} else if (line.compare("echo ON") == 0)
echo_on = true;
else if (line.compare("echo OFF") == 0)
echo_on = false;
else if (line.compare("help") == 0)
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;
}