8.4 Java Tic-Tac-Toe GUI plays against Prolog opponent(§5.3)

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.

Playing tic tac toe with the Prolog expert

The tic tac toe game can be started by running the Java program. Download TicTacToe.jar and double click on it. A dialog will appear that looks something like this. The top textfield is the command that you use to start SWI-Prolog. The bottom textfield is the exact location for the Prolog tic tac toe program ttt.pl. (The values already filled in are the ones on the author's system.)

Figure 8.4.1
Figure 8.4.1. Where are Prolog and ttt.pl?

Click OK when ready and then the game should appear. (If not, something is amiss!)

Figure 8.4.2
Figure 8.4.2. Initial empty tic tac toe board

The Java GUI user goes first. Suppose that we mark (X) the upper-right spot. Prolog quickly responds with O in the center.

Figure 8.4.3
Figure 8.4.3. After one round

Play continues and either it's a cat's game or Prolog wins...

Figure 8.4.4
Figure 8.4.4. We lose!

The connector architecture

The Java tic tac toe program starts a threaded server (Connector.class) that provides a connection to other running programs that connect to the same port. For tic tac toe we are using port 54321. Each program which connects to this server gets an input text stream and an output text stream. Whatever a clients writes on its output stream is broadcast to all other clients so that they can read what was written from their input stream.

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%  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

Here is a little diagram which displays the Connector graphically ...

8.4.5
Figure 8.4.5. The Connector

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 ...

javac TicTacToe.java
and run from the command line like this ...
java TicTacToe <supply prolog command> <supply full file locator for ttt.pl>
or one can write a script to do this.

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.


The Prolog code ttt.pl for tic tac toe discussed in Section 5.3.
Java code:
TicTacToe.java
Connector.java
TicTacToe.jar:
TicTacToe.jar

Prolog Tutorial Contents