使用Java代写一个网络游戏,包含网络通信,以及使用 Java Swing
实现满足 MVC
模式的界面。
Goal
- Ability to use Java Networking API to facilitate communication between a server and client application over the network
- Implement a simple application layer network protocol
- String manipulation
- Understand and use the Model-View-Controller (MVC) architecture pattern for GUI programming
- GUI programming with Java Swing
Introduction
In this project you are going to learn and use the Networking API in Java.
This is a vital component technology in the Java platform stack as
communication between applications over the network is inevitable in modern
software. In fact, the vast majority of applications we use today interact
with a remote service in one way or another. Understanding how to have two
independent programs connect with each other over the network using a
client/server model is an important skill.
We will learn this skill by creating a simple version of a popular game called
Psych. Psych is a simple multiplayer party game based on the idea of
outwitting people by fooling them into picking fake or incorrect words for a
given description. The version we will implement, allows the players to be
dispersed anywhere in the world, as long as they have an internet connection
and can run a Java program. A version of this game is available as an app for
both the Android and iOS platforms, with the name Psych! - you are strongly
encouraged to download this free app and play a round or two to get a sense of
how the game works.
For the purposes of this project, the game will work as follows. The game has
two separate programs that play the role of the server and the client,
respectively. For this project, you will be given the server component, and
will have to implement the Client program. In future projects, you will
implement the Server.
From the Client’s perspective: When the Client is first launched, the user has
an option to either login to the server with an existing user and password
combination, or register a new user and password combination. The Server is
designed to remember previously
created user and password combinations in a file (more details below). Once a
user is created, and logged into the game, the player has an the option to
either start a new game as the leader of the game, orjoin an existing game.
If the user chooses to start a new game, the server creates a special token
for this new game and sends that to the leader. The leader shares this token
with any other player that wants to join that game. If the user chooses
instead to join an existing game, they need to enter the token for that game.
Each new user that joins a game is displayed to the leader who can start
playing the game when all players have joined.
The game proceeds in rounds. In each round the server picks a description for
an uncommon word and sends it to each of the participants in the game (the
leader and all other players that have joined the game). Each participant
displays this description and allows the player to enter a suggested word for
this description. Once a participant enters their word, it is sent to the
server. The server collects the suggestions from each participants, and sends
them all, along with the correct answer that it already knows, to every
participant (the suggestions are in randomized order). Each participant shows
these suggestions and the real answer to the player who then tries to pick the
correct answer. Each participants selection is send to the server. When the
server receives selections from every participant, and assigns scores to each
participant based upon their selections. Players get points for picking the
correct answer, and also if another player was fooled into picking their fake
suggestion instead of the correct answer. The game goes on until the server
runs out of words or the players decide to quit.
For this project, your assignment is to write a Java Swing GUI-based
application for the FoilMaker! Client. When running your program, you will
first start the Server program supplied to you. Each client that runs connects
to this server and follows the steps of the game outlined above. The
communication between the server and the client will follow a protocol that is
provided to you (described below). This protocol specifies the contents of the
messages between the clients and the servers.
Client Implementation
In this section we have an outline of wireframes for the client GUI and
corresponding commands in the protocol. You should use Java Swing API to
implement the GUI. You are strongly advised to structure the Client using the
Model-View-Controller pattern described in the Appendix.
The protocol has the following generic format.
The “-“ is used in this protocol to separate the various parts of each
message. The allowed values for the Command, and other tokens of these
messages are described below. The constants used in these message are defined
in the FoilMakerNetworkProtocol.java file.
Register New User
Users need to be registered with the server before they can play the game.
Once registered, the user credentials (i.e. username and password) are saved
by the server in the UserDatabase” file so that the details will not be lost
even if the server is restarted. The Username or Password cannot be empty. The
client should enable the user to enter a login and password which are then
sent to the server in a “CREATENEWUSER” message. Any error messages should be
displayed to the client. If the user creation is successful, an appropriate
message should be shown to the user.
Hint: you may want to Lookup the JTextField and JPasswordField classes.
User Login
A registered user should log into the system before playing the game. The
client allows the user to enter a login name and password, which it sends to
the server using “LOGIN” message. Any error messages from the sever should be
displayed to the user. If the login is successful, the server will provide a
unique token (session cookie). The cookie is saved in the client and will be
sent in future messages to the server to show that the message is from a
logged-in client.
Play Game
Once a user logs into the system successfully she can either start a new game
or join an existing game.
Start a New Game
A user that starts a new game becomes the leader of that game session. The
user token field below is the session cookie for this user received when the
user logged in to the server. If successful in creating a new game, the server
will respond with the game token (three character string). This token should
be saved by the client and will be used in subsequent requests. The leader
view should now display this game token, and a list of participants that have
joined this game. The leader shares this token (outside the game) with any
other users that want to join the game.
The leader should wait for a fixed number of players (at least one other
player) to join before she starts the game. Once all the participants have
joined, the “Start Game” button on the leader’s view becomes enabled allowing
the leader to start the game. When a player joins the game, the leader is
informed by the server of the new participant in the game (see below). You
should update the leader’s view (see the figure above) as and when new players
join in. You may hardcode the numbers of players you would like to have in a
game.
Join a Game
Player trying to join a game should wait for the leader to start the game. The
leader will share with each such player the game key either verbally, or
through some other means (text message, snapchat etc. - not using this game
itself). The clients sends a “JOINGAME” message to the server with the
player’s login token, and the game key. Any error messages should be displayed
to the user. If the joining is successful, the player’s view should change to
the view below indicating “Waiting for leader …”. This means that we are
waiting for the leader to launch the game. For each user that successfully
joins a game, the leader of the game receives a “NEWPARTICIPANT” message with
the name and score of the new user. This user should be added to the list of
participants in the leader’s view.
Launch Game
When the leader is ready to start the game, she clicks on the “Start Game”
button. This causes the leader’s client to send a “ALLPARTICIPANTSHAVEJOINED”
message to the server. Any error messages from the server should be displayed
to the leader’s view.
When the server managed to launch the game successfully it will not send a
SUCCESS code in this case rather it will start the game.
The game consists of several rounds. Each round has the following Steps:
- The server sends the new description to each participant
- Each participants displays the description and waits for its user to enter a suggested word for this description which is sent to the server
- The server waits for a suggestion from each participant. Once they are all received, it adds the correct answer, scrambles the suggestions and the correct answer and sends them to each participant.
- Each participant waits for the suggestions from the server, and then shows them to the user. Once the user picks a suggestion it is send to the server
- The server waits for each participant to send its choice, then scores each participant and sends a message with all participant names and scores to each participant. After this it also sends to each player either the word for the next round in a “NEWWORD” message, or a “GAMEOVER” message when there are no more words.
- Each participant displays the names and scores of each participant in the game and waits for the user to be ready to play the next round. When the player is ready, the client shows the word for the next round
Server sends the first round’s word, and the correct answer to each
participant’s client (so that the client application can prevent a user from
using it as a suggestion) according to the following format.
Each client receives this command from the server so that client application
can show it on its GUI as follows.
Send Player’s Suggestion
Each client can send her suggestion as follows.
Once the server receives suggestions from all the players it will send them
along with the correct answer to each client so that it can show them to the
user. The user is going to pick an answer from this list in the next step.
Furthermore if there are N players (including leader) in a session there will
be N + 1 answer options on this list.
Send Player’s Choice
Once the user picks her choice you can send it to the server as follows. Once
the server receives all the answers and finishes processing them it will send
the results to each client as given in the next section.
Receive Results
This is the format of the message that includes overall results of the game so
far.
Please note that each player is going to get the same message that includes
overall results of all the players. When you display results you can show the
message that is intended to a given player separately and overall results of
each play in tabular form (see below).
Go to Next Round
The server sends next question (NEWGAMEWORD message) as soon as it has sent
results to all the clients (ROUNDRESULT command). So the clients don’t have to
send a special message to move on to the next round.
However the server sends GAMEOVER-command as it runs out of questions. Clients
can react to this command appropriately and quit. For instance disable the
“Next Round” button and set status message as follows.
Quit Game
The client application should be able to quit the game whenever the user
wishes. You can either have a button to do that or just simply close the GUI.
When the client quits it should send LOGOUT-command to the server as follows.
How to Run the Server
You can test your client application against the given server implementation.
Please make sure the given files, FoilMaker.student.jar, UserDatabase, and
WordleDeck reside in the same directory. The given user store (UserDatabase)
already includes 2 users (Alice and Bob). The wordle file includes one
question and answer. Run the following command in a terminal to launch the
server. Your clients will have to create network connections with this server
using the the port number that you specify below and the IP address of the
machine on which this server is run.
java -jar FoilMaker.student.jar