summaryrefslogtreecommitdiffstats
path: root/hw2/big_judge.c
diff options
context:
space:
mode:
Diffstat (limited to 'hw2/big_judge.c')
-rw-r--r--hw2/big_judge.c47
1 files changed, 45 insertions, 2 deletions
diff --git a/hw2/big_judge.c b/hw2/big_judge.c
index 05077a7..7c029c0 100644
--- a/hw2/big_judge.c
+++ b/hw2/big_judge.c
@@ -25,9 +25,27 @@ typedef struct {
} JudgeData;
typedef struct {
+ int id;
long score;
} PlayerData;
+static int player_sort_cb (const void* aa, const void* bb) {
+ const PlayerData* a = aa;
+ const PlayerData* b = bb;
+ if (a->score > b->score) {
+ return -1;
+ } else if (a->score < b->score) {
+ return 1;
+ } else {
+ if (a->id > b->id) {
+ return 1;
+ } else if (a->id < b->id) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
static bool comb4 (int players[], long player_num) {
for (int i = 3; i >= 0; i--) {
if (players[i] + 1 <= player_num &&
@@ -148,6 +166,7 @@ int main (int argc, char* argv[]) {
struct pollfd* jpoll = xmalloc (sizeof (struct pollfd) * judge_num);
for (int i = 0; i < player_num; i++) {
+ pd[i].id = i + 1;
pd[i].score = 0;
}
for (int i = 0; i < judge_num; i++) {
@@ -194,6 +213,7 @@ int main (int argc, char* argv[]) {
int players[4] = {1, 2, 3, 4};
long completed_jobs = 0;
+ long sent_jobs = 0;
do {
char msg[20];
int msglen;
@@ -208,14 +228,37 @@ int main (int argc, char* argv[]) {
completed_jobs++;
}
- comp135_log (comp, "Send %d %d %d %d to judge %d",
+ comp135_log (comp, "Sending %d %d %d %d to judge %d",
players[0], players[1], players[2], players[3], jnext + 1);
write (jd[jnext].write, msg, msglen);
comp135_log (comp, "Judge %d is working now", jnext + 1);
jd[jnext].working = true;
-
+ sent_jobs++;
} while (comb4 (players, player_num));
+ for (;
+ completed_jobs < sent_jobs &&
+ nextscore (comp, jd, judge_num, pd, player_num, jpoll) >= 0;
+ completed_jobs++);
+
+ for (int i = 0; i < judge_num; i++) {
+ const char msg[] = "-1 -1 -1 -1\n";
+ const int msglen = STATIC_STRLEN (msg);
+ write (jd[i].write, msg, msglen);
+ }
+
+ qsort (pd, player_num, sizeof (PlayerData), player_sort_cb);
+
+ printf ("%d", pd[0].id);
+ for (int i = 1; i < player_num; i++) {
+ printf (" %d", pd[i].id);
+ }
+
+ for (int i = 0; i < player_num; i++) {
+ comp135_log (comp, "Rank %d = Player %d with %ld points",
+ i + 1, pd[i].id, pd[i].score);
+ }
+
comp135_destroy (comp);
return 0;