diff options
author | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2012-11-10 19:19:27 +0100 |
---|---|---|
committer | Benjamin Franzke <benjaminfranzke@googlemail.com> | 2012-11-10 19:21:05 +0100 |
commit | b3c30b9f59b25ed14abec0b1569b62787cc448ba (patch) | |
tree | 172c02eb1a36bbd5fda735365d9c5c59247e9ffc | |
parent | 22b189d3f9b07e0bd9d3fc2c852b16560980d8f6 (diff) | |
download | wbs-b3c30b9f59b25ed14abec0b1569b62787cc448ba.tar.gz wbs-b3c30b9f59b25ed14abec0b1569b62787cc448ba.tar.bz2 wbs-b3c30b9f59b25ed14abec0b1569b62787cc448ba.zip |
Add pd_client that works with SWI-Prolog
Since SWI-Prolog provides a different socket interface.
-rw-r--r-- | pd_client_swipl.pl | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/pd_client_swipl.pl b/pd_client_swipl.pl new file mode 100644 index 0000000..1739be7 --- /dev/null +++ b/pd_client_swipl.pl @@ -0,0 +1,28 @@ +start(Module):-start(Module, localhost). +start(Module, Host):-start(Module, Host, 8068). + +start(Module, Host, Port):- + tcp_socket(Socket), + tcp_connect(Socket, Host:Port, StreamIn, StreamOut), + launch(Module, StreamIn, StreamOut), + tcp_close_socket(Socket). + +launch(Module, StreamIn, StreamOut) :- + atom_concat('rules/', Module, Path), consult(Path), + do(StreamIn, StreamOut, Module, [], []). + +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), + 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):-atom_codes(Atom, [Byte]). |