From 3656d3e8bfc7f074216191fbdc54d0c602a2d9ea Mon Sep 17 00:00:00 2001 From: Benjamin Franzke Date: Thu, 8 Nov 2012 15:01:53 +0100 Subject: Add prisoners dilemma prolog client --- pd_client.pl | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 pd_client.pl diff --git a/pd_client.pl b/pd_client.pl new file mode 100644 index 0000000..e0a83f3 --- /dev/null +++ b/pd_client.pl @@ -0,0 +1,24 @@ +start(Module):-start(Module, localhost). +start(Module, Host):-start(Module, Host, 8068). + +start(Module, Host, Port):- + socket('AF_INET', Socket), + hostname_address(Host, HostAddress), + socket_connect(Socket, 'AF_INET'(HostAddress, Port), + StreamIn, StreamOut), + launch(Module, StreamIn, StreamOut), + socket_close(Socket). + +launch(Module, StreamIn, StreamOut) :- + atom_concat('rules/', Module, Path), consult(Path), + do([], StreamIn, StreamOut, Module, [], []). + +do(e, _, _, _, _) :- !. +do(Choice, StreamIn, StreamOut, Module, ModuleState, Hist) :- + call(Module, Hist, ModuleDecision, ModuleState, NewModuleState), + write(StreamOut, ModuleDecision), flush_output(StreamOut), + loop(StreamIn, StreamOut, Module, NewModuleState, [Choice|Hist]). + +loop(StreamIn, StreamOut, Module, ModuleState, Hist) :- + read_atom(StreamIn, Choice), write(Choice), + do(Choice, StreamIn, StreamOut, Module, ModuleState, Hist). -- cgit