aboutsummaryrefslogtreecommitdiff
path: root/voerbak/board.c
blob: 5bc4bd1cecca6aba782bfde917953c7c57611ee7 (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
#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;
}

Board* createCopy(Board* original) {
	Board *copy = createBoard(original->width, original->height);
	copy->board = original->board;
	return copy;
}

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;
}

int 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 pos;
		}
	}
	if (verbosity >= 0) {
		printf("e:full\n");
		fflush(stdout);
	}
	return -1;
}