This project is a simple client-server trivia game written in Java. The client and server communicate using TCP and UDP protocols, allowing multiple clients to join the game, answer questions, and track scores.
The project is organized into the following directory structure:
/trivia-game
├── src/ # Source files for client, server, and common classes
│ ├── Client/ # Client-side logic and GUI
│ │ ├── TriviaClient.java
│ │ ├── ClientWindow.java
│ │ ├── NetworkHandler.java
│ │ └── ...
│ ├── Server/ # Server-side logic
│ │ ├── Server.java
│ │ ├── ClientHandler.java
│ │ └── ...
│ └── Common/ # Shared classes between client and server
│ ├── Message.java
│ ├── Player.java
│ └── Question.java
├── out/ # Compiled .class files (output folder)
│ ├── Client/ # Compiled client-side classes
│ ├── Server/ # Compiled server-side classes
│ └── Common/ # Compiled common classes
└── README.md # Project documentation (this file)
src/: Contains the source code for both the client and server components, as well as the shared classes.out/: The compiled.classfiles will be placed here, organized by package structure.
- Java: Version 8 or above.
Clone the repository to your local machine:
git clone https://github.com/LJSigersmith/project2-trivia.git
cd project2-triviaTo compile the .java files and generate .class files in the out/ directory, use the following command:
javac -d out/ src/**/*.javaThis will compile all .java files from the src/ directory and place the resulting .class files into the out/ directory, maintaining the package structure.
Once the .java files are compiled into .class files, you can package them into a .jar file. Run the following command to create the JAR:
jar cmf src/META-INF/MANIFEST.MF trivia-game.jar -C out/ .This command creates a trivia-game.jar file, including all compiled .class files from the out/ directory and its subdirectories.
To run the JAR file, use the following command:
java -jar trivia-game.jarThis will launch the trivia game, and you can either start the server or connect as a client based on the program's prompts.
This document provides a high-level design of the server and client components for the Trivia Game. The system consists of two main parts: the server, which manages the game state and client interactions, and the client, which interacts with the player and sends/receives data to/from the server.
The system is designed using a client-server architecture, where:
- Server: Manages the game, processes client connections, sends questions, tracks scores, and handles game state.
- Client: Connects to the server, receives questions, answers them, and sends answers back to the server.
The server and client communicate using both TCP and UDP:
- TCP: Used for reliable communication, such as sending messages about game start, score updates, and answers.
- UDP: Used for faster, less reliable communication, such as sending polling messages and broadcasting questions.
The Server class is responsible for managing the entire game, including handling multiple client connections, sending questions, calculating scores, and managing the game flow.
-
Server.java:
- The main class that runs the server.
- Listens for incoming client connections via TCP and assigns each client a
ClientThread. - Coordinates game flow by sending questions and receiving answers.
- Keeps track of the game state and scores for each connected player.
-
ClientHandler.java:
- Manages communication with individual clients.
- Each client has a
ClientThreadrunning on the server, handling their messages and interactions. - Handles receiving answers, updating scores, and sending acknowledgments to the client.
-
ServerWindow.java:
- Provides a GUI for the server, allowing the server operator to see connected players, manage the game state, and view player scores.
- Displays the current question and answers, as well as logs of server activity.
-
Networking:
- The server listens on both TCP and UDP ports:
- TCP: Manages reliable, stateful communication with the clients.
- UDP: Used for sending polling requests and broadcasting messages to all clients.
- The server listens on both TCP and UDP ports:
- The server listens for incoming connections on a specified TCP port.
- When a client connects, the server spawns a new
ClientThreadto handle that client's requests. - The server sends a game start message to all clients, followed by the questions.
- Clients submit answers, and the server processes them to update scores.
- At the end of the game, the server sends the game over message with the final scores.
The Client class is responsible for interacting with the user, displaying questions, receiving answers, and communicating with the server.
-
TriviaClient.java:
- The main class that connects to the server via TCP and UDP.
- Sends a join message to the server when the client starts.
- Listens for incoming messages (e.g., questions, game status updates) from the server.
- Handles user input and submits answers back to the server.
- Displays the game’s UI for interacting with the player.
-
ClientWindow.java:
- The GUI for the client, displaying questions, options, and buttons for the user to interact with.
- Displays the score, timer, and poll/submit buttons.
- Handles the enabling/disabling of buttons based on game state (polling, answering).
-
NetworkHandler.java:
- Manages both the TCP and UDP connections between the client and server.
- Handles sending and receiving messages to/from the server.
- Listens for incoming messages from the server and processes them accordingly (e.g., question, score update, etc.).
-
UDPThread.java:
- Handles UDP communication for polling and broadcasting messages.
- Receives and processes server messages related to polling and game actions.
- The client connects to the server via TCP to join the game and receives game instructions.
- It listens for question messages from the server and displays the question to the player.
- The client sends a polling message when the player is ready to answer and waits for the server’s approval to proceed with the answer.
- When the player answers, the client sends the selected option to the server and updates the score based on the server’s feedback.
- Once the game ends, the client displays the final score and game results.
-
Server Setup:
- The server starts and waits for clients to join.
- When a client joins, the server assigns them a unique ID and sends a "game start" message.
-
Questioning Phase:
- The server sends the current question to all connected clients via TCP.
- Each client receives the question and polls the server to indicate their readiness to answer.
-
Answering Phase:
- After polling, the server grants permission for the client to submit an answer.
- The client sends the answer via TCP, and the server evaluates it.
- The server then updates the client with their score, and the next round begins.
-
Game Over:
- When all questions have been answered, the server sends a game-over message to all clients, along with their final scores.
-
TCP vs UDP:
- TCP ensures reliable message delivery, which is why it’s used for game-critical actions like submitting answers and scores.
- UDP is used for less-critical actions like polling and broadcasting questions to all clients.
-
Scalability:
- The server is capable of handling multiple clients simultaneously by spawning a
ClientThreadfor each connection.
- The server is capable of handling multiple clients simultaneously by spawning a
-
Error Handling:
- Both the server and client include basic error handling for network failures, such as disconnections or timeouts.
This design separates concerns between the server and client and uses both TCP and UDP protocols to efficiently manage game state and client interactions. The server manages the flow of the game, while the client handles user input and displays the game state.
When the client starts, it prompts the user to enter the server IP address. The client will attempt to connect to the server at the provided address. If the user does not input a valid IP address, the client will default to a pre-configured IP (e.g., localhost or 127.0.0.1).
Steps to Set the Server IP:
- When prompted at the start of the game, enter the IP address of the server.
- If left empty, the client will use the default IP (e.g.,
localhost).
Upon startup, the client also prompts the user to input their Client ID. This ID is used to uniquely identify each client in the game. The client ID must be an integer between 1 and 10 (inclusive). If the user enters an invalid ID (either non-numeric or out of range), the client will default to 1.
Steps to Set the Client ID:
- When prompted for the Client ID, enter an integer between 1 and 10.
- If the input is invalid, the default client ID (
1) will be used.
The .properties files in this project are used to store trivia questions and answers. Each file represents a set of questions in a key-value format, where:
questionXcontains the question text.optionsXcontains the multiple choice options for that question.correct_answerXspecifies the correct answer for the question.num_questionscontains the total number of questions in the file.
question1=What is the largest country by area in the world?
options1=Russia, Canada, China, United States
correct_answer1=Russia
question2=Which river is the longest in the world?
options2=Amazon River, Nile River, Yangtze River, Mississippi River
correct_answer2=Nile River
num_questions=2- Create a new
.propertiesfile (e.g.,myquestions.properties) in thequestionsdirectory. - For each question, define the
questionX,optionsX, andcorrect_answerXproperties, whereXis the question number. - Add the
num_questionskey at the end of the file to specify the total number of questions in the file. - Ensure each question has four options, and the
correct_answerXmatches one of the options. - To load this custom question set, ensure the server is configured to load the desired
.propertiesfile when prompted.
Example of custom question file (myquestions.properties):
question1=What is the capital of France?
options1=Paris, London, Berlin, Rome
correct_answer1=Paris
question2=Who wrote 'Romeo and Juliet'?
options2=Shakespeare, Dickens, Twain, Austen
correct_answer2=Shakespeare
num_questions=2- By default, the game uses a built-in
.propertiesfile located in thesrc/questionsfolder. If you want to use a custom file, ensure the.propertiesfile is located in thesrc/questionsfolder.