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
#define MYBT_H
#include <cstdio>
#include <cstdlib>
#include <random>
#define WHITE 0
#define BLACK 1
#define EMPTY 2
char *cboard = (char *)"o@.";
struct bt_piece_t {
int line;
int col;
};
struct bt_move_t {
int line_i;
int col_i;
@ -47,7 +51,6 @@ struct bt_move_t {
// pieces moves from 1 square in diag and in front
// pieces captures only in diag
// i.e. to go forward, square must be empty
struct bt_t {
int nbl;
int nbc;
@ -60,6 +63,7 @@ struct bt_t {
int nb_black_pieces;
bt_move_t moves[3 * 2 * MAX_LINES];
int nb_moves;
// last turn of moves update
int turn_of_last_moves_update;
@ -127,6 +131,7 @@ void bt_t::init(int _nbl, int _nbc) {
init_pieces();
update_moves();
}
void bt_t::init_pieces() {
nb_white_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)
void bt_t::print_board(FILE *_fp = stderr) {
fprintf(_fp, " \x1B[34m");
@ -153,14 +159,16 @@ void bt_t::print_board(FILE *_fp = stderr) {
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)
if (board[i][j] == BLACK) {
fprintf(_fp, "\x1B[31m%c\x1B[0m ", cboard[board[i][j]]);
else
} else {
fprintf(_fp, "%c ", cboard[board[i][j]]);
}
}
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++) {
@ -169,123 +177,168 @@ void bt_t::print_turn_and_moves(FILE *_fp = stderr) {
}
fprintf(_fp, "\n");
}
void bt_t::update_moves() {
if (turn % 2 == 0)
if (turn % 2 == 0) {
update_moves(WHITE);
else
} else {
update_moves(BLACK);
}
}
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
}
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))
if (white_can_move_right(li, ci)) {
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);
if (white_can_move_left(li, 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))
if (black_can_move_right(li, ci)) {
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);
if (black_can_move_left(li, 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)
if (_line == 0) {
return false;
if (_col == nbc - 1)
}
if (_col == nbc - 1) {
return false;
if (board[_line - 1][_col + 1] != WHITE)
}
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)
if (_line == 0) {
return false;
if (board[_line - 1][_col] == EMPTY)
}
if (board[_line - 1][_col] == EMPTY) {
return true;
}
return false;
}
bool bt_t::white_can_move_left(int _line, int _col) {
if (_line == 0)
if (_line == 0) {
return false;
if (_col == 0)
}
if (_col == 0) {
return false;
if (board[_line - 1][_col - 1] != WHITE)
}
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)
if (_line == nbl - 1) {
return false;
if (_col == nbc - 1)
}
if (_col == nbc - 1) {
return false;
if (board[_line + 1][_col + 1] != BLACK)
}
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)
if (_line == nbl - 1) {
return false;
if (board[_line + 1][_col] == EMPTY)
}
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)
if (_line == nbl - 1) {
return false;
if (_col == 0)
}
if (_col == 0) {
return false;
if (board[_line + 1][_col - 1] != BLACK)
}
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;
return moves[r];
}
bool bt_t::can_play(bt_move_t _m) {
int dx = abs(_m.col_f - _m.col_i);
if (dx > 1)
if (dx > 1) {
return false;
}
int dy = abs(_m.line_f - _m.line_i);
if (dy > 1)
if (dy > 1) {
return false;
if (_m.line_i < 0 || _m.line_i >= nbl)
}
if (_m.line_i < 0 || _m.line_i >= nbl) {
return false;
if (_m.line_f < 0 || _m.line_f >= nbl)
}
if (_m.line_f < 0 || _m.line_f >= nbl) {
return false;
if (_m.col_i < 0 || _m.col_i >= nbc)
}
if (_m.col_i < 0 || _m.col_i >= nbc) {
return false;
if (_m.col_f < 0 || _m.col_f >= nbc)
}
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)
if (color_i == EMPTY) {
return false;
if (color_i == color_f)
}
if (color_i == color_f) {
return false;
if (turn % 2 == 0 && color_i == BLACK)
}
if (turn % 2 == 0 && color_i == BLACK) {
return false;
if (turn % 2 == 1 && color_i == WHITE)
}
if (turn % 2 == 1 && color_i == WHITE) {
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 true;
}
@ -335,23 +388,29 @@ 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)
if (board[0][i] == WHITE) {
return WHITE;
}
}
for (int i = 0; i < nbc; i++) {
if (board[nbl - 1][i] == BLACK)
if (board[nbl - 1][i] == BLACK) {
return BLACK;
}
}
return EMPTY;
}
double bt_t::score(int _color) {
int state = endgame();
if (state == EMPTY)
if (state == EMPTY) {
return 0.0;
if (_color == state)
}
if (_color == state) {
return 1.0;
}
return -1.0;
}

View file

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