COMP2396
Loading...
Searching...
No Matches
GameServer Class Reference

Tic-Tac-Toe Server to handle two-player games over the network. More...

Collaboration diagram for GameServer:

Classes

class  PlayerHandler
 Internal class to handle player connections.
 

Static Public Member Functions

static void main (String[] args) throws Exception
 Main method to run the server.
 

Static Private Member Functions

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.
 

Static Private Attributes

static char[][] board = new char[3][3]
 
static boolean gameEnded = false
 
static PlayerHandler playerO
 
static PlayerHandler playerX
 
static final int PORT = 8901
 

Detailed Description

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.

Member Function Documentation

◆ 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 // Check rows
70 for (int i = 0; i < 3; i++) {
71 if (board[i][0] == board[i][1] &&
72 board[i][1] == board[i][2] &&
73 board[i][0] != '\0') {
74 return board[i][0];
75 }
76 }
77 // Check columns
78 for (int i = 0; i < 3; i++) {
79 if (board[0][i] == board[1][i] &&
80 board[1][i] == board[2][i] &&
81 board[0][i] != '\0') {
82 return board[0][i];
83 }
84 }
85 // Check diagonals
86 if (board[0][0] == board[1][1] &&
87 board[1][1] == board[2][2] &&
88 board[0][0] != '\0') {
89 return board[0][0];
90 }
91 if (board[0][2] == board[1][1] &&
92 board[1][1] == board[2][0] &&
93 board[0][2] != '\0') {
94 return board[0][2];
95 }
96 // Check for draw
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 }
static char[][] board

References board.

◆ main()

static void GameServer.main ( String[] args) throws Exception
inlinestatic

Main method to run the server.

Parameters
argsCommand-line arguments
Exceptions
Exception

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();
27 if (playerX == null) {
28 playerX = new PlayerHandler(socket, 'X');
29 System.out.println("Player X connected.");
30 Thread playerX_Thread = new Thread(playerX);
31 playerX_Thread.start();
32 } else if (playerO == null) {
33 playerO = new PlayerHandler(socket, 'O');
34 System.out.println("Player O connected.");
35 Thread playerO_Thread = new Thread(playerO);
36 playerO_Thread.start();
37 } else {
38 // Reject additional players to prevent overload
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 final int PORT
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
rowThe row of the move
colThe column of the move
symbolThe 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.

Member Data Documentation

◆ board

char [][] GameServer.board = new char[3][3]
staticprivate

Definition at line 13 of file GameServer.java.

Referenced by checkWin(), and makeMove().

◆ gameEnded

boolean GameServer.gameEnded = false
staticprivate

Definition at line 14 of file GameServer.java.

◆ playerO

PlayerHandler GameServer.playerO
staticprivate

Definition at line 12 of file GameServer.java.

Referenced by main().

◆ playerX

PlayerHandler GameServer.playerX
staticprivate

Definition at line 11 of file GameServer.java.

Referenced by main().

◆ PORT

final int GameServer.PORT = 8901
staticprivate

Definition at line 10 of file GameServer.java.

Referenced by main().


The documentation for this class was generated from the following file: