mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-25 19:19:20 -04:00
Initial C implementation commit with Board struct
This commit is contained in:
@@ -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
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user