Java Reference
In-Depth Information
Application
TicTacToeServer
HandleASession
TicTacToeConstants
Similar to
Listing 18.10
Application
TicTacToeClient
Cell
Runnable
TicTacToeServer
HandleASession
TicTacToeClient
start(primaryStage: Stage):
void
-player1: Socket
-player2: Socket
-cell: char[][]
-continueToPlay: boolean
-myTurn: boolean
-myToken: char
-otherToken: char
-cell: Cell[][]
-continueToPlay: boolean
-rowSelected: int
-columnSelected: int
-fromServer: DataInputStream
-toServer: DataOutputStream
-waiting: boolean
«interface»
TicTacToeConstants
+run(): void
-isWon(): boolean
-isFull(): boolean
-sendMove(out:
DataOutputStream, row: int,
column: int): void
+PLAYER1 = 1: int
+PLAYER2 = 2: int
+PLAYER1_WON = 1: int
+PLAYER2_WON = 2: int
+DRAW = 3: int
+CONTINUE = 4: int
+run(): void
-connectToServer(): void
-receiveMove(): void
-sendMove(): void
-receiveInfoFromServer(): void
-waitForPlayerAction(): void
F
IGURE
31.14
TicTacToeServer
creates an instance of
HandleASession
for each session of two players.
TicTacToeClient
creates nine cells in the UI.
5
import
javafx.application.Platform;
6
import
javafx.scene.Scene;
7
import
javafx.scene.control.ScrollPane;
8
import
javafx.scene.control.TextArea;
9
import
javafx.stage.Stage;
10
11
public class
TicTacToeServer
extends
Application
12
implements
TicTacToeConstants {
13
private int
sessionNo =
1
;
// Number a session
14
15 @Override
// Override the start method in the Application class
16
public void
start(Stage primaryStage) {
17 TextArea taLog =
new
TextArea();
18
19
// Create a scene and place it in the stage
20 Scene scene =
new
Scene(
new
ScrollPane(taLog),
450
,
200
);
21 primaryStage.setTitle(
"TicTacToeServer"
);
// Set the stage title
22 primaryStage.setScene(scene);
// Place the scene in the stage
23 primaryStage.show();
// Display the stage
24
25
create UI
new
Thread( () -> {
26
try
{
27
// Create a server socket
28
ServerSocket serverSocket =
new
ServerSocket(
8000
);
server socket
Search WWH ::
Custom Search