add a my_player who works like the random one

This commit is contained in:
Mylloon 2023-03-19 22:28:51 +01:00
parent 68f1406a84
commit 17e2cdc336
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 177 additions and 20 deletions

1
.gitignore vendored
View file

@ -13,6 +13,7 @@ r0
rand_player
player_A
player_B
my_player
# Ajoute les gitignores
!**/.gitignore

View file

@ -1,36 +1,47 @@
CXX = g++
CXXFLAGS = -std=c++11
RM = rm
CXX = g++
CXXFLAGS = -std=c++11
RM = rm -f
NAME = TP2 - Groupe 4
TAR = tar --exclude="README.*" --exclude="Rapport" -czf
RAPPORT = Rapport/rapport.tex
NAME = TP2 - Groupe 4
TAR = tar --exclude="README.*" --exclude="Rapport" -czf
RAPPORT = Rapport/rapport.tex
SOURCES = $(wildcard src/*.cpp)
OBJETS = $(patsubst %.cpp,%.cpp.o,$(notdir $(SOURCES)))
SOURCES_RAND = $(filter-out src/my_player.cpp, $(wildcard src/*.cpp))
OBJETS_RAND = $(patsubst %.cpp,%.cpp.o,$(notdir $(SOURCES_RAND)))
EXE_RAND = rand_player
SOURCES_ME = $(filter-out src/rand_player.cpp, $(wildcard src/*.cpp))
OBJETS_ME = $(patsubst %.cpp,%.cpp.o,$(notdir $(SOURCES_RAND)))
EXE_ME = my_player
EXE = rand_player
%.cpp.o: src/%.cpp
$(CXX) -c -o $@ $< $(CXXFLAGS) $(DEVFLAGS)
all:
$(MAKE) my_player
$(MAKE) rand_player
rand_player: CXXFLAGS += -Wall -O2
rand_player: compilation
rand_player: compilation_rand
my_player: CXXFLAGS += -Wall -O2
my_player: compilation_me
dev: CXXFLAGS += -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic -Og -g
dev: CXXFLAGS += -Wold-style-cast -Wsign-conversion
dev: compilation
dev: compilation_me
compilation: $(OBJETS)
$(CXX) -o $(EXE) $(OBJETS)
compilation_rand: $(OBJETS_RAND)
$(CXX) -o $(EXE_RAND) $(OBJETS_RAND)
all:
rand_player
compilation_me: $(OBJETS_ME)
$(CXX) -o $(EXE_ME) $(OBJETS_ME)
tgz:
$(MAKE) clean 2> /dev/null
$(MAKE) clean
$(TAR) "$(NAME).tar.gz" $(RAPPORT) *
clean:
-$(RM) $(OBJETS) $(EXE)
-$(RM) *.tar.gz
$(RM) $(OBJETS_RAND) $(EXE_RAND) $(OBJETS_ME) $(EXE_ME)
$(RM) *.tar.gz

View file

@ -48,6 +48,8 @@ struct bt_move_t {
// pieces captures only in diag
// i.e. to go forward, square must be empty
struct bt_t {
char *cboard = const_cast<char *>("o@.");
int nbl;
int nbc;
int board[MAX_LINES][MAX_COLS];

145
TP2/src/my_player.cpp Normal file
View file

@ -0,0 +1,145 @@
#include "../includes/mybt.h"
#include <iostream>
bt_t B;
int boardwidth = 0;
int boardheight = 0;
bool white_turn = true;
#ifndef VERBOSE_MY_PLAYER
#define VERBOSE_MY_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");
}
void name() { printf("= my_player\n\n"); }
void newgame() {
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);
}
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) {
fprintf(stderr, "white player wins\n");
} else {
fprintf(stderr, "black player wins\n");
}
printf("= \n\n");
return;
}
bt_move_t m = B.get_rand_move();
B.play(m);
if (verbose) {
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;
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)) {
B.play(m);
if (verbose) {
m.print(stderr, white_turn, B.nbl);
fprintf(stderr, "\n");
}
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);
}
if (showboard_at_each_move) {
B.print_board(stderr);
}
printf("= \n\n");
}
int main(void) {
bool echo_on = false;
setbuf(stdout, 0);
setbuf(stderr, 0);
if (verbose) {
fprintf(stderr, "my_player started\n");
}
char a, b, c, d; // for play cmd
for (std::string line; std::getline(std::cin, line);) {
if (verbose) {
fprintf(stderr, "my_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");
}
return 0;
}

View file

@ -1,7 +1,5 @@
#include "../includes/mybt.h"
char *cboard = const_cast<char *>("o@.");
void bt_t::init(int _nbl, int _nbc) {
if (_nbl > MAX_LINES || _nbc > MAX_COLS) {
fprintf(stderr, "ERROR : MAX_LINES or MAX_COLS exceeded\n");