feat: implement and exhaustively test the game domain core

This commit is contained in:
2026-08-28 22:37:14 +03:00
parent a957d0defc
commit 65eca822c0
9 changed files with 456 additions and 8 deletions
+8
View File
@@ -3,4 +3,12 @@
#include "app_interfaces.h"
#include "game_types.h"
enum {
kFleetPlacementAttempts = 128,
kFleetGenerationRestarts = 64,
};
typedef struct { random_source_t random; } fleet_generator_t;
bool fleet_generator_generate(const fleet_generator_t *generator, board_t *board);
bool fleet_generator_validate(const board_t *board);
+22 -1
View File
@@ -1,6 +1,27 @@
#pragma once
#include <stdbool.h>
#include "app_interfaces.h"
#include "fleet_generator.h"
#include "game_types.h"
/* Rule implementation begins in Milestone 007. */
typedef struct { game_state_t state; } game_engine_t;
typedef uint8_t game_result_t;
enum {
GAME_RESULT_OK,
GAME_RESULT_INVALID_COORDINATE,
GAME_RESULT_WRONG_PHASE,
GAME_RESULT_NOT_YOUR_TURN,
GAME_RESULT_CELL_ALREADY_SHOT,
GAME_RESULT_GENERATION_FAILED,
};
typedef struct { bool hit; bool sunk; bool finished; } shot_result_t;
void game_engine_init(game_engine_t *engine);
game_result_t game_engine_start(game_engine_t *engine, uint32_t game_id, game_mode_t mode,
const fleet_generator_t *generator);
game_result_t game_engine_shot(game_engine_t *engine, uint8_t player, coordinate_t coordinate,
shot_result_t *result);
+13 -1
View File
@@ -17,7 +17,17 @@ enum { MODE_HUMAN, MODE_BOT };
typedef struct { uint8_t x; uint8_t y; } coordinate_t;
typedef struct { uint8_t x; uint8_t y; uint8_t length; uint8_t hits; bool horizontal; } ship_t;
typedef struct { cell_t cells[kBoardCellCount]; ship_t ships[kFleetShipCount]; uint8_t ships_alive; } board_t;
typedef struct { uint32_t game_id; uint32_t version; phase_t phase; game_mode_t mode; uint8_t current_player; board_t boards[kPlayerCapacity]; } game_state_t;
typedef struct { uint16_t shots; uint16_t hits; uint16_t misses; uint8_t ships_sunk; } match_statistics_t;
typedef struct {
uint32_t game_id;
uint32_t version;
phase_t phase;
game_mode_t mode;
uint8_t current_player;
uint8_t winner;
board_t boards[kPlayerCapacity];
match_statistics_t statistics[kPlayerCapacity];
} game_state_t;
_Static_assert(kBoardCellCount == 100, "board contract changed");
_Static_assert(kFleetShipCount == 10, "fleet contract changed");
@@ -25,3 +35,5 @@ _Static_assert(kSessionCapacity == 10, "session contract changed");
_Static_assert(sizeof(coordinate_t) == 2, "coordinate must remain compact");
_Static_assert(sizeof(ship_t) <= 6, "ship grew beyond fixed budget");
_Static_assert(sizeof(board_t) <= 164, "board grew beyond fixed budget");
_Static_assert(sizeof(match_statistics_t) <= 8, "statistics grew beyond fixed budget");
_Static_assert(sizeof(game_state_t) <= 360, "game state grew beyond fixed budget");