hasty bugfix and moved board functions to own directory
This commit is contained in:
31
src/board/board.c
Normal file
31
src/board/board.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include "../globals.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
|
||||
void setBoardBorders(char board[][brdInfo.x]) {
|
||||
for (int i = 0; i < brdInfo.y; i++) {
|
||||
for (int j = 0; j < brdInfo.x; j++) {
|
||||
if (i == 0 || i == brdInfo.y - 1)
|
||||
board[i][j] = '-';
|
||||
else if (j == 0 || j == brdInfo.x - 1)
|
||||
board[i][j] = '|';
|
||||
else
|
||||
board[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
board[0][0] = '+';
|
||||
board[brdInfo.y - 1][0] = '+';
|
||||
board[0][brdInfo.x - 1] = '+';
|
||||
board[brdInfo.y - 1][brdInfo.x - 1] = '+';
|
||||
}
|
||||
|
||||
void printBoard(char board[][brdInfo.x]) {
|
||||
for (int i = 0; i < brdInfo.y; i++) {
|
||||
for (int j = 0; j < brdInfo.x; j++) {
|
||||
printf("%c", board[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
11
src/board/board.h
Normal file
11
src/board/board.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef BOARD_H_
|
||||
#define BOARD_H_
|
||||
|
||||
#include "../globals.h"
|
||||
|
||||
extern boardInfo brdInfo;
|
||||
|
||||
void setBoardBorders(char board[][brdInfo.x]);
|
||||
void printBoard(char board[][brdInfo.x]);
|
||||
|
||||
#endif
|
||||
17
src/main.c
17
src/main.c
@@ -8,6 +8,7 @@
|
||||
#include <stdbool.h>
|
||||
#include "./utils/utils.h"
|
||||
#include "./snake/snake.h"
|
||||
#include "./board/board.h"
|
||||
#include "globals.h"
|
||||
|
||||
const char SNAKE_VIS = '#';
|
||||
@@ -50,21 +51,7 @@ int main() {
|
||||
tcsetattr(STDIN_FILENO, 0, &attr);
|
||||
|
||||
// Game board setup
|
||||
for (int i = 0; i < brdInfo.y; i++) {
|
||||
for (int j = 0; j < brdInfo.x; j++) {
|
||||
if (i == 0 || i == brdInfo.y - 1)
|
||||
board[i][j] = '-';
|
||||
else if (j == 0 || j == brdInfo.x - 1)
|
||||
board[i][j] = '|';
|
||||
else
|
||||
board[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
board[0][0] = '+';
|
||||
board[brdInfo.y - 1][0] = '+';
|
||||
board[0][brdInfo.x - 1] = '+';
|
||||
board[brdInfo.y - 1][brdInfo.x - 1] = '+';
|
||||
setBoardBorders(board);
|
||||
|
||||
int points = 0;
|
||||
int gameSpeed = 0;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "snake.h"
|
||||
#include "../board/board.h"
|
||||
|
||||
extern char SNAKE_VIS;
|
||||
extern boardInfo brdInfo;
|
||||
@@ -107,6 +108,23 @@ void addSnakePart(char board[][brdInfo.x], snakePart* head) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/*
|
||||
Untested fix to a bug where the new part
|
||||
would replace a part of the board border.
|
||||
*/
|
||||
if (newTail->x > plBrdInfo.xe) {
|
||||
newTail->x = plBrdInfo.xs;
|
||||
} else if (newTail->x < plBrdInfo.xs) {
|
||||
newTail->x = plBrdInfo.xe;
|
||||
}
|
||||
|
||||
if (newTail->y > plBrdInfo.ye) {
|
||||
newTail->y = plBrdInfo.ys;
|
||||
} else if (newTail->y < plBrdInfo.ys) {
|
||||
newTail->y = plBrdInfo.ye;
|
||||
}
|
||||
// bugfix end
|
||||
|
||||
newTail->next = NULL;
|
||||
|
||||
tail->next = newTail;
|
||||
|
||||
@@ -39,6 +39,24 @@ void sleep_ms(const int ms) {
|
||||
usleep(ms * 1000);
|
||||
}
|
||||
|
||||
void setBoardBorders(char board[][brdInfo.x]) {
|
||||
for (int i = 0; i < brdInfo.y; i++) {
|
||||
for (int j = 0; j < brdInfo.x; j++) {
|
||||
if (i == 0 || i == brdInfo.y - 1)
|
||||
board[i][j] = '-';
|
||||
else if (j == 0 || j == brdInfo.x - 1)
|
||||
board[i][j] = '|';
|
||||
else
|
||||
board[i][j] = ' ';
|
||||
}
|
||||
}
|
||||
|
||||
board[0][0] = '+';
|
||||
board[brdInfo.y - 1][0] = '+';
|
||||
board[0][brdInfo.x - 1] = '+';
|
||||
board[brdInfo.y - 1][brdInfo.x - 1] = '+';
|
||||
}
|
||||
|
||||
void printBoard(char board[][brdInfo.x]) {
|
||||
for (int i = 0; i < brdInfo.y; i++) {
|
||||
for (int j = 0; j < brdInfo.x; j++) {
|
||||
|
||||
@@ -10,6 +10,7 @@ int randomInt(const int start, const int end, const unsigned int seed);
|
||||
int randomX(const clock_t initClock);
|
||||
int randomY(const clock_t initClock);
|
||||
void sleep_ms(const int ms);
|
||||
void setBoardBorders(char board[][brdInfo.x]);
|
||||
void printBoard(char board[][brdInfo.x]);
|
||||
|
||||
#endif // UTILS_H_
|
||||
|
||||
Reference in New Issue
Block a user