4 public class TTTServer
\r
6 //the tictactoe game board
\r
8 private static char[][] board;
\r
9 //keeps track of how many turns have past
\r
10 private static int numOfTurns;
\r
11 //ints used to store the location of the cell the user will input
\r
12 private static int row;
\r
13 private static int col;
\r
14 private static boolean notDone;
\r
16 private static void resetGame()
\r
22 for(int i = 0; i < 3; i++)
\r
24 for(int j = 0; j < 3; j++)
\r
31 private static void displayBoard()
\r
33 System.out.println("--------------------");
\r
34 System.out.println("[R,C][ 1 ][ 2 ][ 3 ]");
\r
35 System.out.println("--------------------");
\r
36 System.out.println("[ 1 ]| " + board[0][0] + " | " + board[0][1] +
\r
37 " | " + board[0][2] + " | ");
\r
38 System.out.println("--------------------");
\r
39 System.out.println("[ 2 ]| " + board[1][0] + " | " + board[1][1] +
\r
40 " | " + board[1][2] + " | ");
\r
41 System.out.println("--------------------");
\r
42 System.out.println("[ 3 ]| " + board[2][0] + " | " + board[2][1] +
\r
43 " | " + board[2][2] + " | ");
\r
44 System.out.println("--------------------");
\r
47 //put the move on the board and update numOfTurns
\r
48 private static void markMove(char xo)
\r
50 board[row - 1][col - 1] = xo;
\r
54 //check for a winner or a tie
\r
55 //true == winner or tie
\r
56 private static boolean checkWinner(char xo)
\r
59 if(board[0][0] == xo && board[0][0] == board[0][1] &&
\r
60 board[0][1] == board[0][2])
\r
62 System.out.println(xo + " is the winner!");
\r
66 else if(board[1][0] == xo && board[1][0] == board[1][1] &&
\r
67 board[1][1] == board[1][2])
\r
69 System.out.println(xo + " is the winner!");
\r
73 else if(board[2][0] == xo && board[2][0] == board[2][1] &&
\r
74 board[2][1] == board[2][2])
\r
76 System.out.println(xo + " is the winner!");
\r
80 else if(board[0][0] == xo && board[0][0] == board[1][0] &&
\r
81 board[1][0] == board[2][0])
\r
83 System.out.println(xo + " is the winner!");
\r
87 else if(board[0][1] == xo && board[0][1] == board[1][1] &&
\r
88 board[1][1] == board[2][1])
\r
90 System.out.println(xo + " is the winner!");
\r
94 else if(board[0][2] == xo && board[0][2] == board[1][2] &&
\r
95 board[1][2] == board[2][2])
\r
97 System.out.println(xo + " is the winner!");
\r
101 else if(board[0][0] == xo && board[0][0] == board[1][1] &&
\r
102 board[1][1] == board[2][2])
\r
104 System.out.println(xo + " is the winner!");
\r
108 else if(board[0][2] == xo && board[0][2] == board[1][1] &&
\r
109 board[1][1] == board[2][0])
\r
111 System.out.println(xo + " is the winner!");
\r
116 else if(numOfTurns == 9)
\r
118 System.out.println("Tie Game!");
\r
126 //the logic that happens for each turn for X or O
\r
127 public static void turnLogic(char xo, int r, int c)
\r
131 System.out.println("\n" + xo + "'s turn.");
\r
132 System.out.println("Please enter your move as two separate integers: ");
\r
136 System.out.println("\nYou entered (" + row + "," + col + ")\n");
\r
140 System.out.println("\n" + xo + "'s turn.");
\r
148 //check for a winner and quit cond.
\r
149 if(checkWinner(xo) || (row == -1 && col == -1))
\r
153 public static void main(String[] args)
\r
155 //the sockets to be used
\r
156 ServerSocket mySocket = null;
\r
157 Socket myConnection= null;
\r
158 //string that will hold the message to be received and sent
\r
159 String myString = null;
\r
161 BufferedReader myInput = null;
\r
163 PrintStream myOutput = null;
\r
167 board = new char[3][3];
\r
173 //create new socket
\r
174 mySocket = new ServerSocket(8080);
\r
175 System.out.println("Server Port: " + mySocket.getLocalPort());
\r
179 //accept incoming tcp connection
\r
180 myConnection = mySocket.accept();
\r
182 //create and read the message from the client to the input buffer
\r
183 myInput = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));
\r
185 //create and print the message to the output buffer
\r
186 myOutput = new PrintStream(myConnection.getOutputStream());
\r
188 //loop for nine times at most
\r
189 while(numOfTurns != 9 && notDone == true)
\r
191 switch(numOfTurns % 2)
\r
195 turnLogic('X', 0, 0);
\r
196 myOutput.println(row + " " + col);
\r
201 myString = myInput.readLine();
\r
203 //was "quit" received?
\r
204 if(myString.equals("quit"))
\r
210 int r = Integer.parseInt(myString.substring(0, 1));
\r
211 int c = Integer.parseInt(myString.substring(2, 3));
\r
212 turnLogic('O', r, c);
\r
216 //should not happen
\r
218 System.out.println("Program Error!");
\r
228 myConnection.close();
\r
230 catch(IOException e)
\r
232 System.err.println(e);
\r
237 //some good stuff to read user ints, maybe too much?
\r
238 public static int readInt() throws NumberFormatException
\r
240 String inputString = null;
\r
241 inputString = readWord();
\r
243 return Integer.parseInt(inputString);
\r
247 public static String readWord()
\r
249 String result = "";
\r
253 while (Character.isWhitespace(next))
\r
256 while(!(Character.isWhitespace(next)))
\r
258 result = result + next;
\r
268 System.err.println("Error.");
\r
277 public static char readChar()
\r
279 int charAsInt = -1;
\r
283 charAsInt = System.in.read();
\r
285 catch(IOException e)
\r
287 System.err.println(e);
\r
291 return (char)charAsInt;
\r