add spaces

This commit is contained in:
Mylloon 2023-03-10 13:40:11 +01:00
parent 27a170c6e3
commit 46f426dfbe
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 140 additions and 64 deletions

View file

@ -1,17 +1,21 @@
#ifndef MYBT_H #ifndef MYBT_H
#define MYBT_H #define MYBT_H
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <random> #include <random>
#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 line;
int col; int col;
}; };
struct bt_move_t { struct bt_move_t {
int line_i; int line_i;
int col_i; int col_i;
@ -47,7 +51,6 @@ struct bt_move_t {
// pieces moves from 1 square in diag and in front // pieces moves from 1 square in diag and in front
// pieces captures only in diag // pieces captures only in diag
// i.e. to go forward, square must be empty // i.e. to go forward, square must be empty
struct bt_t { struct bt_t {
int nbl; int nbl;
int nbc; int nbc;
@ -60,6 +63,7 @@ struct bt_t {
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;
@ -127,6 +131,7 @@ void bt_t::init(int _nbl, int _nbc) {
init_pieces(); init_pieces();
update_moves(); update_moves();
} }
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;
@ -143,6 +148,7 @@ 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");
@ -153,14 +159,16 @@ void bt_t::print_board(FILE *_fp = stderr) {
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]]);
} }
}
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++) {
@ -169,123 +177,168 @@ 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) if (turn % 2 == 0) {
update_moves(WHITE); update_moves(WHITE);
else } else {
update_moves(BLACK); update_moves(BLACK);
}
} }
void bt_t::update_moves(int _color) { void bt_t::update_moves(int _color) {
if (turn_of_last_moves_update == turn) if (turn_of_last_moves_update == turn) {
return; // MAJ ever done 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)) if (white_can_move_right(li, ci)) {
add_move(li, ci, li - 1, ci + 1); add_move(li, ci, li - 1, ci + 1);
if (white_can_move_forward(li, ci)) }
if (white_can_move_forward(li, ci)) {
add_move(li, ci, li - 1, ci); add_move(li, ci, li - 1, ci);
if (white_can_move_left(li, ci)) }
if (white_can_move_left(li, ci)) {
add_move(li, ci, li - 1, ci - 1); 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)) if (black_can_move_right(li, ci)) {
add_move(li, ci, li + 1, ci + 1); add_move(li, ci, li + 1, ci + 1);
if (black_can_move_forward(li, ci)) }
if (black_can_move_forward(li, ci)) {
add_move(li, ci, li + 1, ci); add_move(li, ci, li + 1, ci);
if (black_can_move_left(li, ci)) }
if (black_can_move_left(li, ci)) {
add_move(li, ci, li + 1, ci - 1); 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) if (_line == 0) {
return false; return false;
if (_col == nbc - 1) }
if (_col == nbc - 1) {
return false; return false;
if (board[_line - 1][_col + 1] != WHITE) }
if (board[_line - 1][_col + 1] != WHITE) {
return true; 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) if (_line == 0) {
return false; return false;
if (board[_line - 1][_col] == EMPTY) }
if (board[_line - 1][_col] == EMPTY) {
return true; 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) if (_line == 0) {
return false; return false;
if (_col == 0) }
if (_col == 0) {
return false; return false;
if (board[_line - 1][_col - 1] != WHITE) }
if (board[_line - 1][_col - 1] != WHITE) {
return true; 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) if (_line == nbl - 1) {
return false; return false;
if (_col == nbc - 1) }
if (_col == nbc - 1) {
return false; return false;
if (board[_line + 1][_col + 1] != BLACK) }
if (board[_line + 1][_col + 1] != BLACK) {
return true; 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) if (_line == nbl - 1) {
return false; return false;
if (board[_line + 1][_col] == EMPTY) }
if (board[_line + 1][_col] == EMPTY) {
return true; 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) if (_line == nbl - 1) {
return false; return false;
if (_col == 0) }
if (_col == 0) {
return false; return false;
if (board[_line + 1][_col - 1] != BLACK) }
if (board[_line + 1][_col - 1] != BLACK) {
return true; 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) if (dx > 1) {
return false; return false;
}
int dy = abs(_m.line_f - _m.line_i); int dy = abs(_m.line_f - _m.line_i);
if (dy > 1) if (dy > 1) {
return false; return false;
if (_m.line_i < 0 || _m.line_i >= nbl) }
if (_m.line_i < 0 || _m.line_i >= nbl) {
return false; return false;
if (_m.line_f < 0 || _m.line_f >= nbl) }
if (_m.line_f < 0 || _m.line_f >= nbl) {
return false; return false;
if (_m.col_i < 0 || _m.col_i >= nbc) }
if (_m.col_i < 0 || _m.col_i >= nbc) {
return false; return false;
if (_m.col_f < 0 || _m.col_f >= nbc) }
if (_m.col_f < 0 || _m.col_f >= nbc) {
return false; 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) if (color_i == EMPTY) {
return false; return false;
if (color_i == color_f) }
if (color_i == color_f) {
return false; return false;
if (turn % 2 == 0 && color_i == BLACK) }
if (turn % 2 == 0 && color_i == BLACK) {
return false; return false;
if (turn % 2 == 1 && color_i == WHITE) }
if (turn % 2 == 1 && color_i == WHITE) {
return false; return false;
if (_m.col_i == _m.col_f && color_f != EMPTY) }
if (_m.col_i == _m.col_f && color_f != EMPTY) {
return false; return false;
}
return true; return true;
} }
@ -335,23 +388,29 @@ 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) if (board[0][i] == WHITE) {
return WHITE; return WHITE;
} }
}
for (int i = 0; i < nbc; i++) { for (int i = 0; i < nbc; i++) {
if (board[nbl - 1][i] == BLACK) if (board[nbl - 1][i] == BLACK) {
return 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) if (state == EMPTY) {
return 0.0; return 0.0;
if (_color == state) }
if (_color == state) {
return 1.0; return 1.0;
}
return -1.0; return -1.0;
} }

