-
Notifications
You must be signed in to change notification settings - Fork 6
/
hvc_match.cpp
100 lines (95 loc) · 3.25 KB
/
hvc_match.cpp
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
#include <time.h>
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include "common_define.h"
#include "hvc_match.h"
#include "sandbox.h"
const int BUF_LEN = 1024;
static std::string exit_flag_2_str(ExitFlagType exit_type) {
switch (exit_type) {
case EXIT_NORMAL:
return "The_opponent_exited_normally.";
case EXIT_RF:
return "The_opponent_called_restricted_function.";
case EXIT_TLE:
return "The_opponent_exceeded_time_limit.";
case EXIT_MLE:
return "The_opponent_exceeded_memory_limit.";
case EXIT_RE:
return "The_opponent_got_Runtime_Error.";
default:
return "The_opponent_exited_by_unknown_reason.";
}
}
static std::string winner_to_str(int winner) {
if (winner == 0)
return "Draw.";
else if (winner == 1)
return "Player_1_win.";
else
return "Player_2_win.";
}
void HVCMatch::Start() {
m_start_time = time(NULL);
m_judge->Run(false, true);
m_computer->Run();
char buf[BUF_LEN], tmp_buf[BUF_LEN];
while (1) {
memset(buf, 0, sizeof(buf));
ExitFlagType tmp;
if (m_judge->Recv(buf, BUF_LEN-1, tmp) == 0) {
fprintf(stderr, "The judge crashed.");
break;
}
fprintf(stderr, "The judge said {%s}\n", buf);
if (buf[0] == '>') {
int dst = buf[1] - '1';
if (dst == 0) {
// to human: display the computer's move
// but ignore the first "first"
if (strncmp(buf+4, "first", 5) != 0) {
printf("%s\n", buf+4);
fflush(stdout); // neccessary?
}
} else if (dst == 1) {
// computer
m_computer->Send(buf+4);
} else assert(false);
} else if (buf[0] == '<') {
int src = buf[1] - '1';
if (src == 0) {
// from human: request move from browser
// scanf("%s", tmp_buf);
fgets(tmp_buf, BUF_LEN-1, stdin);
int tmp_len = strlen(tmp_buf);
if (tmp_buf[tmp_len-1] == '\n') tmp_buf[tmp_len-1] = 0;
m_judge->Send(tmp_buf);
} else if (src == 1) {
ExitFlagType exit_type;
if (m_computer->Recv(buf, BUF_LEN-1, exit_type) == 0) {
fprintf(stderr, "Computer exited, type: %d\n", (int)exit_type);
m_winner = (1 - src) + 1;
fprintf(stderr, "the winner is %d\n", m_winner);
printf(": %d %s %s\n", m_winner,
winner_to_str(m_winner).c_str(),
exit_flag_2_str(exit_type).c_str());
fflush(stdout);
break;
}
m_judge->Send(buf);
} else assert(false);
} else if (buf[0] == '*') {
printf("*\n");
fflush(stdout);
} else if (isdigit(buf[0])) {
char res_str[BUF_LEN], reason[BUF_LEN];
sscanf(buf, "%d %s %s", &m_winner, res_str, reason);
printf(": %d %s %s\n", m_winner, res_str, reason);
fflush(stdout);
break;
} // ignore '+'
}
m_end_time = time(NULL);
};