summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pd_client.pl5
-rw-r--r--pd_server.c79
2 files changed, 48 insertions, 36 deletions
diff --git a/pd_client.pl b/pd_client.pl
index 7b32e3a..5e6c5b0 100644
--- a/pd_client.pl
+++ b/pd_client.pl
@@ -17,13 +17,14 @@ do(_, _, _, _, [e|_]) :- !.
do(StreamIn, StreamOut, Module, ModuleState, Hist) :-
call(Module, Hist, ModuleDecision, ModuleState, NewModuleState),
write(StreamOut, ModuleDecision), flush_output(StreamOut),
+ format('Own choose:\t~w\n', [ModuleDecision]),
loop(StreamIn, StreamOut, Module, NewModuleState, Hist).
loop(StreamIn, StreamOut, Module, ModuleState, Hist) :-
get_code(StreamIn, ChoiceCode), byte_to_atom(ChoiceCode, Choice),
- write(Choice),
+ format('Opponent chose:\t~w\n', [Choice]),
do(StreamIn, StreamOut, Module, ModuleState, [Choice|Hist]).
% -1 = EOF
byte_to_atom(-1, e):-!.
-byte_to_atom(Byte, Atom):-name(Atom, [Byte]).
+byte_to_atom(Byte, Atom):-atom_codes(Atom, [Byte]).
diff --git a/pd_server.c b/pd_server.c
index d0b2315..b8ff6f0 100644
--- a/pd_server.c
+++ b/pd_server.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
@@ -14,25 +15,33 @@ enum {
};
int pay(int a, int b)
-{
- if (a == COORPERATION && b == DEFECTION) return 1;
- if (a == DEFECTION && b == DEFECTION) return 2;
- if (a == COORPERATION && b == COORPERATION) return 3;
- if (a == DEFECTION && b == COORPERATION) return 4;
+{
+ assert(a == COORPERATION || a == DEFECTION);
+ assert(b == COORPERATION || b == DEFECTION);
+
+ if (a == COORPERATION && b == DEFECTION) return 1;
+ if (a == DEFECTION && b == DEFECTION) return 2;
+ if (a == COORPERATION && b == COORPERATION) return 3;
+ if (a == DEFECTION && b == COORPERATION) return 4;
- exit(EXIT_FAILURE);
-}
+ exit(EXIT_FAILURE);
+}
int
main(int argc, char *argv[])
{
- int sock, player[2], read_fd;
+ int sock, player[2];
struct sockaddr_in serv_addr;
int port = 8068;
- int rounds, i;
+ int a_payment = 0, b_payment = 0, rounds, i;
int optval = 1;
+ char *endptr;
- rounds = atoi(argv[1]);
+ errno = 0;
+ if (argc < 2 || (rounds = strtol(argv[1], NULL, 10)) <= 0 || errno!=0) {
+ fprintf(stderr, "usage: %s number-of-rounds\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
@@ -50,34 +59,36 @@ main(int argc, char *argv[])
}
listen(sock, 2);
- for (i = 0; i < 2; ++i) {
- player[i] = accept(sock, NULL, NULL);
- }
- int a_payment = 0, b_payment = 0;
- for (i = 0; i < rounds; ++i) {
- char a, b;
- read(player[0], &a, 1);
- read(player[1], &b, 1);
-
- if (a != 'c' && a != 'd') {
- printf("a sent incorrect char: %c\n", a);
- continue;
- }
- if (b != 'c' && b != 'd') {
- printf("b sent incorrect char: %c\n", b);
- continue;
+ for (;;) {
+ for (i = 0; i < 2; ++i) {
+ player[i] = accept(sock, NULL, NULL);
}
+ for (i = 0; i < rounds; ++i) {
+ char a, b;
+ read(player[0], &a, 1);
+ read(player[1], &b, 1);
- a_payment += pay(a, b);
- b_payment += pay(b, a);
+ if (a != 'c' && a != 'd') {
+ printf("a sent incorrect char: %c\n", a);
+ continue;
+ }
+ if (b != 'c' && b != 'd') {
+ printf("b sent incorrect char: %c\n", b);
+ continue;
+ }
- printf("A [%c]: %d Euro\tB [%c]: %d Euro\t\t\tA: %d\tB: %d\n",
- a, pay(a, b), b, pay(b, a), a_payment, b_payment);
+ a_payment += pay(a, b);
+ b_payment += pay(b, a);
- write(player[0], &b, 1);
- write(player[1], &a, 1);
+ printf("A [%c]: %d Euro\tB [%c]: %d Euro\t\t\t"
+ "A: %d\tB: %d\n",
+ a, pay(a,b), b, pay(b,a), a_payment, b_payment);
+
+ write(player[0], &b, 1);
+ write(player[1], &a, 1);
+ }
+ close(player[0]);
+ close(player[1]);
}
- close(player[0]);
- close(player[1]);
close(sock);
}