From ac9e2f1a42e4779e60ad975e78604fc15c62f348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20Ekstr=C3=B6m?= Date: Tue, 18 Oct 2016 23:49:52 +0200 Subject: [PATCH] Update README to reflect the new API --- README.md | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 5bbd858..2dbde3b 100644 --- a/README.md +++ b/README.md @@ -17,38 +17,60 @@ First, we should include the library headers: #include ``` -Let's set up a game board. You can have as many boards as you want if you for example want to implement multiplayer. -First, you need to supply the library with a memory location where it can put board data. The minimum required length -of this buffer can be retrieved from the function `minimum_buffer_size(int width, int height)`. +Let's set up a game. You can have as many games as you want, if you for example want to implement multiplayer. +First, you need to supply the library with a memory location where it can put game data. The minimum required length +of this buffer can be retrieved from the function `minesweeper_minimum_buffer_size(unsigned width, unsigned height)`. -To initialize a board in this buffer, call `init_board(int width, int height, float mine_density, uint8_t *buffer)`, +To initialize a game in this buffer, call `minesweeper_init(unsigned width, unsigned height, float mine_density, uint8_t *buffer)`, where mine density is a float value between 0 and 1. If 1, all tiles will contain a mine, and if 0, no tiles will contain a mine. The game starts to get unplayable around a density of 0.5. ```c -int width = 20; -int height = 10; -uint8_t *board_buffer = malloc(minimum_buffer_size(width, height)); +unsigned width = 20; +unsigned height = 10; +uint8_t *game_buffer = malloc(mineseeper_minimum_buffer_size(width, height)); srand(time(NULL)); // Let's make the game less predictable -struct board *board = board_init(width, height, 0.1, board_buffer); +struct minesweeper_game *game = minesweeper_init(width, height, 0.1, game_buffer); ``` -Next, let's move the cursor around and open a tile. Check out `minesweeper.h` for more stuff you can do. +You don't need to free the pointer returned from minesweeper_init(). It points to somewhere +within the buffer created above, so to invalidate a game you simply free the game buffer. + +Next, let's move the cursor around and open a tile. ```c -move_cursor(board, RIGHT, false); -open_tile_at_cursor(board); +minesweeper_set_cursor(game, 0, 0); +minesweeper_move_cursor(game, RIGHT, false); +minesweeper_open_tile(game, game->selected_tile); ``` -To render tiles correctly in your UI, you can check out the reference implementations. But the quick -answer is that each "tile" is an 8-bit number that contains all info about that tile. To check if a -tile contains a flag or is opened, you can do: +Using the cursor functionality is no requirement. It exists to help handling common +movement logic for keypress-based sweeping. It's also possible to modify tiles directly, +if for example making a mouse based sweeper: +```c +uint8_t *tile = minesweeper_get_tile_at(game, x, y); +minesweeper_open_tile(game, tile); +``` + +To render tiles correctly in your UI, you can find the state of a tile by doing the following: + ```c -uint8_t *tile = get_tile_at(board, board->cursor_x, board->cursor_y); +uint8_t *tile = minesweeper_get_tile_at(game, x, y); bool contains_flag = *tile & TILE_FLAG; bool is_opened = *tile & TILE_OPENED; +bool has_mine = *tile & TILE_MINE; ``` +The trick is that each tile is an 8-bit number of flags representing the state of that tile. +It also contains the number of adjacent mines, which is the number shown on opened tiles +in most minesweepers. You can retrieve it like this: +```c +uint8_t mine_count = minesweeper_get_adjacent_mine_count(tile); +``` + +Check out the reference implementations for more examples on how to render a game. +All available functions are documented in minesweeper.h. + ## Testing Run `make test` to run the unit tests. It might be a good idea to run the tests with your preferred compiler, to catch anything I might've missed. Please add an