aboutsummaryrefslogtreecommitdiff
path: root/voerbak/voerbak.c
blob: 22c4a5d6fe71473817cd428c885873d507f2bc01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <stdbool.h>

struct Board {
	int width;
	int height;
	int* board;
};

void printBoard(struct Board *b) {
	for (int i = 0; i < b->width * b->height; i++)
		printf("%d", b->board[i]);
	printf("\n");
	fflush(stdout);
}

int recursiveSolve(struct Board *b, int pos, int checkFor, int direction, int d_index, int currentLength) {
	int newPos = pos + direction;
	if (newPos > b->width * b->height - 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] != checkFor) return currentLength;
	return recursiveSolve(b, newPos, checkFor, direction, d_index, currentLength + 1);
}

bool checkWin(struct 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] = recursiveSolve(b, pos, b->board[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;
}

bool boardFull(struct Board *b) {
	for (int i = 0; i < b->width * b->height; i++)
		if (b->board[i] == 0) return false;
	return true;
}

bool dropFisje(struct 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
}

int main() {
	int width, height;
	scanf("%d %d", &width, &height);
	struct Board *gameBoard = malloc(sizeof(struct Board));
	gameBoard->board = malloc(sizeof(int) * (width * height - 1));
	gameBoard->width = width;
	gameBoard->height = height;

	/* memset(gameBoard->board, 0, sizeof gameBoard->board); */

	bool player_1 = true;
	int move = 0;
	while (scanf("%d", &move) == 1) {
		if (move == 0) break;
		if (move < 1 || move > width) continue;

		bool dropSuccess = dropFisje(gameBoard, move - 1, player_1 + 1);

		player_1 = player_1 ^ dropSuccess; // only flip turns on successful drop
		printf("m:%s\n", player_1 ? "true" : "false");
		fflush(stdout);

		if (boardFull(gameBoard)) {
			printf("d:full\n");
			fflush(stdout);
		}

		printBoard(gameBoard);
	}

	return 0;
}