View file

@ -1,4 +1,5 @@
#include "mybt.h" #include "mybt.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
@ -26,7 +27,9 @@ void help() {
fprintf(stderr, " play <L0C0L1C1>\n"); fprintf(stderr, " play <L0C0L1C1>\n");
fprintf(stderr, " showboard\n"); fprintf(stderr, " showboard\n");
} }
void name() { printf("= rand_player\n\n"); } void name() { printf("= rand_player\n\n"); }
void newgame() { void newgame() {
if ((boardheight < 1 || boardheight > 10) && if ((boardheight < 1 || boardheight > 10) &&
(boardwidth < 1 || boardwidth > 10)) { (boardwidth < 1 || boardwidth > 10)) {
@ -36,22 +39,26 @@ void newgame() {
} }
B.init(boardheight, boardwidth); B.init(boardheight, boardwidth);
white_turn = true; white_turn = true;
if (verbose) if (verbose) {
fprintf(stderr, "ready to play on %dx%d board\n", boardheight, boardwidth); fprintf(stderr, "ready to play on %dx%d board\n", boardheight, boardwidth);
}
printf("= \n\n"); printf("= \n\n");
} }
void showboard() { void showboard() {
B.print_board(stderr); B.print_board(stderr);
printf("= \n\n"); printf("= \n\n");
} }
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) if (ret == WHITE) {
fprintf(stderr, "white player wins\n"); fprintf(stderr, "white player wins\n");
else } else {
fprintf(stderr, "black player wins\n"); fprintf(stderr, "black player wins\n");
}
printf("= \n\n"); printf("= \n\n");
return; return;
} }
@ -64,6 +71,7 @@ void genmove() {
white_turn = !white_turn; white_turn = !white_turn;
printf("= %s\n\n", m.tostr(B.nbl).c_str()); printf("= %s\n\n", m.tostr(B.nbl).c_str());
} }
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');
@ -81,52 +89,61 @@ void play(char a, char b, char c, char d) {
fprintf(stderr, "CANT play %d %d %d %d ?\n", m.line_i, m.col_i, m.line_f, fprintf(stderr, "CANT play %d %d %d %d ?\n", m.line_i, m.col_i, m.line_f,
m.col_f); m.col_f);
} }
if (showboard_at_each_move) if (showboard_at_each_move) {
B.print_board(stderr); B.print_board(stderr);
}
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) if (verbose) {
fprintf(stderr, "rand_player started\n"); 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) if (verbose) {
fprintf(stderr, "rand_player receive %s\n", line.c_str()); fprintf(stderr, "rand_player receive %s\n", line.c_str());
if (echo_on) }
if (verbose) if (echo_on) {
if (verbose) {
fprintf(stderr, "%s\n", line.c_str()); fprintf(stderr, "%s\n", line.c_str());
}
}
if (line.compare("quit") == 0) { if (line.compare("quit") == 0) {
printf("= \n\n"); printf("= \n\n");
break; break;
} else if (line.compare("echo ON") == 0) } else if (line.compare("echo ON") == 0) {
echo_on = true; echo_on = true;
else if (line.compare("echo OFF") == 0) } else if (line.compare("echo OFF") == 0) {
echo_on = false; echo_on = false;
else if (line.compare("help") == 0) } else if (line.compare("help") == 0) {
help(); help();
else if (line.compare("name") == 0) } else if (line.compare("name") == 0) {
name(); name();
else if (sscanf(line.c_str(), "newgame %d %d\n", &boardheight, } else if (sscanf(line.c_str(), "newgame %d %d\n", &boardheight,
&boardwidth) == 2) &boardwidth) == 2) {
newgame(); newgame();
else if (line.compare("genmove") == 0) } else if (line.compare("genmove") == 0) {
genmove(); genmove();
else if (sscanf(line.c_str(), "play %c%c%c%c\n", &a, &b, &c, &d) == 4) } else if (sscanf(line.c_str(), "play %c%c%c%c\n", &a, &b, &c, &d) == 4) {
play(a, b, c, d); play(a, b, c, d);
else if (line.compare("showboard") == 0) } else if (line.compare("showboard") == 0) {
showboard(); showboard();
else if (line.compare(0, 2, "//") == 0) } else if (line.compare(0, 2, "//") == 0)
; // just comments ; // just comments
else else {
fprintf(stderr, "???\n"); fprintf(stderr, "???\n");
if (echo_on) }
if (echo_on) {
printf(">"); printf(">");
} }
if (verbose) }
if (verbose) {
fprintf(stderr, "bye.\n"); fprintf(stderr, "bye.\n");
}
return 0; return 0;
} }