aboutsummaryrefslogtreecommitdiff
path: root/src/ct1986.c
blob: 0872ca744db35a15ae0eac229cad607ce83f3ac2 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
	ct1986, an interface to the ctak library designed to be embedded on
	a Raspberry Pi Zero

	Copyright (C) 2021, tslil clingman

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <string.h>

#include <tak.h>
#include <negamax.h>
#include <lcdlib.h>

static char *gamelog = 0;
static int human;

static void
append_to_gamelog(const char *line, const uint8_t win_line) {
	// I _could_ dynamically compute the size but ... don't let
	// `perfect' be the enemy of `good' ?
	char prepend[8];

	if (win_line) {
		prepend[0] = '\n';
		prepend[1] = 0;
	} else if (ply & 1) {
		snprintf(prepend, 7, "%s%d. ",
						 (ply == 1) ? "" : "\n", ply/2+1);
	} else {
		strcpy(prepend, " ");
	}
	// Make room for this line
	gamelog = realloc(gamelog,
										strlen(gamelog)
										+ strlen(prepend)
										+ strlen(line) + 1);
	strcat(gamelog,prepend);
	strcat(gamelog,line);
}

static void
end_game(char *line, char *win) {
	append_to_gamelog(line, 0);
	append_to_gamelog(win, 1);
	lcd_printf_line(L_SCROLL, "Game over: %s", win);
}

static int
handle_turn(char *line) {
	// Track win state
	uint8_t new_win = (won == 0xFF);
	switch (do_ptn(line)) {
		// Errors
		case PTN_INVALID:  { lcd_put_line(L_SCROLL, "Invalid PTN.");  break; }
		case ACT_ILLEGAL:  { lcd_put_line(L_SCROLL, "Illegal ply.");  break; }
		case ACT_OVERFLOW: { lcd_put_line(L_SCROLL, "Overflow.");     break; }
			// Game has ended
		case GAME_END: {
			// Did it end this turn?
			if (new_win) {
				switch (won) {
					case WIN_DRAW:       { end_game(line,"1/2-1/2"); break; }
					case WIN_FLAT_BLACK: { end_game(line,"0-F");     break; }
					case WIN_FLAT_WHITE: { end_game(line,"F-0");     break; }
					case WIN_ROAD_BLACK: { end_game(line,"0-R");     break; }
					case WIN_ROAD_WHITE: { end_game(line,"R-0");     break; }
				}
				return EXIT_SUCCESS;
			} else {
				lcd_put_line(L_SCROLL, "Game over.");
				break;
			}
		}
			// Valid, append to game log
		default: {
			append_to_gamelog(line, 0);
			return EXIT_SUCCESS;
		}
	}
	return EXIT_FAILURE;
}

static void
new_game(uint8_t size) {
	reset_state(size);
	lcd_put_line(L_SCROLL, "New 5x5 game.");
	if (gamelog) gamelog = realloc(gamelog, sizeof(char));
	else gamelog = malloc(sizeof(char));
	gamelog[0] = 0;
}

// Set up output function for negamax
static uint8_t perc;

inline void
negamax_display_progress(const uint8_t depth, const uint32_t length) {
	if (depth == 0) {
		perc++;
		lcd_printf_line(L_OVERWRITE, "Computing: %d%%", (perc*100)/length);
	}
}

static int
negamax_turn() {
	// Prepare progress bar
	lcd_put_line(L_SCROLL, "Computing: 0%");
	// Run the minimax
	perc = 0;
	float minimax = negamax_generate();
	// Failed to find a move?
	if (minimax < -infty) {
		lcd_printf_line(L_SCROLL, "%s concedes!",
										(ply & 1) ? "Black" : "White");
		return EXIT_FAILURE;
	} else {
		lcd_printf_line(L_SCROLL, "%s: %s",
										(ply & 1) ? "Black" : "White",
										negamax_ptn);
		return handle_turn(negamax_ptn);
	}
}

static void
do_game_log(void) {
	lcd_put_line(L_SCROLL, "Not implemented.");
}

static int
input_is_not_turn(const char *line) {
	switch (line[0]) {
		case 'l': { do_game_log(); break; }
		case 'n': { new_game(5);   break; }
		case 's': {
			negamax_search_depth = line[1] - '0';
			lcd_printf_line(L_SCROLL, "Search depth: %d",
											negamax_search_depth);
			break;
		}
		case 'B': { human = 1; new_game(5); break; }
		case 'W': { human = 0; new_game(5); break; }
		default: return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}

const char* license = "ct1986, an interface to the ctak library designed to be embedded on a Raspberry Pi Zero\n\
\n\
Copyright (C) 2021, tslil clingman\n\
\n\
This program comes with ABSOLUTELY NO WARRANTY; and is made available under the terms of the GNU GPL v3 license. This is free software, and you are welcome to redistribute it under certain conditions; see COPYING for details.\n";

int main(int argc, char **argv) {
	(void)(argc);
	(void)(argv);

	puts(license);

	if (lcd_begin() != EXIT_SUCCESS)
		return EXIT_FAILURE;


	negamax_search_depth = 3;
	new_game(5);
	negamax_init(5);

	char *line = NULL;
	ssize_t read;
	size_t alloc_size;

	human = 0;
	for (int playing = 1; playing;) {
		while (human == 0) {
			if (ply & 1) lcd_put_line(L_SCROLL, "Black: ");
			else lcd_put_line(L_SCROLL, "White: ");
			read = getline(&line, &alloc_size, stdin);
			if (read > 0) {
				line[read - 1] = 0;
				if (input_is_not_turn(line)) {
					int r = handle_turn(line);
					if (r == EXIT_SUCCESS && won == 0xFF) {
						human = 1;
					}
				}
				free(line);
				line = NULL;
			} else {
				playing = 0;
				human = 1;
			}
		}
		if (playing) {
			negamax_turn();
			human = 0;
		}
	}

	free(line);
	free(gamelog);
	negamax_free();

	lcd_end();
	return EXIT_SUCCESS;
}