Initial C implementation commit with Board struct

This commit is contained in:
2023-03-24 18:25:56 -04:00
parent c09dce4bd6
commit 8c9ed00826
2 changed files with 65 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
#ifndef BOGGLER_UTILS
#define BOGGLER_UTILS
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
typedef struct Board {
uint8_t height;
unsigned short width;
char **cells;
} Board;
char *get_cell_at(Board *board, uint8_t row, uint8_t col) {
return board->cells[row * col + col];
}
int print_board(Board board) {
char *row_delim = malloc((board.width * 4 + 1) * sizeof(char));
row_delim[0] = '+';
for (uint8_t i = 0; i < board.width; i++) {
strcat(row_delim, "----");
}
row_delim[strlen(row_delim)-1] = '+';
for (uint8_t row = 0; row < board.height; row++) {
printf("%s\n", row_delim);
for (uint8_t col = 0; col < board.width; col++) {
printf("|%2s ", board.cells[(row * board.width) + col]);
}
puts("|");
}
printf("%s\n", row_delim);
free(row_delim);
return 0;
}
#endif
+24
View File
@@ -0,0 +1,24 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "board.h"
int main(int argc, char **argv) {
const uint8_t BOARD_SIZE = 4;
char *letters[] = {
"u", "n", "r", "e",
"qu","n", "i", "l",
"i", "s", "h", "a",
"s", "e", "l", "b"
};
char **cell_letters = malloc(BOARD_SIZE * sizeof(char*));
for (uint8_t i = 0; i < BOARD_SIZE * BOARD_SIZE; i++) {
cell_letters[i] = malloc(strlen(letters[i]) * sizeof(char));
cell_letters[i] = letters[i];
}
Board board = { .height = 4, .width = 4, .cells = cell_letters };
print_board(board);
free(cell_letters);
}