+++ /dev/null
-import java.net.*;\r
-import java.io.*;\r
-\r
-public class TTTServer\r
-{\r
- //the tictactoe game board\r
- //2d 3x3 char array\r
- private static char[][] board;\r
- //keeps track of how many turns have past\r
- private static int numOfTurns;\r
- //ints used to store the location of the cell the user will input\r
- private static int row;\r
- private static int col;\r
- private static boolean notDone;\r
- \r
- private static void resetGame()\r
- {\r
- numOfTurns = 0;\r
- row = 0;\r
- col = 0;\r
- \r
- for(int i = 0; i < 3; i++)\r
- {\r
- for(int j = 0; j < 3; j++)\r
- {\r
- board[i][j] = ' ';\r
- }\r
- }\r
- }\r
- \r
- private static void displayBoard()\r
- {\r
- System.out.println("--------------------");\r
- System.out.println("[R,C][ 1 ][ 2 ][ 3 ]");\r
- System.out.println("--------------------");\r
- System.out.println("[ 1 ]| " + board[0][0] + " | " + board[0][1] +\r
- " | " + board[0][2] + " | ");\r
- System.out.println("--------------------");\r
- System.out.println("[ 2 ]| " + board[1][0] + " | " + board[1][1] +\r
- " | " + board[1][2] + " | ");\r
- System.out.println("--------------------");\r
- System.out.println("[ 3 ]| " + board[2][0] + " | " + board[2][1] +\r
- " | " + board[2][2] + " | ");\r
- System.out.println("--------------------"); \r
- }\r
- \r
- //put the move on the board and update numOfTurns\r
- private static void markMove(char xo)\r
- {\r
- board[row - 1][col - 1] = xo;\r
- numOfTurns++;\r
- }\r
- \r
- //check for a winner or a tie\r
- //true == winner or tie\r
- private static boolean checkWinner(char xo)\r
- {\r
- //horizontal win\r
- if(board[0][0] == xo && board[0][0] == board[0][1] &&\r
- board[0][1] == board[0][2])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- }\r
- //horizontal win\r
- else if(board[1][0] == xo && board[1][0] == board[1][1] &&\r
- board[1][1] == board[1][2])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- }\r
- //horizontal win\r
- else if(board[2][0] == xo && board[2][0] == board[2][1] &&\r
- board[2][1] == board[2][2])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- } \r
- //vertial win\r
- else if(board[0][0] == xo && board[0][0] == board[1][0] &&\r
- board[1][0] == board[2][0])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- }\r
- //vertial win\r
- else if(board[0][1] == xo && board[0][1] == board[1][1] &&\r
- board[1][1] == board[2][1])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- }\r
- //vertial win\r
- else if(board[0][2] == xo && board[0][2] == board[1][2] &&\r
- board[1][2] == board[2][2])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- }\r
- //diagonal win\r
- else if(board[0][0] == xo && board[0][0] == board[1][1] &&\r
- board[1][1] == board[2][2])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- }\r
- //diagonal win\r
- else if(board[0][2] == xo && board[0][2] == board[1][1] &&\r
- board[1][1] == board[2][0])\r
- {\r
- System.out.println(xo + " is the winner!");\r
- return true;\r
- }\r
- //tie game\r
- //board is full\r
- else if(numOfTurns == 9)\r
- {\r
- System.out.println("Tie Game!");\r
- return true; \r
- }\r
- //no winner yet\r
- else\r
- return false;\r
- }\r
- \r
- //the logic that happens for each turn for X or O\r
- public static void turnLogic(char xo, int r, int c)\r
- {\r
- if(xo == 'X')\r
- {\r
- System.out.println("\n" + xo + "'s turn.");\r
- System.out.println("Please enter your move as two separate integers: ");\r
- //-1 -1 to quit\r
- row = readInt();\r
- col = readInt();\r
- System.out.println("\nYou entered (" + row + "," + col + ")\n");\r
- }\r
- else if(xo == 'O')\r
- {\r
- System.out.println("\n" + xo + "'s turn.");\r
- row = r;\r
- col = c;\r
- }\r
- \r
- markMove(xo);\r
- displayBoard();\r
-\r
- //check for a winner and quit cond.\r
- if(checkWinner(xo) || (row == -1 && col == -1))\r
- notDone = false;\r
- }\r
- \r
- public static void main(String[] args)\r
- { \r
- //the sockets to be used\r
- ServerSocket mySocket = null;\r
- Socket myConnection= null;\r
- //string that will hold the message to be received and sent\r
- String myString = null;\r
- //input buffer\r
- BufferedReader myInput = null;\r
- //output buffer\r
- PrintStream myOutput = null;\r
- //loop variable\r
- notDone = true;\r
- \r
- board = new char[3][3];\r
- resetGame();\r
- \r
- //start server\r
- try\r
- {\r
- //create new socket\r
- mySocket = new ServerSocket(8080);\r
- System.out.println("Server Port: " + mySocket.getLocalPort());\r
- \r
- displayBoard();\r
- \r
- //accept incoming tcp connection\r
- myConnection = mySocket.accept();\r
- \r
- //create and read the message from the client to the input buffer\r
- myInput = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));\r
- \r
- //create and print the message to the output buffer\r
- myOutput = new PrintStream(myConnection.getOutputStream());\r
- \r
- //loop for nine times at most\r
- while(numOfTurns != 9 && notDone == true)\r
- {\r
- switch(numOfTurns % 2)\r
- {\r
- //even case\r
- case 0: \r
- turnLogic('X', 0, 0);\r
- myOutput.println(row + " " + col); \r
- break;\r
- \r
- //odd case\r
- case 1:\r
- myString = myInput.readLine(); \r
- \r
- //was "quit" received?\r
- if(myString.equals("quit"))\r
- {\r
- notDone = false;\r
- }\r
- else\r
- {\r
- int r = Integer.parseInt(myString.substring(0, 1));\r
- int c = Integer.parseInt(myString.substring(2, 3));\r
- turnLogic('O', r, c);\r
- }\r
- break;\r
- \r
- //should not happen\r
- default:\r
- System.out.println("Program Error!");\r
- break;\r
- }\r
- }\r
- \r
- //close buffers\r
- myInput.close();\r
- myOutput.close();\r
- \r
- //close socket\r
- myConnection.close();\r
- }\r
- catch(IOException e)\r
- {\r
- System.err.println(e);\r
- System.exit(1);\r
- }\r
- }\r
- \r
- //some good stuff to read user ints, maybe too much?\r
- public static int readInt() throws NumberFormatException\r
- {\r
- String inputString = null;\r
- inputString = readWord();\r
- \r
- return Integer.parseInt(inputString);\r
- }\r
-\r
- //read a lot \r
- public static String readWord()\r
- {\r
- String result = "";\r
- char next;\r
-\r
- next = readChar();\r
- while (Character.isWhitespace(next))\r
- next = readChar();\r
-\r
- while(!(Character.isWhitespace(next)))\r
- {\r
- result = result + next;\r
- next = readChar();\r
- }\r
-\r
- if (next == '\r')\r
- {\r
- next = readChar();\r
- \r
- if (next != '\n')\r
- {\r
- System.err.println("Error.");\r
- System.exit(1);\r
- }\r
- }\r
-\r
- return result;\r
- }\r
-\r
- //read one\r
- public static char readChar()\r
- {\r
- int charAsInt = -1; \r
- \r
- try\r
- {\r
- charAsInt = System.in.read();\r
- }\r
- catch(IOException e)\r
- {\r
- System.err.println(e);\r
- System.exit(1);\r
- }\r
-\r
- return (char)charAsInt;\r
- }\r
-}\r