agent.c (1605B)
1 #include "hexes.h" 2 3 bool 4 agent_init(struct agent *self, enum agent_type type, struct board const *board, 5 struct threadpool *threadpool, u32 mem_limit_mib, enum hex_player player) 6 { 7 assert(self); 8 9 self->type = type; 10 11 (void) player; 12 13 switch (type) { 14 case AGENT_RANDOM: 15 return agent_random_init(&self->backend.random, board); 16 17 case AGENT_MCTS: 18 return agent_mcts_init(&self->backend.mcts, board, threadpool, mem_limit_mib, player); 19 } 20 21 return false; 22 } 23 24 void 25 agent_free(struct agent *self) 26 { 27 assert(self); 28 29 switch (self->type) { 30 case AGENT_RANDOM: agent_random_free(&self->backend.random); break; 31 case AGENT_MCTS: agent_mcts_free(&self->backend.mcts); break; 32 } 33 } 34 35 void 36 agent_play(struct agent *self, enum hex_player player, u32 x, u32 y) 37 { 38 assert(self); 39 40 switch (self->type) { 41 case AGENT_RANDOM: agent_random_play(&self->backend.random, player, x, y); break; 42 case AGENT_MCTS: agent_mcts_play(&self->backend.mcts, player, x, y); break; 43 } 44 } 45 46 void 47 agent_swap(struct agent *self) 48 { 49 assert(self); 50 51 switch (self->type) { 52 case AGENT_RANDOM: agent_random_swap(&self->backend.random); break; 53 case AGENT_MCTS: agent_mcts_swap(&self->backend.mcts); break; 54 } 55 } 56 57 bool 58 agent_next(struct agent *self, struct timespec timeout, u32 *restrict out_x, 59 u32 *restrict out_y) 60 { 61 assert(self); 62 assert(out_x); 63 assert(out_y); 64 65 switch (self->type) { 66 case AGENT_RANDOM: return agent_random_next(&self->backend.random, timeout, out_x, out_y); 67 case AGENT_MCTS: return agent_mcts_next(&self->backend.mcts, timeout, out_x, out_y); 68 } 69 70 return false; 71 } 72 73 #include "agent/random.c" 74 #include "agent/mcts.c"