--- /dev/null
+public class Board {
+ // TicTacToe Board flags
+ flag init;
+
+ int[][] board;
+
+ int winningplayer;
+
+ public Board() {
+ winningplayer = -1;
+ board = new int[3][3];
+ for (int i = 0; i < 3; i++)
+ for (int j = 0; j < 3; j++)
+ board[i][j] = 0;
+ }
+
+ public int makeMove(int row, int col) {
+ if (boardFull() == 1) {
+ winningplayer = 0;
+ return 2;
+ }
+ if (board[row][col] != 0) { // Space taken
+ return -1;
+ }
+ else {
+ board[row][col] = 1;
+ if (checkForWin(1) == 1) { // Check if player won
+ winningplayer = 1;
+ return 2;
+ }
+ // Computer makes move
+ if (computerMakeMove() == 1) { // If made move successful
+ if (checkForWin(2) == 1) { // Check if computer won
+ winningplayer = 2;
+ return 2;
+ }
+ }
+ else { // Board full, no winner
+ winningplayer = 0;
+ return 2;
+ }
+ }
+ return 1;
+ }
+
+ public int boardFull() {
+ for (int i = 0; i < 3; i++)
+ for (int j = 0; j < 3; j++)
+ if (board[i][j] == 0)
+ return 0;
+ return 1;
+ }
+
+ public int computerMakeMove() {
+ for (int i = 0; i < 3; i++)
+ for (int j = 0; j < 3; j++)
+ if (board[i][j] == 0) {
+ board[i][j] = 2;
+ return 1;
+ }
+ return 0;
+ }
+
+ public int checkForWin(int p) {
+ // Add logic for checking if player p wins
+ // Horiz
+
+ if ((board[0][0] == p) && (board[0][1] == p) && (board[0][2] == p) ||
+ (board[1][0] == p) && (board[1][1] == p) && (board[1][2] == p) ||
+ (board[2][0] == p) && (board[2][1] == p) && (board[2][2] == p)) {
+ return 1;
+ }
+
+ // Vert
+ if ((board[0][0] == p) && (board[1][0] == p) && (board[2][0] == p) ||
+ (board[0][1] == p) && (board[1][1] == p) && (board[2][1] == p) ||
+ (board[0][2] == p) && (board[1][2] == p) && (board[2][2] == p)) {
+ return 1;
+ }
+
+ //Diag
+ if ((board[0][0] == p) && (board[1][1] == p) && (board[2][2] == p) ||
+ (board[0][2] == p) && (board[1][1] == p) && (board[2][0] == p)) {
+ return 1;
+ }
+
+ return 0;
+ }
+
+ public int winner() {
+ return winningplayer;
+ }
+}
--- /dev/null
+/* Startup object is generated with the initialstate flag set by the
+ * system to start the computation up */
+
+// Create ServerSocket
+task Startup(StartupObject s {initialstate}) {
+ System.printString("TTT Server Starting...\n");
+ ServerSocket ss = new ServerSocket(8000);
+ System.printString("Creating ServerSocket\n");
+ Board tttBoard = new Board() {init};
+ taskexit(s {!initialstate}); // Turn off initial state flag
+}
+
+//Listen for a request and accept request
+task AcceptConnection(ServerSocket ss{SocketPending}) {
+ System.printString("Waiting for connection...\n");
+ tag t=new tag(connect);
+ TTTServerSocket ttts = new TTTServerSocket() {TTTSInitialize}{t};
+ System.printString("Calling accept...\n");
+ ss.accept(t);
+ System.printString("Connected...\n");
+}
+
+// Process incoming requests
+task ProcessRequest(TTTServerSocket ttts{TTTSInitialize}{connect l}, Socket s{IOPending}{connect l}) {
+ System.printString("Request received...");
+ int action = ttts.receive(s);
+ if (action == 1) { // Make move
+ taskexit(ttts {MakeMove});
+ }
+ else { // Send Error
+ taskexit(ttts {SendError});
+ }
+}
+
+task ProcessMove(TTTServerSocket ttts{MakeMove}, Board tttBoard{init}) {
+ System.printString("Processing player's move...");
+ int result = tttBoard.makeMove(ttts.getRow(), ttts.getCol());
+ if (result == 1) { //Move made, send board display
+ taskexit(ttts {!MakeMove, SendBoard});
+ }
+ else if (result == 2) { //Move made, game over
+ taskexit(ttts {!MakeMove, SendDone});
+ }
+ else {// Error
+ taskexit(ttts {!MakeMove, SendError});
+ }
+}
+
+task SendBoardDisplay(TTTServerSocket ttts{SendBoard}{connect l}, Board tttBoard{init}, Socket s{}{connect l}) {
+ ttts.sendBoardDisplay(tttBoard, s);
+}
+
+task GameOver(TTTServerSocket ttts{SendDone}{connect l}, Board tttBoard{init}, Socket s{}{connect l}) {
+ ttts.sendDone(tttBoard.winner(), s);
+}
+
+task SendErrorMessage(TTTServerSocket ttts{SendError}{connect l}, Board tttBoard{init}, Socket s{}{connect l}) {
+ ttts.sendError(s);
+}
--- /dev/null
+public class TTTServerSocket {
+ // TTTServerSocket flags
+ flag TTTSInitialize;
+
+ flag MakeMove;
+ flag SendError;
+ flag SendBoard;
+ flag SendDone;
+
+ String request;
+ int row, col;
+
+ //Constructor
+ public TTTServerSocket(){
+ System.printString("Constructing TTTServerSocket....\n");
+ }
+
+ public int receive(Socket s)
+ {
+ byte b1[] = new byte[1024];
+ s.read(b1);
+ request = new String(b1);
+ System.printString("request: ");
+ System.printString(request);
+ if (parseTransaction() == 1) {
+ return 1;
+ }
+ return 0;
+ }
+
+ // Parse request
+ public int parseTransaction(){
+ int start = request.indexOf('_');
+ String s = request.subString(start+1);
+//_move:3:3
+ if (s.startsWith("move")==true){
+ //Get row
+ int i1 = s.indexOf(':');
+ String rowStr = new String(s.subString(i1+1, i1+2));
+ row = Integer.parseInt(rowStr);
+
+ //Get col
+ String s2 = new String(s.subString(i1+2));
+ int i2 = s2.indexOf(':');
+ String colStr = new String(s.subString(i2+1, i2+2));
+ col = Integer.parseInt(colStr);
+ return 1;
+
+ }
+ // Error transaction
+ return -1;
+ }
+
+ public int getRow(){
+ return row;
+ }
+ public int getCol(){
+ return col;
+ }
+
+ public void sendBoardDisplay(Board theBoard, Socket s) {
+ StringBuffer line1 = new String ("display_");
+
+ for (int i = 0; i < 3; i++) {
+ for (int j = 0; j < 3; j++) {
+ if (theBoard.board[i][j] == 1)
+ line1.append("X");
+ else if (theBoard.board[i][j] == 2)
+ line1.append("O");
+ else
+ line1.append("-");
+ }
+ line1.append("_");
+ }
+ String towrite = new String(line1);
+ s.write(towrite.getBytes());
+ return;
+ }
+
+ public void sendDone(int winner, Socket s) {
+ StringBuffer line1 = new String ("done_");
+ if (winner == 0)
+ line1.append("tie");
+ else if (winner == 1)
+ line1.append("player");
+ else
+ line1.append("computer");
+
+ String towrite = new String(line1);
+ s.write(towrite.getBytes());
+ return;
+ }
+
+ public void sendError(Socket s) {
+ StringBuffer line1 = new String ("error_wrongmove");
+
+ String towrite = new String(line1);
+ s.write(towrite.getBytes());
+ return;
+ }
+}