Tic-Tac-Toe Server to handle two-player games over the network.
More...
|
class | PlayerHandler |
| Internal class to handle player connections.
|
|
|
static void | main (String[] args) throws Exception |
| Main method to run the server.
|
|
|
static synchronized char | checkWin () |
| Check if a player has won the game.
|
|
static synchronized boolean | makeMove (int row, int col, char symbol) |
| Make a move on the board.
|
|
Tic-Tac-Toe Server to handle two-player games over the network.
COMP2396 Assignment 5
- Author
- Cheng Ho Ming, Eric (3036216734)
Definition at line 9 of file GameServer.java.
◆ checkWin()
static synchronized char GameServer.checkWin |
( |
| ) |
|
|
inlinestaticprivate |
Check if a player has won the game.
- Returns
- The winning player's symbol ('X' or 'O'), or 'D' for draw, or '\0' if no winner
Definition at line 68 of file GameServer.java.
68 {
69
70 for (int i = 0; i < 3; i++) {
73 board[i][0] !=
'\0') {
75 }
76 }
77
78 for (int i = 0; i < 3; i++) {
81 board[0][i] !=
'\0') {
83 }
84 }
85
88 board[0][0] !=
'\0') {
90 }
93 board[0][2] !=
'\0') {
95 }
96
97 boolean draw = true;
98 for (int i = 0; i < 3 && draw; i++) {
99 for (int j = 0; j < 3 && draw; j++) {
100 if (
board[i][j] ==
'\0') {
101 draw = false;
102 }
103 }
104 }
105 if (draw) return 'D';
106 return '\0';
107 }
References board.
◆ main()
static void GameServer.main |
( |
String[] | args | ) |
throws Exception |
|
inlinestatic |
Main method to run the server.
- Parameters
-
args | Command-line arguments |
- Exceptions
-
Definition at line 21 of file GameServer.java.
21 {
22 ServerSocket listener =
new ServerSocket(
PORT);
23 System.out.println("Tic-Tac-Toe Server is Running...");
24 try {
25 while (true) {
26 Socket socket = listener.accept();
28 playerX =
new PlayerHandler(socket,
'X');
29 System.out.println("Player X connected.");
31 playerX_Thread.start();
33 playerO =
new PlayerHandler(socket,
'O');
34 System.out.println("Player O connected.");
36 playerO_Thread.start();
37 } else {
38
39 PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
40 out.println("SERVERFULL");
41 socket.close();
42 }
43 }
44 } finally {
45 listener.close();
46 }
47 }
static PlayerHandler playerO
static PlayerHandler playerX
References playerO, playerX, and PORT.
◆ makeMove()
static synchronized boolean GameServer.makeMove |
( |
int | row, |
|
|
int | col, |
|
|
char | symbol ) |
|
inlinestaticprivate |
Make a move on the board.
- Parameters
-
row | The row of the move |
col | The column of the move |
symbol | The player's symbol ('X' or 'O') |
- Returns
- True if the move is valid, false otherwise
Definition at line 56 of file GameServer.java.
56 {
57 if (
board[row][col] ==
'\0') {
58 board[row][col] = symbol;
59 return true;
60 }
61 return false;
62 }
References board.
◆ board
char [][] GameServer.board = new char[3][3] |
|
staticprivate |
◆ gameEnded
boolean GameServer.gameEnded = false |
|
staticprivate |
◆ playerO
PlayerHandler GameServer.playerO |
|
staticprivate |
◆ playerX
PlayerHandler GameServer.playerX |
|
staticprivate |
◆ PORT
final int GameServer.PORT = 8901 |
|
staticprivate |
The documentation for this class was generated from the following file: