diff options
author | lonkaars <l.leblansch@gmail.com> | 2021-02-23 14:14:50 +0100 |
---|---|---|
committer | lonkaars <l.leblansch@gmail.com> | 2021-02-23 14:14:50 +0100 |
commit | 1a55f52bb79b609cd850a77e2f7a9fdc6b4fbf6b (patch) | |
tree | a28604c4e89be863954baa887e758bd0419d2e53 /voerbak/win.c | |
parent | 34c9fa5176f0d2c18457a72085f5803c2d616cc6 (diff) |
split up code into seperate files
Diffstat (limited to 'voerbak/win.c')
-rw-r--r-- | voerbak/win.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/voerbak/win.c b/voerbak/win.c new file mode 100644 index 0000000..c995060 --- /dev/null +++ b/voerbak/win.c @@ -0,0 +1,53 @@ +#include <stdio.h> +#include <stdbool.h> + +#include "voerbak.h" + +int winCheckRecursive(Board *b, int pos, int direction, int d_index, int currentLength) { + int newPos = pos + direction; + if (newPos > b->length - 1 || newPos < 0) return currentLength; + int row = pos % b->width; + if (row == b->width && d_index >= 1 && d_index <= 3) return currentLength; + if (row == 0 && d_index >= 5 && d_index <= 7) return currentLength; + if (b->board[newPos] != b->board[pos]) return currentLength; + return winCheckRecursive(b, newPos, direction, d_index, currentLength + 1); +} + +bool checkWin(Board *b, int pos) { + int directions[8] = { + b->width, // north + b->width + 1, // northeast + 1, // east + -b->width + 1, // southeast + -b->width, // south + -b->width -1, // southwest + -1, // west + b->width -1 // northwest + }; + + int values[8]; + for (int i = 0; i < 8; i++) + values[i] = winCheckRecursive(b, pos, directions[i], i, 0); + + int joinedValues[4] = { + values[0] + values[4], + values[1] + values[5], + values[2] + values[6], + values[3] + values[7] + }; + + bool won = false; + for (int i = 0; i < 4; i++) { + if (joinedValues[i] >= 3) { + won = won || true; + + int start_pos = pos + directions[i+0] * values[i+0]; + int end_pos = pos + directions[i+4] * values[i+4]; + printf("w:%d-%d\n", start_pos, end_pos); + fflush(stdout); + } + } + + return won; +} + |