Java Reference
In-Depth Information
152
153 waiting = true ;
154 }
155
156 /** Send this player's move to the server */
157 private void sendMove() throws IOException {
158 toServer.writeInt(rowSelected); // Send the selected row
159 toServer.writeInt(columnSelected); // Send the selected column
160 }
161
162
/** Receive info from the server */
163
private void receiveInfoFromServer() throws IOException {
164
// Receive game status
165
int status = fromServer.readInt();
166
167 if (status == PLAYER1_WON) {
168 // Player 1 won, stop playing
169 continueToPlay = false ;
170 if (myToken == 'X' ) {
171 Platform.runLater(() -> lblStatus.setText( "I won! (X)" ));
172 }
173 else if (myToken == 'O' ) {
174 Platform.runLater(() ->
175 lblStatus.setText( "Player 1 (X) has won!" ));
176 receiveMove();
177 }
178 }
179 else if (status == PLAYER2_WON) {
180 // Player 2 won, stop playing
181 continueToPlay = false ;
182 if (myToken == 'O' ) {
183 Platform.runLater(() -> lblStatus.setText( "I won! (O)" ));
184 }
185 else if (myToken == 'X' ) {
186 Platform.runLater(() ->
187 lblStatus.setText( "Player 2 (O) has won!" ));
188 receiveMove();
189 }
190 }
191 else if (status == DRAW) {
192 // No winner, game is over
193 continueToPlay = false ;
194 Platform.runLater(() ->
195 lblStatus.setText( "Game is over, no winner!" ));
196
197 if (myToken == 'O' ) {
198 receiveMove();
199 }
200 }
201 else {
202 receiveMove();
203 Platform.runLater(() -> lblStatus.setText( "My turn" ));
204 myTurn = true ; // It is my turn
205 }
206 }
207
208
private void receiveMove() throws IOException {
209
// Get the other player's move
210
int row = fromServer.readInt();
211
int column = fromServer.readInt();
 
Search WWH ::




Custom Search