changes: generated annotated code but it still causes type errors + re-formatting...
[IRC.git] / Robust / src / Benchmarks / TTTJava / TTTServer.java
1 import java.net.*;\r
2 import java.io.*;\r
3 \r
4 public class TTTServer\r
5 {\r
6         //the tictactoe game board\r
7     //2d 3x3 char array\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
15     \r
16     private static void resetGame()\r
17     {\r
18         numOfTurns = 0;\r
19         row = 0;\r
20         col = 0;\r
21         \r
22         for(int i = 0; i < 3; i++)\r
23         {\r
24             for(int j = 0; j < 3; j++)\r
25             {\r
26                 board[i][j] = ' ';\r
27             }\r
28         }\r
29     }\r
30     \r
31     private static void displayBoard()\r
32     {\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
45     }\r
46     \r
47     //put the move on the board and update numOfTurns\r
48     private static void markMove(char xo)\r
49     {\r
50         board[row - 1][col - 1] = xo;\r
51         numOfTurns++;\r
52     }\r
53     \r
54     //check for a winner or a tie\r
55     //true == winner or tie\r
56     private static boolean checkWinner(char xo)\r
57     {\r
58         //horizontal win\r
59         if(board[0][0] == xo && board[0][0] == board[0][1] &&\r
60            board[0][1] == board[0][2])\r
61         {\r
62             System.out.println(xo + " is the winner!");\r
63             return true;\r
64         }\r
65         //horizontal win\r
66         else if(board[1][0] == xo && board[1][0] ==  board[1][1] &&\r
67                 board[1][1] == board[1][2])\r
68         {\r
69             System.out.println(xo + " is the winner!");\r
70             return true;\r
71         }\r
72         //horizontal win\r
73         else if(board[2][0] == xo && board[2][0] ==  board[2][1] &&\r
74                 board[2][1] == board[2][2])\r
75         {\r
76             System.out.println(xo + " is the winner!");\r
77             return true;\r
78         } \r
79         //vertial win\r
80         else if(board[0][0] == xo && board[0][0] ==  board[1][0] &&\r
81                 board[1][0] == board[2][0])\r
82         {\r
83             System.out.println(xo + " is the winner!");\r
84             return true;\r
85         }\r
86         //vertial win\r
87         else if(board[0][1] == xo && board[0][1] ==  board[1][1] &&\r
88                 board[1][1] == board[2][1])\r
89         {\r
90             System.out.println(xo + " is the winner!");\r
91             return true;\r
92         }\r
93         //vertial win\r
94         else if(board[0][2] == xo && board[0][2] ==  board[1][2] &&\r
95                 board[1][2] == board[2][2])\r
96         {\r
97             System.out.println(xo + " is the winner!");\r
98             return true;\r
99         }\r
100         //diagonal win\r
101         else if(board[0][0] == xo && board[0][0] ==  board[1][1] &&\r
102                 board[1][1] == board[2][2])\r
103         {\r
104             System.out.println(xo + " is the winner!");\r
105             return true;\r
106         }\r
107         //diagonal win\r
108         else if(board[0][2] == xo && board[0][2] ==  board[1][1] &&\r
109                 board[1][1] == board[2][0])\r
110         {\r
111             System.out.println(xo + " is the winner!");\r
112             return true;\r
113         }\r
114         //tie game\r
115         //board is full\r
116         else if(numOfTurns == 9)\r
117         {\r
118             System.out.println("Tie Game!");\r
119             return true;            \r
120         }\r
121         //no winner yet\r
122         else\r
123             return false;\r
124     }\r
125     \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
128     {\r
129         if(xo == 'X')\r
130         {\r
131                 System.out.println("\n" + xo + "'s turn.");\r
132                 System.out.println("Please enter your move as two separate integers: ");\r
133                 //-1 -1 to quit\r
134                 row = readInt();\r
135                 col = readInt();\r
136                 System.out.println("\nYou entered (" + row + "," + col + ")\n");\r
137         }\r
138         else if(xo == 'O')\r
139         {\r
140                 System.out.println("\n" + xo + "'s turn.");\r
141                 row = r;\r
142                 col = c;\r
143         }\r
144         \r
145         markMove(xo);\r
146         displayBoard();\r
147 \r
148         //check for a winner and quit cond.\r
149         if(checkWinner(xo) || (row == -1 && col == -1))\r
150                 notDone = false;\r
151     }\r
152     \r
153     public static void main(String[] args)\r
154     {  \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
160         //input buffer\r
161         BufferedReader myInput = null;\r
162         //output buffer\r
163         PrintStream myOutput = null;\r
164         //loop variable\r
165         notDone = true;\r
166         \r
167         board = new char[3][3];\r
168         resetGame();\r
169         \r
170         //start server\r
171         try\r
172         {\r
173             //create new socket\r
174             mySocket = new ServerSocket(8080);\r
175             System.out.println("Server Port: " + mySocket.getLocalPort());\r
176             \r
177             displayBoard();\r
178             \r
179             //accept incoming tcp connection\r
180             myConnection = mySocket.accept();\r
181             \r
182             //create and read the message from the client to the input buffer\r
183             myInput = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));\r
184             \r
185             //create and print the message to the output buffer\r
186             myOutput = new PrintStream(myConnection.getOutputStream());\r
187             \r
188             //loop for nine times at most\r
189             while(numOfTurns != 9 && notDone == true)\r
190             {\r
191                 switch(numOfTurns % 2)\r
192                 {\r
193                     //even case\r
194                     case 0:              \r
195                         turnLogic('X', 0, 0);\r
196                         myOutput.println(row + " " + col); \r
197                         break;\r
198                         \r
199                     //odd case\r
200                     case 1:\r
201                         myString = myInput.readLine(); \r
202                         \r
203                         //was "quit" received?\r
204                         if(myString.equals("quit"))\r
205                         {\r
206                             notDone = false;\r
207                         }\r
208                         else\r
209                         {\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
213                         }\r
214                         break;\r
215                         \r
216                     //should not happen\r
217                     default:\r
218                         System.out.println("Program Error!");\r
219                         break;\r
220                 }\r
221             }\r
222             \r
223             //close buffers\r
224             myInput.close();\r
225             myOutput.close();\r
226             \r
227             //close socket\r
228             myConnection.close();\r
229         }\r
230         catch(IOException e)\r
231         {\r
232             System.err.println(e);\r
233             System.exit(1);\r
234         }\r
235     }\r
236     \r
237     //some good stuff to read user ints, maybe too much?\r
238         public static int readInt() throws NumberFormatException\r
239         {\r
240                 String inputString = null;\r
241             inputString = readWord();\r
242             \r
243         return Integer.parseInt(inputString);\r
244         }\r
245 \r
246         //read a lot \r
247         public static String readWord()\r
248     {\r
249                 String result = "";\r
250         char next;\r
251 \r
252         next = readChar();\r
253         while (Character.isWhitespace(next))\r
254             next = readChar();\r
255 \r
256             while(!(Character.isWhitespace(next)))\r
257             {\r
258                 result = result + next;\r
259                 next = readChar();\r
260             }\r
261 \r
262         if (next == '\r')\r
263         {\r
264                 next = readChar();\r
265                 \r
266             if (next != '\n')\r
267             {\r
268                 System.err.println("Error.");\r
269                 System.exit(1);\r
270             }\r
271         }\r
272 \r
273         return result;\r
274     }\r
275 \r
276         //read one\r
277         public static char readChar()\r
278     {\r
279         int charAsInt = -1; \r
280         \r
281         try\r
282         {\r
283             charAsInt = System.in.read();\r
284         }\r
285         catch(IOException e)\r
286         {\r
287             System.err.println(e);\r
288             System.exit(1);\r
289         }\r
290 \r
291         return (char)charAsInt;\r
292     }\r
293 }\r