aboutsummaryrefslogtreecommitdiff
path: root/console/voerbak.c
diff options
context:
space:
mode:
Diffstat (limited to 'console/voerbak.c')
-rw-r--r--console/voerbak.c56
1 files changed, 55 insertions, 1 deletions
diff --git a/console/voerbak.c b/console/voerbak.c
index 43eb6c6..1f47351 100644
--- a/console/voerbak.c
+++ b/console/voerbak.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <memory.h>
#include <stdbool.h>
@@ -21,12 +22,63 @@ void printBoard(int board[], int width, int height) {
}
}
+int recursiveSolve(int board[], int width, int height, int pos, int checkFor, int direction, int currentLength) {
+ printf("recursiveSolve call\n");
+ int overflow = (pos % width) + direction;
+ printf("overflow: %d\n", overflow);
+ if (overflow == width || overflow == -1)
+ return currentLength;
+ int newPos = pos + direction;
+ printf("newPos: %d\n", newPos);
+ if (newPos < 0 || newPos > width * height - 1)
+ return currentLength;
+ if (board[newPos] != checkFor)
+ return currentLength;
+ printf("recursion increase\n");
+ /* exit(0); */
+ return recursiveSolve(board, width, height, pos, checkFor, direction, currentLength + 1);
+}
+
+bool checkWin(int board[], int width, int height, int pos) {
+ int directions[8] = {
+ width, // north
+ width + 1, // northeast
+ 1, // east
+ -width + 1, // southeast
+ -width, // south
+ -width -1, // southwest
+ -1, // west
+ width -1 // northwest
+ };
+
+ int values[8];
+ for (int i = 0; i < 8; i++)
+ values[i] = recursiveSolve(board, width, height, pos, board[pos], directions[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 = true;
+ break;
+ }
+ }
+
+ return won;
+}
+
void dropFisje(int board[], int width, int height, int column, int disc) {
for (int row = 0; row < height; row++) {
int pos = column + row * width;
if (board[pos] == 0) {
board[pos] = disc;
- /* bool won = checkWin(board, width. height, pos); */
+ bool won = checkWin(board, width, height, pos);
/* printf("%d", won); */
return;
}
@@ -44,6 +96,8 @@ int main() {
int move = 0;
while (scanf("%d", &move) == 1) {
dropFisje(board, width, height, move - 1, player_1 + 1);
+ printf("New board:\n");
+ printBoard(board, width, height);
player_1 = !player_1;
}
printBoard(board, width, height);