rsmc
Loading...
Searching...
No Matches
abstractions.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2// Copyright (c) 2026 01eksa
3
10#ifndef RSMC_ABSTRACTIONS_H
11#define RSMC_ABSTRACTIONS_H
12
13#include "core.h"
14#include <assert.h>
15#include <stdbool.h>
16#include <stdint.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
25typedef uint8_t RsmcBoardCell;
26enum {
31};
38static inline bool rsmc_board_cell_is_valid(const RsmcBoardCell cell)
39{
40 return cell < RsmcBoardCellCount;
41}
42
49static_assert(sizeof(RsmcClassicBoard) == 64, "unexpected padding in RsmcBoard");
50
55typedef struct {
56 int8_t x;
57 int8_t y;
59static_assert(sizeof(RsmcCoords) == 2, "unexpected padding in RsmcCoords");
66static inline bool rsmc_coords_is_valid(const RsmcCoords coords)
67{
68 return coords.x >= RsmcMinCoord && coords.y >= RsmcMinCoord && coords.x <= RsmcMaxCoord &&
69 coords.y <= RsmcMaxCoord;
70}
71
78static inline int rsmc_coords_to_index(const RsmcCoords coords)
79{
80 int index = coords.y * RsmcBoardSize + coords.x;
81 if (index < 0 || index >= RsmcBoardArea)
82 index = -1;
83
84 return index;
85}
86
93static inline RsmcBitMask rsmc_index_to_bitmask(const uint8_t index)
94{
95 if (index >= RsmcBoardArea || index >= sizeof(uint64_t) * 8)
96 return 0;
97 return (uint64_t)1 << index;
98}
99
100static inline RsmcBoardCell rsmc_board_get_cell(const RsmcBoard board, const RsmcCoords coords)
101{
102 const RsmcBitMask mask = rsmc_index_to_bitmask(rsmc_coords_to_index(coords));
103 if (board.black & mask)
104 return RsmcBoardCellBlack;
105 if (board.white & mask)
106 return RsmcBoardCellWhite;
107 return RsmcBoardCellEmpty;
108}
109
110static inline RsmcBoard rsmc_board_set_cell(RsmcBoard board, const RsmcCoords coords,
111 const RsmcBoardCell cell)
112{
113 const RsmcBitMask mask = rsmc_index_to_bitmask(rsmc_coords_to_index(coords));
114 board.black &= ~mask;
115 board.white &= ~mask;
116
117 if (cell == RsmcBoardCellBlack) {
118 board.black |= mask;
119 } else if (cell == RsmcBoardCellWhite) {
120 board.white |= mask;
121 }
122
123 return board;
124}
125
126static inline RsmcClassicBoard rsmc_classic_board_from_board(const RsmcBoard board)
127{
128 RsmcClassicBoard classic_board;
129 for (int y = 0; y < RsmcBoardSize; y++) {
130 for (int x = 0; x < RsmcBoardSize; x++) {
131 classic_board.cells[y][x] =
132 rsmc_board_get_cell(board, (RsmcCoords){.x = (int8_t)x, .y = (int8_t)y});
133 }
134 }
135
136 return classic_board;
137}
138
139static inline RsmcBoard rsmc_board_from_classic_board(const RsmcClassicBoard classic_board)
140{
141 RsmcBoard board = {0};
142 for (int y = 0; y < RsmcBoardSize; y++) {
143 for (int x = 0; x < RsmcBoardSize; x++) {
144 const RsmcCoords coords = {.x = (int8_t)x, .y = (int8_t)y};
145 if (classic_board.cells[y][x] == RsmcBoardCellBlack) {
146 board.black |= rsmc_index_to_bitmask(rsmc_coords_to_index(coords));
147 } else if (classic_board.cells[y][x] == RsmcBoardCellWhite) {
148 board.white |= rsmc_index_to_bitmask(rsmc_coords_to_index(coords));
149 }
150 }
151 }
152
153 return board;
154}
155
156#ifdef __cplusplus
157}
158#endif
159
160#endif // RSMC_ABSTRACTIONS_H
@ RsmcBoardCellBlack
Definition abstractions.h:28
@ RsmcBoardCellCount
Definition abstractions.h:30
@ RsmcBoardCellEmpty
Definition abstractions.h:27
@ RsmcBoardCellWhite
Definition abstractions.h:29
uint8_t RsmcBoardCell
Board cell (empty, black, white)
Definition abstractions.h:25
Types and constants for rsmc.
uint64_t RsmcBitMask
Definition core.h:134
@ RsmcMinCoord
Minimal possible value for index on the 2-dimensional board.
Definition core.h:47
@ RsmcMaxCoord
Maximal possible value for index on the 2-dimensional board.
Definition core.h:51
@ RsmcBoardArea
Count of cells on the board.
Definition core.h:43
@ RsmcBoardSize
Size of one side of the board.
Definition core.h:39
Stores board as two RsmcBitMap for each player pieces.
Definition core.h:139
RsmcBitMap black
Definition core.h:142
RsmcBitMap white
Definition core.h:143
Stores board in 2-dimensional array of all RsmcBoardCell.
Definition abstractions.h:46
RsmcBoardCell cells[RsmcBoardSize][RsmcBoardSize]
Definition abstractions.h:47
Stores coords for the board. Coords might be invalid and negative.
Definition abstractions.h:55
int8_t x
Definition abstractions.h:56
int8_t y
Definition abstractions.h:57