diff options
Diffstat (limited to 'voerbak/board.c')
-rw-r--r-- | voerbak/board.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/voerbak/board.c b/voerbak/board.c new file mode 100644 index 0000000..8008db4 --- /dev/null +++ b/voerbak/board.c @@ -0,0 +1,41 @@ +#include <memory.h> + +#include "board.h" +#include "win.h" + +Board* createBoard(int width, int height) { + Board *gameBoard = malloc(sizeof(Board)); + gameBoard->board = malloc(sizeof(int) * (width * height - 1)); + gameBoard->width = width; + gameBoard->height = height; + gameBoard->length = width * height; + return gameBoard; +} + +void printBoard(Board *b) { + for (int i = 0; i < b->length; i++) + printf("%d", b->board[i]); + printf("\n"); + fflush(stdout); +} + +bool boardFull(Board *b) { + for (int i = 0; i < b->length; i++) + if (b->board[i] == 0) return false; + return true; +} + +bool dropFisje(Board *b, int column, int disc) { + for (int row = 0; row < b->height; row++) { + int pos = column + row * b->width; + if (b->board[pos] == 0) { + b->board[pos] = disc; + bool won = checkWin(b, pos); + return true; // success + } + } + printf("e:full\n"); + fflush(stdout); + return false; // unsuccessful drop on board full +} + |