aboutsummaryrefslogtreecommitdiff
path: root/voerbak/voerbak.c
blob: 0b564c0549f19601d39fb5ec5ef138fe2e6b1aa2 (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
#include <stdbool.h>
#include <memory.h>

#include "voerbak.h"
#include "win.h"
#include "board.h"
#include "messages.h"
#include "argparse.h"
#include "solvers.h"

#define EMPTY ""

int verbosity = 0;

int main(int argc, char* argv[]) {
	struct arguments arguments = argparse(argc, argv);
	verbosity = arguments.verbosity;

	Board *gameBoard = createBoard(arguments.width, arguments.height);

	bool cpu_2 = strlen(arguments.solver) > 0;
	bool player_1 = true;
	int move = 0;
	char* message = malloc(1); // this is weird and i don't understand it but it prevents a segmentation fault or something
	strcpy(message, EMPTY);
	while (scanf("%d", &move) == 1 || scanf("%s", message) == 1) {
		if (strlen(message) != 0) {
			parseMessage(message);

			strcpy(message, EMPTY); // clear message
			continue;
		}

		move:

		if (move == 0) break;
		if (move < 1 || move > gameBoard->width) continue;

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

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

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

		printBoard(gameBoard);

		if (cpu_2 && !player_1) {
			move = cpuMove(gameBoard, arguments.solver);
			goto move;
		}
	}

	return 0;
}