Board.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <assert.h>
  4. #include "config.h"
  5. #include "Board.h"
  6. #include "Snake.h"
  7. #include "utils.h"
  8. #define MAT_INDEX(mat, w, i, j) (mat)[(j) + (w) * (i)]
  9. Board board_alloc(const int width, const int height) {
  10. Board board = {};
  11. board.width = width * 2;
  12. board.height = height;
  13. board.width_with_borders = board.width + 2;
  14. board.height_with_borders = board.height + 2;
  15. board.squares = (char*) malloc(sizeof(char) * board.width_with_borders * board.height_with_borders);
  16. if (board.squares == NULL) {
  17. mallocError("board.squares", __FILE__, "board_alloc");
  18. }
  19. for (size_t i = 1; i < board.height_with_borders; i++) {
  20. for (size_t j = 1; j < board.width_with_borders; j++) {
  21. MAT_INDEX(board.squares, board.width_with_borders, i, j) = ' ';
  22. }
  23. }
  24. // Create border pattern
  25. const int width_with_borders_last_i = board.width_with_borders - 1;
  26. const int height_with_borders_last_i = board.height_with_borders - 1;
  27. // Vertical bars
  28. for (int i = 1; i < height_with_borders_last_i; i++) {
  29. MAT_INDEX(board.squares, board.width_with_borders, i, 0) = CHAR_BORDER_VER;
  30. MAT_INDEX(board.squares, board.width_with_borders, i, width_with_borders_last_i) = CHAR_BORDER_VER;
  31. }
  32. // Horizontal lines
  33. for (int j = 1; j < width_with_borders_last_i; j++) {
  34. MAT_INDEX(board.squares, board.width_with_borders, 0, j) = CHAR_BORDER_HOR;
  35. MAT_INDEX(board.squares, board.width_with_borders, height_with_borders_last_i, j) = CHAR_BORDER_HOR;
  36. }
  37. // Corners
  38. MAT_INDEX(board.squares, board.width_with_borders, 0, 0) = CHAR_BORDER_CORNER_TL;
  39. MAT_INDEX(board.squares, board.width_with_borders, height_with_borders_last_i, 0) = CHAR_BORDER_CORNER_BL;
  40. MAT_INDEX(board.squares, board.width_with_borders, 0, width_with_borders_last_i) = CHAR_BORDER_CORNER_TR;
  41. MAT_INDEX(board.squares, board.width_with_borders, height_with_borders_last_i, width_with_borders_last_i) = CHAR_BORDER_CORNER_BR;
  42. return board;
  43. }
  44. void board_free(Board* board) {
  45. free(board->squares);
  46. }
  47. bool board_coords_out_of_bounds(Board* board, const size_t x, const size_t y) {
  48. return x >= board->width_with_borders || y >= board->height_with_borders;
  49. }
  50. void board_set_square(
  51. Board* board,
  52. int x,
  53. int y,
  54. const char ch
  55. )
  56. {
  57. x++;
  58. y++;
  59. if (board_coords_out_of_bounds(board, x, y)) {
  60. fprintf(stderr, "ERROR: Board coords out of bounds: x: %d y: %d\n", x, y);
  61. exit(EXIT_FAILURE);
  62. }
  63. // assert(board_coords_out_of_bounds(board, x, y));
  64. MAT_INDEX(board->squares, board->width_with_borders, y, x) = ch;
  65. }
  66. void board_clear(Board* board) {
  67. for (size_t i = 0; i < board->height; i++) {
  68. for (size_t j = 0; j < board->width; j++) {
  69. // printf("Clearing board: i: %zu j: %zu\n", i, j);
  70. board_set_square(board, j, i, ' ');
  71. }
  72. }
  73. }
  74. void board_draw_snake(Board* board, Snake* snake) {
  75. BoardPiece part = {};
  76. for (size_t i = 0; i < snake->length; i++) {
  77. part = snake_get_part(snake, i);
  78. board_set_square(board, part.x, part.y, part.vis_char);
  79. }
  80. }
  81. void print_board(Board* board) {
  82. for (size_t i = 0; i < board->height_with_borders; i++) {
  83. for (size_t j = 0; j < board->width_with_borders; j++) {
  84. printf("%c", MAT_INDEX(board->squares, board->width_with_borders, i, j));
  85. }
  86. printf("\n");
  87. }
  88. }