penguins  1.0.0
movement.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #include "board.h"
7 #include "game.h"
8 #include "utils.h"
9 #include <assert.h>
10 #include <stdbool.h>
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 typedef enum MovementError {
29 
35 typedef struct PossibleSteps {
38 
39 void movement_begin(Game* game);
40 void movement_end(Game* game);
41 
42 int movement_switch_player(Game* game);
43 bool any_valid_player_move_exists(const Game* game, int player_idx);
45 MovementError validate_movement(const Game* game, Coords start, Coords target, Coords* fail);
46 void move_penguin(Game* game, Coords start, Coords target);
47 void undo_move_penguin(Game* game);
48 
50 inline int count_obstructed_directions(const Game* game, Coords penguin) {
51  assert(is_tile_in_bounds(game, penguin));
52  int result = 0;
53  for (int dir = 0; dir < DIRECTION_MAX; dir++) {
54  Coords neighbor = DIRECTION_TO_COORDS[dir];
55  neighbor.x += penguin.x, neighbor.y += penguin.y;
56  if (!(is_tile_in_bounds(game, neighbor) && is_fish_tile(get_tile(game, neighbor)))) {
57  result += 1;
58  }
59  }
60  return result;
61 }
62 
65  assert(is_tile_in_bounds(game, start));
66  PossibleSteps moves;
67  for (int dir = 0; dir < DIRECTION_MAX; dir++) {
68  Coords d = DIRECTION_TO_COORDS[dir];
69  Coords target = start;
70  int steps = 0;
71  while (true) {
72  target.x += d.x, target.y += d.y;
73  if (!(is_tile_in_bounds(game, target) && is_fish_tile(get_tile(game, target)))) break;
74  steps++;
75  }
76  moves.steps[dir] = steps;
77  }
78  return moves;
79 }
80 
81 #ifdef __cplusplus
82 }
83 #endif
Functions for working with the game board (and the specifics of its encoding)
#define is_fish_tile(tile)
Definition: board.h:25
The core of the unified game logic library, contains the Game struct.
MovementError
Definition: movement.h:16
@ MOVEMENT_OVER_EMPTY_TILE
Definition: movement.h:25
@ MOVEMENT_PENGUIN_BLOCKED
Definition: movement.h:27
@ MOVEMENT_NOT_YOUR_PENGUIN
Definition: movement.h:22
@ MOVEMENT_NOT_A_PENGUIN
Definition: movement.h:21
@ MOVEMENT_ONTO_PENGUIN
Definition: movement.h:24
@ MOVEMENT_OUT_OF_BOUNDS
Definition: movement.h:18
@ MOVEMENT_CURRENT_LOCATION
Definition: movement.h:19
@ MOVEMENT_DIAGONAL
Definition: movement.h:20
@ MOVEMENT_VALID
Definition: movement.h:17
@ MOVEMENT_OVER_PENGUIN
Definition: movement.h:26
@ MOVEMENT_ONTO_EMPTY_TILE
Definition: movement.h:23
A pair of 2D coordinates, used for addressing the Game::board_grid.
Definition: utils.h:15
int x
Definition: utils.h:16
int y
Definition: utils.h:17
The central struct of the application, holds the game data and settings.
Definition: game.h:237
ALWAYS_INLINE bool is_tile_in_bounds(const Game *game, Coords coords)
Checks if the given coords are within the bounds of the board.
Definition: board.h:70
int movement_switch_player(Game *game)
Performs the player switching logic for the movement phase.
Definition: movement.c:31
void move_penguin(Game *game, Coords start, Coords target)
Creates a GameLogMovement entry. The requested move must be valid.
Definition: movement.c:118
ALWAYS_INLINE short get_tile(const Game *game, Coords coords)
Returns the value of the tile at coords. Fails if coords are outside the bounds.
Definition: board.h:108
bool any_valid_player_move_exists(const Game *game, int player_idx)
Definition: movement.c:47
int count_obstructed_directions(const Game *game, Coords penguin)
Definition: movement.h:50
void movement_begin(Game *game)
Enters the GAME_PHASE_MOVEMENT phase, can only be called in GAME_PHASE_SETUP_DONE.
Definition: movement.c:11
PossibleSteps calculate_penguin_possible_moves(const Game *game, Coords start)
Definition: movement.h:64
MovementError validate_movement(const Game *game, Coords start, Coords target, Coords *fail)
Definition: movement.c:76
MovementError validate_movement_start(const Game *game, Coords start)
Definition: movement.c:58
void movement_end(Game *game)
Exits the GAME_PHASE_MOVEMENT phase and switches to GAME_PHASE_SETUP_DONE.
Definition: movement.c:20
void undo_move_penguin(Game *game)
Removes a GameLogMovement entry from the log and undoes it.
Definition: movement.c:142
Exists purely to wrap an array of the numbers of steps in every possible Direction.
Definition: movement.h:35
int steps[DIRECTION_MAX]
Definition: movement.h:36
const Coords DIRECTION_TO_COORDS[DIRECTION_MAX]
A table that maps Direction variants to relative Coords.
Definition: utils.c:19
@ DIRECTION_MAX
Definition: utils.h:25