penguins  1.0.0
bot.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 #include "game.h"
8 #include "movement.h"
9 #include "utils.h"
10 #include <stdbool.h>
11 #include <stddef.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
18 typedef enum BotPlacementStrategy {
29 
31 typedef enum BotMovementStrategy {
40 
44 typedef struct BotParameters {
58 
60 
62 typedef struct BotMove {
64 } BotMove;
65 
67 typedef struct FillSpan {
68  int x1, x2, y, dy;
69 } FillSpan;
70 
101 typedef struct BotState {
105 
113 
115 
119 
125  int depth;
126 
128 
171  volatile bool cancelled;
172 
176 
181 
189  short* fill_grid1;
191  short* fill_grid2;
194 
196 } BotState;
197 
199 void bot_state_free(BotState* self);
201 
202 bool bot_compute_placement(BotState* self, Coords* out_target);
203 int bot_rate_placement(BotState* self, Coords penguin);
204 
205 bool bot_compute_move(BotState* self, Coords* out_penguin, Coords* out_target);
207  BotState* self, int penguins_count, Coords* penguins, int* moves_count
208 );
209 int* bot_rate_moves_list(BotState* self, int moves_count, BotMove* moves_list);
210 int bot_rate_move(BotState* self, BotMove move);
211 bool bot_quick_junction_check(BotState* self, Coords coords);
212 short* bot_flood_fill_reset_grid(BotState* self, short** fill_grid, size_t* fill_grid_cap);
213 int bot_flood_fill_count_fish(BotState* self, short* grid, Coords start, short marker_value);
214 
215 void flood_fill(
216  int x,
217  int y,
218  bool (*check)(int x, int y, void* data),
219  void (*mark)(int x, int y, void* data),
220  FillSpan* (*alloc_stack)(size_t capacity, void* data),
221  void* data
222 );
223 
224 #ifdef __cplusplus
225 }
226 #endif
void flood_fill(int x, int y, bool(*check)(int x, int y, void *data), void(*mark)(int x, int y, void *data), FillSpan *(*alloc_stack)(size_t capacity, void *data), void *data)
An implementation of flood fill using the span filling algorithm.
Definition: bot.c:725
BotPlacementStrategy
Values of BotParameters::placement_strategy.
Definition: bot.h:18
@ BOT_PLACEMENT_MOST_FISH
Pick the tiles with the most fish in the vicinity.
Definition: bot.h:27
@ BOT_PLACEMENT_SMART
The standard "smart" algorithm, see bot_compute_placement for its description.
Definition: bot.h:21
@ BOT_PLACEMENT_RANDOM
Pick a random tile for placement.
Definition: bot.h:23
@ BOT_PLACEMENT_FIRST_POSSIBLE
Pick the first possible tile (first tile in the first row).
Definition: bot.h:25
BotMovementStrategy
Values of BotParameters::movement_strategy.
Definition: bot.h:31
@ BOT_MOVEMENT_SMART
The standard "smart" algorithm, see bot_compute_move for its description.
Definition: bot.h:34
@ BOT_MOVEMENT_RANDOM
Pick a random move.
Definition: bot.h:36
@ BOT_MOVEMENT_FIRST_POSSIBLE
Pick the first possible move (also known as the "dumb" algorithm).
Definition: bot.h:38
The core of the unified game logic library, contains the Game struct.
Movement phase functions.
A penguin -> target pair.
Definition: bot.h:62
Coords penguin
Definition: bot.h:63
Coords target
Definition: bot.h:63
Various parameters for the bot algorithm.
Definition: bot.h:44
BotPlacementStrategy placement_strategy
BOT_PLACEMENT_SMART by default.
Definition: bot.h:46
int max_move_length
The maximum number of tiles allowed for bot's moves, must be positive.
Definition: bot.h:52
int recursion_limit
The maximum recursion depth, must be positive. Zero means no recursion.
Definition: bot.h:54
void init_bot_parameters(BotParameters *self)
Initializes all fields of the given BotParameters to default values.
Definition: bot.c:31
BotMovementStrategy movement_strategy
BOT_MOVEMENT_SMART by default.
Definition: bot.h:50
int junction_check_recursion_limit
The maximum recursion depth at which junction checks are performed.
Definition: bot.h:56
int placement_scan_area
An area surrounding the placement tile which bot_rate_placement considers.
Definition: bot.h:48
Contains temporary data created during the evaluation of bot's moves.
Definition: bot.h:101
bool bot_compute_placement(BotState *self, Coords *out_target)
Computes the best placement for the current player given the current game state.
Definition: bot.c:143
int * move_scores
Definition: bot.h:187
size_t all_moves_cap
Definition: bot.h:184
size_t fill_grid1_cap
Definition: bot.h:188
FillSpan * fill_stack
Definition: bot.h:193
PossibleSteps * possible_steps
Definition: bot.h:183
struct BotState * substate
The link to the next recursive substate. Substates are allocated on demand by bot_enter_substate.
Definition: bot.h:122
size_t fill_stack_cap
Definition: bot.h:192
void bot_state_free(BotState *self)
Recursively destroys a BotState and its substates (similarly to game_free).
Definition: bot.c:75
size_t tile_scores_cap
Definition: bot.h:179
Rng * rng
Just the Rng, nothing special.
Definition: bot.h:112
size_t move_scores_cap
Definition: bot.h:186
const BotParameters * params
Shouldn't be changed while the bot is running (hence marked as const).
Definition: bot.h:107
int bot_rate_move(BotState *self, BotMove move)
The heart of the bot, assigns a score to the given move according to its usefulness.
Definition: bot.c:433
short * fill_grid2
Definition: bot.h:191
BotMove * bot_generate_all_moves_list(BotState *self, int penguins_count, Coords *penguins, int *moves_count)
Creates a list with all the possible moves (as penguin -> target pairs) of the provided penguins.
Definition: bot.c:330
short * bot_flood_fill_reset_grid(BotState *self, short **fill_grid, size_t *fill_grid_cap)
Allocates a grid for use in bot_flood_fill_count_fish and fills it with zeroes.
Definition: bot.c:620
bool bot_quick_junction_check(BotState *self, Coords coords)
A precondition for junction checks to know if a more expensive flood fill test is necessary.
Definition: bot.c:565
int * bot_rate_moves_list(BotState *self, int moves_count, BotMove *moves_list)
Applies bot_rate_move to every move in the provided list and returns a pointer to the list of scores.
Definition: bot.c:369
short * fill_grid1
Definition: bot.h:189
volatile bool cancelled
Can be set to true from another thread to cancel the move evaluation.
Definition: bot.h:171
Game * game
May be modified during the move evaluation, but once the job is done the Game will be returned to its...
Definition: bot.h:110
int depth
The recursion depth of the current state, starts at 0 for the base state and increases in substates.
Definition: bot.h:125
BotState * bot_state_new(const BotParameters *params, Game *game, Rng *rng)
Constructs a BotState (similarly game_new).
Definition: bot.c:42
int bot_flood_fill_count_fish(BotState *self, short *grid, Coords start, short marker_value)
Counts all fish accessible within an enclosed area starting at the given point using the flood fill a...
Definition: bot.c:677
Coords * tile_coords
Definition: bot.h:178
size_t tile_coords_cap
Definition: bot.h:177
bool bot_compute_move(BotState *self, Coords *out_penguin, Coords *out_target)
Computes the best move for the current player given the current game state.
Definition: bot.c:295
size_t fill_grid2_cap
Definition: bot.h:190
size_t possible_steps_cap
Definition: bot.h:182
BotState * bot_enter_substate(BotState *self)
Allocates BotState::substate if necessary and returns it.
Definition: bot.c:94
BotMove * all_moves
Definition: bot.h:185
int * tile_scores
Definition: bot.h:180
int bot_rate_placement(BotState *self, Coords penguin)
Assigns a score to the placement at the given coordinates.
Definition: bot.c:185
A pair of 2D coordinates, used for addressing the Game::board_grid.
Definition: utils.h:15
Used internally by flood_fill.
Definition: bot.h:67
int y
Definition: bot.h:68
int x2
Definition: bot.h:68
int x1
Definition: bot.h:68
int dy
Definition: bot.h:68
The central struct of the application, holds the game data and settings.
Definition: game.h:237
Exists purely to wrap an array of the numbers of steps in every possible Direction.
Definition: movement.h:35
A wrapper around random number generators.
Definition: utils.h:129