summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLAN-TW <lantw44@gmail.com>2013-11-16 18:34:11 +0800
committerLAN-TW <lantw44@gmail.com>2013-11-16 18:34:11 +0800
commit8018ec1fed3400d46d2de89236c5c9397deadc8f (patch)
tree4ae8dfc18754dd709af8e31e81e3e87c60d1edf3
parent0a2f61bc3b40a26fb192555f404a32f206d112a4 (diff)
downloadsp2013-8018ec1fed3400d46d2de89236c5c9397deadc8f.tar.gz
sp2013-8018ec1fed3400d46d2de89236c5c9397deadc8f.tar.zst
sp2013-8018ec1fed3400d46d2de89236c5c9397deadc8f.zip
HW2: 加入測試用與實際可用的 player
-rw-r--r--.gitignore1
-rw-r--r--hw2/judge.c4
-rw-r--r--hw2/player.c85
-rwxr-xr-xhw2/player_13
-rwxr-xr-xhw2/player_211
-rwxr-xr-xhw2/player_311
-rwxr-xr-xhw2/player_411
7 files changed, 126 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index b5d8517..a1d4086 100644
--- a/.gitignore
+++ b/.gitignore
@@ -43,3 +43,4 @@ hw1/read_server
hw1/write_server
hw1/sp2013_hw1_slide.pdf
hw1/sp2013_hw1_spec.pdf
+hw2/player_[0-9]*
diff --git a/hw2/judge.c b/hw2/judge.c
index 51c195b..5e1da77 100644
--- a/hw2/judge.c
+++ b/hw2/judge.c
@@ -105,12 +105,14 @@ int main (int argc, char* argv[]) {
argv[0], ffd[i].fname, strerror (errno));
return 3;
}
+ xfaddfd (fdr, FD_CLOEXEC);
int fdw = open (ffd[i].fname, O_WRONLY | O_NONBLOCK);
if (fdw < 0) {
fprintf (stderr, "%s: cannot open `%s\' for writing: %s\n",
argv[0], ffd[i].fname, strerror (errno));
return 3;
}
+ xfaddfd (fdw, FD_CLOEXEC);
if (i) {
ffd[i].fd = fdw;
@@ -205,6 +207,8 @@ int main (int argc, char* argv[]) {
char* plthis = xgetres (plname);
char* plkey = xsprintf ("%ld", ffd[i].key);
char plid[2] = { c, '\0' };
+ close (STDIN_FILENO);
+ close (STDOUT_FILENO);
execl (plthis, plname, judgename, plid, plkey, (char*)NULL);
fprintf (stderr, "Cannot execl `%s\': %s\n",
plthis, strerror (errno));
diff --git a/hw2/player.c b/hw2/player.c
new file mode 100644
index 0000000..03a2f58
--- /dev/null
+++ b/hw2/player.c
@@ -0,0 +1,85 @@
+/* b01902062 藍挺瑋 */
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "xwrap.h"
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+int main (int argc, char* argv[]) {
+ if (argc < 4) {
+ fprintf (stderr, "Usage: %s judge_id player_index random_key\n", argv[0]);
+ return 1;
+ }
+
+ char* judge_id = argv[1];
+ char* player_index = argv[2];
+ char* random_key = argv[3];
+
+ pid_t mypid = getpid();
+ srandom (mypid + getppid () + getpgid (mypid) + getsid (mypid) + time (NULL));
+
+ FILE* rf = NULL;
+ FILE* wf = NULL;
+ char* fifoname;
+ int oth[4];
+ int cnt[3] = { 0, 0, 0 };
+ int i = 0;
+
+ do {
+ if (wf == NULL) {
+ wf = fopen (fifoname = xstrcat ("judge", judge_id, ".FIFO", (char*)NULL), "wb");
+ if (wf == NULL) {
+ fprintf (stderr, "%s: Cannot open `%s\': %s\n", argv[0], fifoname, strerror (errno));
+ return 1;
+ }
+ free (fifoname);
+ }
+
+ const char* val = "135";
+ if (i) {
+ fprintf (wf, "%s %s %c\n", player_index, random_key, val[random () % 3]);
+ } else {
+ int hismin = INT_MAX;
+ int choice = 0;
+
+ for (int j = 0; j < 4; j++) {
+ switch (oth[j]) {
+ case 1: cnt[0]++; break;
+ case 3: cnt[1]++; break;
+ case 5: cnt[2]++; break;
+ }
+ }
+
+ for (int j = 0; j < 3; j++) {
+ if (cnt[j] < hismin) {
+ choice = j;
+ hismin = cnt[j];
+ }
+ }
+
+ fprintf (wf, "%s %s %c\n", player_index, random_key, val[choice]);
+ }
+ fflush (wf);
+
+ if (rf == NULL) {
+ rf = fopen (fifoname = xstrcat ("judge", judge_id,
+ "_", player_index, ".FIFO", (char*)NULL), "rb");
+ if (rf == NULL) {
+ fprintf (stderr, "%s: Cannot open `%s\': %s\n", argv[0], fifoname, strerror (errno));
+ return 1;
+ }
+ free (fifoname);
+ }
+ } while (++i < 20 && fscanf (rf, "%d %d %d %d", &oth[0], &oth[1], &oth[2], &oth[3]) == 4);
+
+ return 0;
+}
diff --git a/hw2/player_1 b/hw2/player_1
new file mode 100755
index 0000000..a8ffa70
--- /dev/null
+++ b/hw2/player_1
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+sleep 1000
diff --git a/hw2/player_2 b/hw2/player_2
new file mode 100755
index 0000000..c1d14f4
--- /dev/null
+++ b/hw2/player_2
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+exec 1> judge${1}.FIFO
+exec 0< judge${1}_${2}.FIFO
+
+echo ${2} ${3} 5
+
+while read oneline
+do
+ echo ${2} ${3} 5
+done
diff --git a/hw2/player_3 b/hw2/player_3
new file mode 100755
index 0000000..5dfb210
--- /dev/null
+++ b/hw2/player_3
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+exec 1> judge${1}.FIFO
+exec 0< judge${1}_${2}.FIFO
+
+echo ${2} ${3} 3
+
+while read oneline
+do
+ echo ${2} ${3} 3
+done
diff --git a/hw2/player_4 b/hw2/player_4
new file mode 100755
index 0000000..78960f1
--- /dev/null
+++ b/hw2/player_4
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+exec 1> judge${1}.FIFO
+exec 0< judge${1}_${2}.FIFO
+
+echo ${2} ${3} 1
+
+while read oneline
+do
+ echo ${2} ${3} 1
+done