-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (94 loc) · 2.17 KB
/
Copy pathmain.cpp
File metadata and controls
103 lines (94 loc) · 2.17 KB
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
#include <stdio.h>
#include <string.h>
#include "MyAI.h"
// commands enumerate
enum COMMANDS{
PROTOCOL_VERSION = 0, // 0
NAME, // 1
VERSION, // 2
KNOWN_COMMAND, // 3
LIST_COMMANDS, // 4
QUIT, // 5
BOARDSIZE, // 6
RESET_BOARD, // 7
NUM_REPETITION, // 8
NUM_MOVES_TO_DRAW, // 9
MOVE, // 10
FLIP, // 11
GENMOVE, // 12
GAME_OVER, // 13
READY, // 14
TIME_SETTINGS, // 15
TIME_LEFT, // 16
SHOWBOARD // 17
};
// function pointer array
static bool (MyAI::*functions[])(const char* [], char*) = {
&MyAI::protocol_version,
&MyAI::name,
&MyAI::version,
&MyAI::known_command,
&MyAI::list_commands,
&MyAI::quit,
&MyAI::boardsize,
&MyAI::reset_board,
&MyAI::num_repetition,
&MyAI::num_moves_to_draw,
&MyAI::move,
&MyAI::flip,
&MyAI::genmove,
&MyAI::game_over,
&MyAI::ready,
&MyAI::time_settings,
&MyAI::time_left,
&MyAI::showboard,
&MyAI::init_board
};
int main(){
char read[1024], write[1024], output[2048], *token;
const char *data[50];
int id;
bool isFailed;
MyAI myai;
do{
// read command
if(fgets(read, 1024, stdin) == NULL){ // ERROR
fprintf(stderr, "READ COMMAND ERROR\n");
exit(1);
}
fprintf(stderr, "%s", read);
// remove newline(\n)
read[strlen(read) - 1] = '\0';
// get command id
token = strtok(read, " ");
sscanf(token, "%d", &id);
// get command name
token = strtok(NULL, " ");
// get command data
int i = 0;
while((token = strtok(NULL, " ")) != NULL){
data[i++] = token;
}
write[0] = '\0'; // empty the char array
isFailed = (myai.*functions[id])(data, write);
if(strlen(write) > 0){
if(isFailed){
sprintf(output, "?%d %s\n", id, write);
}else{
sprintf(output, "=%d %s\n", id, write);
}
}else{
if(isFailed){
sprintf(output, "?%d\n", id);
}else{
sprintf(output, "=%d\n", id);
}
}
fprintf(stdout, "%s", output);
fprintf(stderr, "%s", output);
// important, do not delete
fflush(stdout);
fflush(stderr);
}while(id != QUIT);
return 0;
}