Add BoardCell struct

This commit is contained in:
2023-03-27 16:32:11 -04:00
parent 1386e1cace
commit 82991e96e7
2 changed files with 98 additions and 14 deletions
+92 -9
View File
@@ -6,28 +6,81 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
/* Board is structured like so when referencing cells
* x ->
* +---+---+---+---+
* y |0,0|1,0|2,0|3,0| -+
* | |---------------| |
* v |0,1|1,1|2,1|3,1| |
* |---------------| | height
* |0,2|1,2|2,2|3,2| |
* |---------------| |
* |0,3|1,3|2,3|3,3| -+
* +---+---+---+---+
* | |
* +-------------+
* width
*
* Vec2 = (x, y) = (col, row)
*/
struct BoardCell {
char *letters;
uint8_t row;
uint8_t col;
struct BoardCell **adjacent_cells;
uint8_t adjacent_cnt;
};
typedef struct Vec2 {
uint8_t row;
uint8_t col;
} Vec2;
typedef struct Board { typedef struct Board {
/* BoardCells are indexed from top-to-bottom, left-to-right
* For instance a 3x3 board would have cells ordered like this
* +---+---+---+
* | 0 | 1 | 2 |
* |---+---+---|
* | 3 | 4 | 5 |
* |---+---+---|
* | 6 | 7 | 8 |
* +---+---+---+
*/
uint8_t height; uint8_t height;
unsigned short width; unsigned short width;
char **cells; struct BoardCell **cells;
} Board; } Board;
char *get_cell_at(Board *board, uint8_t row, uint8_t col) { struct BoardCell *create_board_cell(char *letters, uint8_t row, uint8_t col) {
return board->cells[row * col + col]; /* BoardCells are created with 8 adjacent cells since that is the minimum required
* for centrally placed (not on sides or corners) cells on a board.
*/
struct BoardCell *cell = malloc(sizeof(struct BoardCell*));
cell->letters = letters;
cell->row = row;
cell->col = col;
return cell;
} }
int print_board(Board board) { struct BoardCell *get_cell_at(Board *board, Vec2 pos) {
char *row_delim = malloc((board.width * 4 + 1) * sizeof(char)); return board->cells[pos.row * pos.col + pos.col];
}
int print_board(Board *board) {
char *row_delim = malloc((board->width * 4 + 1) * sizeof(char));
row_delim[0] = '+'; row_delim[0] = '+';
for (uint8_t i = 0; i < board.width; i++) { for (uint8_t i = 0; i < board->width; i++) {
strcat(row_delim, "----"); strcat(row_delim, "----");
} }
row_delim[strlen(row_delim)-1] = '+'; row_delim[strlen(row_delim)-1] = '+';
for (uint8_t row = 0; row < board.height; row++) { for (uint8_t row = 0; row < board->height; row++) {
printf("%s\n", row_delim); printf("%s\n", row_delim);
for (uint8_t col = 0; col < board.width; col++) { for (uint8_t col = 0; col < board->width; col++) {
printf("|%2s ", board.cells[(row * board.width) + col]); printf("|%2s ", board->cells[(row * board->width) + col]->letters);
} }
puts("|"); puts("|");
} }
@@ -38,4 +91,34 @@ int print_board(Board board) {
return 0; return 0;
} }
//Vec2[] get_adjacent_indexes(uint8_t board_size, Vec2 pos) {
// // Calculate total possible number of adjacent cells
// uint16_t = total_adj_cell_cnt = ((board-size-2) * (board-size-2) * 8) + ((board-size-2) * 4 * 5) + (4 * 3);
//
// Vec2[] indexes = malloc(8 * sizeof(Vec2));
// uint16_t i = 0;
// if (pos.row > 0) {
// indexes[i].append((row-1, col)) // up
// if col > 0:
// indexes.append((row-1, col-1)) // up-left
// if col < self.width - 1:
// indexes.append((row-1, col+1)) // up-right
// }
// if row < self.height - 1:
// indexes.append((row+1, col)) // down
// if col > 0:
// indexes.append((row+1, col-1)) // down-left
// if col < self.width - 1:
// indexes.append((row+1, col+1)) // down-right
// if col > 0:
// indexes.append((row, col-1)) // left
// if col < self.width - 1:
// indexes.append((row, col+1)) // right
//
// if (i != 8) {
// indexes = realloc(i * sizeof(Vec2));
// }
// return indexes
//}
#endif #endif
+6 -5
View File
@@ -12,13 +12,14 @@ int main(int argc, char **argv) {
"i", "s", "h", "a", "i", "s", "h", "a",
"s", "e", "l", "b" "s", "e", "l", "b"
}; };
char **cell_letters = malloc(BOARD_SIZE * sizeof(char*)); char **cell_letters = malloc(BOARD_SIZE * sizeof(struct BoardCell*));
struct BoardCell **cells = malloc(sizeof(struct BoardCell*) * BOARD_SIZE * BOARD_SIZE);
for (uint8_t i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) { for (uint8_t i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
cell_letters[i] = malloc(strlen(letters[i]) * sizeof(char)); cells[i] = create_board_cell(letters[i], (int)(i / BOARD_SIZE), i % BOARD_SIZE);
cell_letters[i] = letters[i];
} }
Board board = { .height = 4, .width = 4, .cells = cell_letters }; Board board = { .height = 4, .width = 4, .cells = cells };
print_board(board); print_board(&board);
free(cell_letters); free(cell_letters);
} }