format
This commit is contained in:
parent
89dce59af6
commit
e8ad0d3364
3 changed files with 235 additions and 156 deletions
37
TP2/mk_stats.sh
Normal file → Executable file
37
TP2/mk_stats.sh
Normal file → Executable file
|
@ -27,21 +27,26 @@ then
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
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
|
||||||
|
|
257
TP2/mybt.h
257
TP2/mybt.h
|
@ -6,28 +6,34 @@
|
||||||
#define WHITE 0
|
#define WHITE 0
|
||||||
#define BLACK 1
|
#define BLACK 1
|
||||||
#define EMPTY 2
|
#define EMPTY 2
|
||||||
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -48,22 +54,22 @@ struct bt_t {
|
||||||
int board[MAX_LINES][MAX_COLS];
|
int board[MAX_LINES][MAX_COLS];
|
||||||
int turn;
|
int turn;
|
||||||
|
|
||||||
bt_piece_t white_pieces[2*MAX_LINES];
|
bt_piece_t white_pieces[2 * MAX_LINES];
|
||||||
int nb_white_pieces;
|
int nb_white_pieces;
|
||||||
bt_piece_t black_pieces[2*MAX_LINES];
|
bt_piece_t black_pieces[2 * MAX_LINES];
|
||||||
int nb_black_pieces;
|
int nb_black_pieces;
|
||||||
bt_move_t moves[3*2*MAX_LINES];
|
bt_move_t moves[3 * 2 * MAX_LINES];
|
||||||
int nb_moves;
|
int nb_moves;
|
||||||
// last turn of moves update
|
// last turn of moves update
|
||||||
int turn_of_last_moves_update;
|
int turn_of_last_moves_update;
|
||||||
|
|
||||||
void init(int _nbl, int _nbc);
|
void init(int _nbl, int _nbc);
|
||||||
void init_pieces();
|
void init_pieces();
|
||||||
void print_board(FILE* _fp);
|
void print_board(FILE *_fp);
|
||||||
void print_turn_and_moves(FILE* _fp);
|
void print_turn_and_moves(FILE *_fp);
|
||||||
void update_moves();
|
void update_moves();
|
||||||
void update_moves(int _color);
|
void update_moves(int _color);
|
||||||
|
|
||||||
bool white_can_move_right(int _line, int _col);
|
bool white_can_move_right(int _line, int _col);
|
||||||
bool white_can_move_forward(int _line, int _col);
|
bool white_can_move_forward(int _line, int _col);
|
||||||
bool white_can_move_left(int _line, int _col);
|
bool white_can_move_left(int _line, int _col);
|
||||||
|
@ -80,7 +86,7 @@ struct bt_t {
|
||||||
std::string mkH1();
|
std::string mkH1();
|
||||||
std::string mkH2();
|
std::string mkH2();
|
||||||
long long int mkH3();
|
long long int mkH3();
|
||||||
|
|
||||||
// déclarées mais non définies
|
// déclarées mais non définies
|
||||||
double eval();
|
double eval();
|
||||||
bt_move_t minimax(double _sec);
|
bt_move_t minimax(double _sec);
|
||||||
|
@ -91,25 +97,28 @@ 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++;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
void bt_t::init(int _nbl, int _nbc) {
|
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");
|
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++)
|
||||||
for(int j = 0; j < nbc; j++) {
|
for (int j = 0; j < nbc; j++) {
|
||||||
if(i <= 1 ) {
|
if (i <= 1) {
|
||||||
board[i][j] = BLACK;
|
board[i][j] = BLACK;
|
||||||
} else if(i < _nbl-2) {
|
} else if (i < _nbl - 2) {
|
||||||
board[i][j] = EMPTY;
|
board[i][j] = EMPTY;
|
||||||
} else {
|
} else {
|
||||||
board[i][j] = WHITE;
|
board[i][j] = WHITE;
|
||||||
|
@ -121,13 +130,13 @@ void bt_t::init(int _nbl, int _nbc) {
|
||||||
void bt_t::init_pieces() {
|
void bt_t::init_pieces() {
|
||||||
nb_white_pieces = 0;
|
nb_white_pieces = 0;
|
||||||
nb_black_pieces = 0;
|
nb_black_pieces = 0;
|
||||||
for(int i = 0; i < nbl; i++)
|
for (int i = 0; i < nbl; i++)
|
||||||
for(int j = 0; j < nbc; j++) {
|
for (int j = 0; j < nbc; j++) {
|
||||||
if(board[i][j] == WHITE) {
|
if (board[i][j] == WHITE) {
|
||||||
white_pieces[nb_white_pieces].line = i;
|
white_pieces[nb_white_pieces].line = i;
|
||||||
white_pieces[nb_white_pieces].col = j;
|
white_pieces[nb_white_pieces].col = j;
|
||||||
nb_white_pieces++;
|
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].line = i;
|
||||||
black_pieces[nb_black_pieces].col = j;
|
black_pieces[nb_black_pieces].col = j;
|
||||||
nb_black_pieces++;
|
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)
|
// 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");
|
fprintf(_fp, " \x1B[34m");
|
||||||
for(int j = 0; j < nbc; j++) {
|
for (int j = 0; j < nbc; j++) {
|
||||||
fprintf(_fp, "%c ", 'a'+j);
|
fprintf(_fp, "%c ", 'a' + j);
|
||||||
}
|
}
|
||||||
fprintf(_fp, "\x1B[0m\n");
|
fprintf(_fp, "\x1B[0m\n");
|
||||||
for(int i = 0; i < nbl; i++) {
|
for (int i = 0; i < nbl; i++) {
|
||||||
fprintf(_fp, "\x1B[34m%2d\x1B[0m ", (nbl-i));
|
fprintf(_fp, "\x1B[34m%2d\x1B[0m ", (nbl - i));
|
||||||
for(int j = 0; j < nbc; j++) {
|
for (int j = 0; j < nbc; j++) {
|
||||||
if(board[i][j] == BLACK)
|
if (board[i][j] == BLACK)
|
||||||
fprintf(_fp, "\x1B[31m%c\x1B[0m ", cboard[board[i][j]]);
|
fprintf(_fp, "\x1B[31m%c\x1B[0m ", cboard[board[i][j]]);
|
||||||
else
|
else
|
||||||
fprintf(_fp, "%c ", cboard[board[i][j]]);
|
fprintf(_fp, "%c ", cboard[board[i][j]]);
|
||||||
|
@ -152,95 +161,131 @@ void bt_t::print_board(FILE* _fp = stderr) {
|
||||||
fprintf(_fp, "\n");
|
fprintf(_fp, "\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void bt_t::print_turn_and_moves(FILE* _fp = stderr) {
|
void bt_t::print_turn_and_moves(FILE *_fp = stderr) {
|
||||||
fprintf(_fp,"turn:%d\nmoves:", turn);
|
fprintf(_fp, "turn:%d\nmoves:", turn);
|
||||||
for(int i = 0; i < nb_moves; i++) {
|
for (int i = 0; i < nb_moves; i++) {
|
||||||
moves[i].print(_fp, turn%2 == 1, nbl);
|
moves[i].print(_fp, turn % 2 == 1, nbl);
|
||||||
fprintf(_fp, " ");
|
fprintf(_fp, " ");
|
||||||
}
|
}
|
||||||
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() {
|
||||||
update_moves();
|
update_moves();
|
||||||
int r = ((int)rand())%nb_moves;
|
int r = ((int)rand()) % nb_moves;
|
||||||
return moves[r];
|
return moves[r];
|
||||||
}
|
}
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -249,37 +294,39 @@ void bt_t::play(bt_move_t _m) {
|
||||||
int color_f = board[_m.line_f][_m.col_f];
|
int color_f = board[_m.line_f][_m.col_f];
|
||||||
board[_m.line_f][_m.col_f] = color_i;
|
board[_m.line_f][_m.col_f] = color_i;
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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] = black_pieces[nb_black_pieces-1];
|
black_pieces[i].col == _m.col_f) {
|
||||||
|
black_pieces[i] = black_pieces[nb_black_pieces - 1];
|
||||||
nb_black_pieces--;
|
nb_black_pieces--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if(color_i == BLACK) {
|
} else if (color_i == 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_i &&
|
if (black_pieces[i].line == _m.line_i &&
|
||||||
black_pieces[i].col == _m.col_i) {
|
black_pieces[i].col == _m.col_i) {
|
||||||
black_pieces[i].line = _m.line_f;
|
black_pieces[i].line = _m.line_f;
|
||||||
black_pieces[i].col = _m.col_f;
|
black_pieces[i].col = _m.col_f;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(color_f == WHITE) {
|
if (color_f == 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_f &&
|
if (white_pieces[i].line == _m.line_f &&
|
||||||
white_pieces[i].col == _m.col_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--;
|
nb_white_pieces--;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -289,18 +336,22 @@ void bt_t::play(bt_move_t _m) {
|
||||||
turn++;
|
turn++;
|
||||||
}
|
}
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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() {
|
||||||
|
@ -46,16 +46,18 @@ void showboard() {
|
||||||
}
|
}
|
||||||
void genmove() {
|
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;
|
||||||
}
|
}
|
||||||
bt_move_t m = B.get_rand_move();
|
bt_move_t m = B.get_rand_move();
|
||||||
B.play(m);
|
B.play(m);
|
||||||
if(verbose) {
|
if (verbose) {
|
||||||
m.print(stderr, white_turn, B.nbl);
|
m.print(stderr, white_turn, B.nbl);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
|
@ -64,46 +66,67 @@ void genmove() {
|
||||||
}
|
}
|
||||||
void play(char a, char b, char c, char d) {
|
void play(char a, char b, char c, char d) {
|
||||||
bt_move_t m;
|
bt_move_t m;
|
||||||
m.line_i = boardheight-(a-'0');
|
m.line_i = boardheight - (a - '0');
|
||||||
m.col_i = b-'a';
|
m.col_i = b - 'a';
|
||||||
m.line_f = boardheight-(c-'0');
|
m.line_f = boardheight - (c - '0');
|
||||||
m.col_f = d-'a';
|
m.col_f = d - 'a';
|
||||||
if(B.can_play(m)) {
|
if (B.can_play(m)) {
|
||||||
B.play(m);
|
B.play(m);
|
||||||
if(verbose) {
|
if (verbose) {
|
||||||
m.print(stderr, white_turn, B.nbl);
|
m.print(stderr, white_turn, B.nbl);
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
}
|
}
|
||||||
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)
|
||||||
char a,b,c,d; // for play cmd
|
fprintf(stderr, "rand_player started\n");
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue