Skip to content

Instantly share code, notes, and snippets.

@woodrowbarlow
Forked from jdpage/hex.h
Last active October 13, 2018 22:16
Show Gist options
  • Save woodrowbarlow/388a4b354a19562b246fd7855d1c7234 to your computer and use it in GitHub Desktop.
Save woodrowbarlow/388a4b354a19562b246fd7855d1c7234 to your computer and use it in GitHub Desktop.
proposed hex board API
#ifndef HEX_HEX_H
#define HEX_HEX_H
#include <stdlib.h>
enum hex_err_e {
HEX_OK,
HEX_ENOMEM,
HEX_EBOUNDS,
HEX_EBADSPACE,
HEX_ESIZEMISMATCH,
HEX_ERRS_MAX
};
enum hex_color_e {
HEX_COLOR_NONE,
HEX_COLOR_RED,
HEX_COLOR_BLUE,
HEX_COLORS_MAX
} __attribute__ ((__packed__));
enum hex_edge_e {
HEX_EDGE_RIGHT,
HEX_EDGE_BOT_RIGHT,
HEX_EDGE_BOT_LEFT,
HEX_EDGE_LEFT,
HEX_EDGE_TOP_LEFT,
HEX_EDGE_TOP_RIGHT,
HEX_EDGES_MAX
}
struct hex_tile_s
{
struct hex_tile_s *edges[HEX_EDGES_MAX];
enum hex_color_e color;
int row;
int column;
};
struct hex_board_s
{
const unsigned int width;
const unsigned int height;
struct hex_tile_s *tiles;
struct hex_tile_s *top_right_anchor;
struct hex_tile_s *bottom_right_anchor;
};
typedef enum hex_err_e hex_err;
typedef enum hex_color_e hex_color;
typedef struct hex_tile_s hex_tile;
typedef struct hex_board_s hex_board;
typedef void *hex_alloc(size_t nmemb, size_t size);
// Creates a new hex board of the given size. In the case of hex_board_init, the
// pointer returned can be freed as usual for memory allocated with the given
// allocator function. Returns HEX_ENOMEM if allocation fails.
hex_err hex_board_init(hex_board **board, int size, hex_alloc *alloc);
void hex_board_initat(hex_board *board, int size);
// Gets the number of bytes necessary to store a board of the given size. Can be
// used to allocate memory for use with hex_board_initat.
size_t hex_board_sizeof(int size);
// Gets a pointer to the board data, in column-major order, i.e. columns are
// contiguous.
hex_tile *hex_board_data(hex_board *board, size_t *data_len);
const hex_tile *hex_board_rodata(const hex_board *board, size_t *data_len);
// Gets a pointer to a space on the board. Returns HEX_EBOUNDS if row, col are
// out-of-bounds.
hex_err hex_board_space(
hex_board *board,
int row, int col,
hex_tile **space);
hex_err hex_board_rospace(
const hex_board *board,
int row, int col,
const hex_tile **space);
// Gets the coordinates of the given space. Returns HEX_EBADSPACE if the given
// space is not part of the given board.
hex_err hex_board_coords(
const hex_board *board,
const hex_tile *space,
int *row, int *col);
// Given a space on a board, gets the corresponding space on another board.
// Returns HEX_EBADSPACE if src_space is not from src_board.
hex_err hex_board_correlate(
const hex_board *dest_board,
hex_tile **dest_space,
const hex_board *src_board,
const hex_tile *src_space);
// Populates the 'neighbors' array with the coordinates of the neighbors of the
// given square, with alternating row and column values, and stores the number
// of neighbors. The neighbors array is assumed to have enough room to store six
// neighbors, i.e. it should be of length 12. Returns HEX_EBOUNDS if row, col
// are out-of-bounds.
hex_err hex_board_neighborcoords(
const hex_board *board,
int row, int col,
int *neighbors,
int *neighbor_count);
// Populates the 'neighbors' array with pointers to the spaces neighboring the
// given space. Returns HEX_EBADSPACE if the given space is not part of the
// given board.
hex_err hex_board_neighbors(
const hex_board *board,
const hex_tile *space,
hex_tile **neighbors,
int *neighbor_count);
// Gets the winner of the board, if any. Returns HEX_COLOR_NONE if there is no
// winner.
hex_color hex_board_getwinner(const hex_board *board);
// Clears the given board, resetting it to its initial state.
void hex_board_clear(hex_board *board);
// Copies the data from one board to another board. Returns HEX_ESIZEMISMATCH if
// the boards are not the same size.
hex_err hex_board_copy(hex_board *dest, const hex_board *src);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment