format
This commit is contained in:
parent
89dce59af6
commit
e8ad0d3364
3 changed files with 235 additions and 156 deletions
33
TP2/mk_stats.sh
Normal file → Executable file
33
TP2/mk_stats.sh
Normal file → Executable 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_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
|
||||
|
|
153
TP2/mybt.h
153
TP2/mybt.h
|
@ -9,25 +9,31 @@
|
|||
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);
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
@ -91,8 +97,10 @@ 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++;
|
||||
}
|
||||
};
|
||||
|
@ -102,7 +110,8 @@ void bt_t::init(int _nbl, int _nbc) {
|
|||
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++)
|
||||
|
@ -161,63 +170,88 @@ void bt_t::print_turn_and_moves(FILE* _fp = stderr) {
|
|||
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++) {
|
||||
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++) {
|
||||
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() {
|
||||
|
@ -227,20 +261,31 @@ bt_move_t bt_t::get_rand_move() {
|
|||
}
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -251,7 +296,8 @@ void bt_t::play(bt_move_t _m) {
|
|||
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 (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;
|
||||
|
@ -259,7 +305,8 @@ void bt_t::play(bt_move_t _m) {
|
|||
}
|
||||
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) {
|
||||
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;
|
||||
|
@ -290,17 +337,21 @@ void bt_t::play(bt_move_t _m) {
|
|||
}
|
||||
int bt_t::endgame() {
|
||||
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++) {
|
||||
if(board[nbl-1][i] == BLACK) return BLACK;
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
@ -48,8 +48,10 @@ void genmove() {
|
|||
int ret = B.endgame();
|
||||
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;
|
||||
}
|
||||
|
@ -76,34 +78,55 @@ void play(char a, char b, char c, char d) {
|
|||
}
|
||||
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) {
|
||||
bool echo_on = false;
|
||||
setbuf(stdout, 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
|
||||
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;
|
||||
}
|
||||
|
|
Reference in a new issue