2023-02-16 20:46:54 +01:00
|
|
|
#include "mybt.h"
|
2023-02-16 20:35:32 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <iostream>
|
2023-02-16 20:46:54 +01:00
|
|
|
#include <string.h>
|
2023-02-16 20:35:32 +01:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
bt_t B;
|
|
|
|
int boardwidth = 0;
|
|
|
|
int boardheight = 0;
|
|
|
|
bool white_turn = true;
|
|
|
|
|
|
|
|
#ifndef VERBOSE_RAND_PLAYER
|
|
|
|
#define VERBOSE_RAND_PLAYER
|
|
|
|
bool verbose = true;
|
|
|
|
bool showboard_at_each_move = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void help() {
|
|
|
|
fprintf(stderr, " quit\n");
|
|
|
|
fprintf(stderr, " echo ON | OFF\n");
|
|
|
|
fprintf(stderr, " help\n");
|
|
|
|
fprintf(stderr, " name <PLAYER_NAME>\n");
|
|
|
|
fprintf(stderr, " newgame <NBCOL> <NBLINE>\n");
|
|
|
|
fprintf(stderr, " genmove\n");
|
|
|
|
fprintf(stderr, " play <L0C0L1C1>\n");
|
|
|
|
fprintf(stderr, " showboard\n");
|
|
|
|
}
|
2023-02-16 20:46:54 +01:00
|
|
|
void name() { printf("= rand_player\n\n"); }
|
2023-02-16 20:35:32 +01:00
|
|
|
void newgame() {
|
2023-02-16 20:46:54 +01:00
|
|
|
if ((boardheight < 1 || boardheight > 10) &&
|
|
|
|
(boardwidth < 1 || boardwidth > 10)) {
|
2023-02-16 20:35:32 +01:00
|
|
|
fprintf(stderr, "boardsize is %d %d ???\n", boardheight, boardwidth);
|
|
|
|
printf("= \n\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
B.init(boardheight, boardwidth);
|
|
|
|
white_turn = true;
|
2023-02-16 20:46:54 +01:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "ready to play on %dx%d board\n", boardheight, boardwidth);
|
2023-02-16 20:35:32 +01:00
|
|
|
printf("= \n\n");
|
|
|
|
}
|
|
|
|
void showboard() {
|
|
|
|
B.print_board(stderr);
|
|
|
|
printf("= \n\n");
|
|
|
|
}
|
|
|
|
void genmove() {
|
|
|
|
int ret = B.endgame();
|
2023-02-16 20:46:54 +01:00
|
|
|
if (ret != EMPTY) {
|
2023-02-16 20:35:32 +01:00
|
|
|
fprintf(stderr, "game finished\n");
|
2023-02-16 20:46:54 +01:00
|
|
|
if (ret == WHITE)
|
|
|
|
fprintf(stderr, "white player wins\n");
|
|
|
|
else
|
|
|
|
fprintf(stderr, "black player wins\n");
|
2023-02-16 20:35:32 +01:00
|
|
|
printf("= \n\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bt_move_t m = B.get_rand_move();
|
|
|
|
B.play(m);
|
2023-02-16 20:46:54 +01:00
|
|
|
if (verbose) {
|
2023-02-16 20:35:32 +01:00
|
|
|
m.print(stderr, white_turn, B.nbl);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
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;
|
2023-02-16 20:46:54 +01:00
|
|
|
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)) {
|
2023-02-16 20:35:32 +01:00
|
|
|
B.play(m);
|
2023-02-16 20:46:54 +01:00
|
|
|
if (verbose) {
|
2023-02-16 20:35:32 +01:00
|
|
|
m.print(stderr, white_turn, B.nbl);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
white_turn = !white_turn;
|
|
|
|
} else {
|
2023-02-16 20:46:54 +01:00
|
|
|
fprintf(stderr, "CANT play %d %d %d %d ?\n", m.line_i, m.col_i, m.line_f,
|
|
|
|
m.col_f);
|
2023-02-16 20:35:32 +01:00
|
|
|
}
|
2023-02-16 20:46:54 +01:00
|
|
|
if (showboard_at_each_move)
|
2023-03-10 13:29:30 +01:00
|
|
|
B.print_board(stderr);
|
2023-02-16 20:35:32 +01:00
|
|
|
printf("= \n\n");
|
|
|
|
}
|
2023-02-16 20:46:54 +01:00
|
|
|
int main(int _ac, char **_av) {
|
2023-02-16 20:35:32 +01:00
|
|
|
bool echo_on = false;
|
|
|
|
setbuf(stdout, 0);
|
|
|
|
setbuf(stderr, 0);
|
2023-02-16 20:46:54 +01:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "rand_player started\n");
|
|
|
|
char a, b, c, d; // for play cmd
|
2023-02-16 20:35:32 +01:00
|
|
|
for (std::string line; std::getline(std::cin, line);) {
|
2023-02-16 20:46:54 +01:00
|
|
|
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(">");
|
2023-02-16 20:35:32 +01:00
|
|
|
}
|
2023-02-16 20:46:54 +01:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "bye.\n");
|
2023-02-16 20:35:32 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|