This section discusses a methodology for enabling a Java tic tac toe GUI to interact with the Prolog tic tac toe game agent described in Section 5.3, using a socket data connector.
Click OK when ready and then the game should appear. (If not, something is amiss!)
The Java GUI user goes first. Suppose that we mark (X) the upper-right spot. Prolog quickly responds with O in the center.
Play continues and either it's a cat's game or Prolog wins...
For the tic tac toe game, The Java GUI player program starts the Connector, connects itself to it, then starts a process that loads the Prolog player ttt.pl. The Prolog program connects to the port and then the two programs can talk to each other.
If the the Java GUI player marks the spot X,Y then the program writes "(X,Y)." to its Connector output stream and Prolog reads this term from its Connector input stream. Prolog records Java's move, calls the alpha-beta routine, gets a best move, and writes its move to its Connector output stream (see code below). Java in turn reads Prolog's move, and play continues thusly. Here is the connection code in ttt.pl
Here is a little diagram which displays the Connector graphically ...%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%% Play tic tac toe with the Java GUI %%% using port 54321. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% connect(Port) :- tcp_socket(Socket), gethostname(Host), % local host tcp_connect(Socket,Host:Port), tcp_open_socket(Socket,INs,OUTs), assert(connectedReadStream(INs)), assert(connectedWriteStream(OUTs)). :- connect(54321). % Comment out for Prolog-only testing ttt :- connectedReadStream(IStream), %%%% read x's move read(IStream,(X,Y)), %%%% update Prolog's board record(x,X,Y), board(B), %%%% look for best next move alpha_beta(o,2,B,-200,200,(U,V),_Value), %%%% update the board record(o,U,V), connectedWriteStream(OStream), %%%% tell Java tic tac toe player write(OStream,(U,V)), nl(OStream), flush_output(OStream), ttt. :- ttt. % Comment out for Prolog-only testing
The Connector provides a simple way to connect running programs that need to talk to each other. Notice that there is no interchange of objects: Each client simply writes text that other client(s) can parse in order to get the needed kind of data.
Suppose that TicTacToe.java and Connector.java are downloaded into a folder. Then, inside that folder, compile this way ...
and run from the command line like this ...javac TicTacToe.java
or one can write a script to do this.java TicTacToe <supply prolog command> <supply full file locator for ttt.pl>
Exercise 8.4.1. Modify the tic tac toe programs so that either player can play first.
Exercise 8.4.2. Implement the Connector in Prolog. It is required that exceptions generated by clients do NOT disrupt the connection server itself.
Exercise 8.4.3. Play Tic Tac Toe with the Prolog "expert", and figure out how to win. Analyze why it is that the human player can win. Then modify the Prolog program so that it either wins or causes a draw, that is, the human can no longer win.
Exercise 8.4.4. (Masters Project) Create a generalized connection server in Prolog that uses some exchange data standard, like XML, or, better, XML subject to verification via an XML schema. Such a connect-let then serves as a perfectly general way to connect Prolog to other processes. One assumes that the processes handle common objects by marshalling them into and out of exchange data connections. In this way there is no need for "common" objects. Instead, "common" intentions are used to design the programs that need to talk to each other; so each application uses the same exchange schema, which can be independently specified.
TicTacToe.javaTicTacToe.jar:
Connector.java
TicTacToe.jar