-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToeServer.java
More file actions
197 lines (187 loc) · 6.79 KB
/
Copy pathTicTacToeServer.java
File metadata and controls
197 lines (187 loc) · 6.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.io.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.concurrent.Executors;
/**
* Server side class for Tic Tac Toe game.
* Connects two clients using multithreading and processes their moves.
* Determines winner by given game rules.
*
* @author Janvi Sharma
*
*/
public class TicTacToeServer {
/**
* Main function of TicTacToeServer
* Connects 2 clients and assigns them symbols depending on who connected first.
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
try (var serverSocket = new ServerSocket(55000)) {
System.out.println("Tic Tac Toe Server is Running...");
var threadPool = Executors.newFixedThreadPool(200);
while (true) {
TicTacToeGame newGame = new TicTacToeGame();
threadPool.execute(newGame.new GamePlayer(serverSocket.accept(), 'X'));
Thread.sleep(500);
threadPool.execute(newGame.new GamePlayer(serverSocket.accept(), 'O'));
}
}
}
}
/**
* Class that implements the game using defined rules.
*
* @author Janvi Sharma
*
*/
class TicTacToeGame {
public int ctr = 0;
private GamePlayer[] gameBox = new GamePlayer[9];
GamePlayer currPlayer;
/**
* Method to check if the playing grid is full
* @return True if the playing grid is full
*/
public boolean gameBoxFull() {
return Arrays.stream(gameBox).allMatch(p -> p != null);
}
/**
* Synchronized method to ensure that only one client can make a move at any given time
*
* @param loc Location of mouse click
* @param player Reference to the player who made this move
*/
public synchronized void move(int loc, GamePlayer player) {
if (player != currPlayer) {
throw new IllegalStateException("Wait for your turn.");
} else if (player.oppPlayer == null) {
throw new IllegalStateException("You don't have an opponent");
} else if (gameBox[loc] != null) {
throw new IllegalStateException("Already occupied");
}
gameBox[loc] = currPlayer;
currPlayer = currPlayer.oppPlayer;
}
/**
* Method that stores winning combinations according to the game rules
*
* @return True if any of the winner combinations is found
*/
public boolean winnerCombinations() {
return (gameBox[0] != null && gameBox[0] == gameBox[1] && gameBox[0] == gameBox[2])
|| (gameBox[3] != null && gameBox[3] == gameBox[4] && gameBox[3] == gameBox[5])
|| (gameBox[6] != null && gameBox[6] == gameBox[7] && gameBox[6] == gameBox[8])
|| (gameBox[0] != null && gameBox[0] == gameBox[3] && gameBox[0] == gameBox[6])
|| (gameBox[1] != null && gameBox[1] == gameBox[4] && gameBox[1] == gameBox[7])
|| (gameBox[2] != null && gameBox[2] == gameBox[5] && gameBox[2] == gameBox[8])
|| (gameBox[0] != null && gameBox[0] == gameBox[4] && gameBox[0] == gameBox[8])
|| (gameBox[2] != null && gameBox[2] == gameBox[4] && gameBox[2] == gameBox[6]
);
}
/**
*A Player is either "X" or "O"
*The player communicates with the server to keep its board up to date.
*
*@author Janvi Sharma
*
*/
class GamePlayer implements Runnable {
char symbol;
GamePlayer oppPlayer;
PrintWriter oStream;
Scanner inStream;
Socket socket;
/**
* Constructor for class assigns socket and symbol(either 'X' or 'O') to the player.
*
* @param socket
* @param symbol
*/
public GamePlayer(Socket socket, char symbol) {
this.socket = socket;
this.symbol = symbol;
}
/**
* Method that sets up the default game set up and allows back and forth client-server communication
*
*/
public void run() {
try {
defaultGameSetUp();
basicMessageProcessing();
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (oppPlayer != null && oppPlayer.oStream != null) {
oppPlayer.oStream.println("OppLeft");
}
try {socket.close();}
catch (IOException e) {}
}
}
/**
* Method that sends the first message from the server assigning the symbol to a client/player.
*
* @throws IOException
*/
private void defaultGameSetUp() throws IOException {
inStream = new Scanner(socket.getInputStream());
oStream = new PrintWriter(socket.getOutputStream(), true);
oStream.println("WELCOME " + symbol);
if (symbol == 'X') {
currPlayer = this;
}
else {
oppPlayer = currPlayer;
oppPlayer.oppPlayer = this;
}
}
/**
* Method that reads incoming messages from the server
*/
private void basicMessageProcessing() {
while (inStream.hasNextLine()) {
var command = inStream.nextLine();
if(command.startsWith("ButtonClicked")) {
System.out.println("Start button clicked!");
ctr++;
System.out.println("Times: "+ctr);
} else if (command.startsWith("QuitGame")) {
return;
} else if(ctr==2){
if (command.startsWith("Move")) {
moveMessageProcessing(Integer.parseInt(command.substring(5)));
}
}
}
}
/**
* Method to process the message if the message contained a "Move" that the client made.
* Also checks if a client has won, lost or a game is draw.
* Sends appropriate messages back to the client.
* @param location location of the player's move
*/
private void moveMessageProcessing(int location) {
try {
move(location, this);
oStream.println("ValidMove");
oppPlayer.oStream.println("OppMoved " + location);
if (winnerCombinations()) {
oStream.println("Winner");
oppPlayer.oStream.println("Loss");
} else if (gameBoxFull()) {
oStream.println("Draw");
oppPlayer.oStream.println("Draw");
}
} catch (IllegalStateException e) {
oStream.println("Message " + e.getMessage());
}
}
}
}