Lab 12x

burgerraraΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 4 χρόνια και 7 μήνες)

115 εμφανίσεις


Game Time With Friend!

A Remote Player Chess App

After completing this lab, you should be able to:

Use Java Net API, including Socket and ServerSocket;

Write simple client
server applications based on socket;

Know the basics of Java
multithreading mechanism;

Expand applications (with GUI, collections, and IO/Networking) using OO principles.

s Boardg
Night next Wednesday
You may want to bring something special to share
with your friends. The good thing is that we’ve created
a board game app in our class: the chess
board in Project 3. A few changes (redesign and extension) are needed to make it perfect for a
game for you and one of your friends play on your laptop or two lab machines in CSB. That
means we need to get to work n
ow, and it may last into this weekend.


First, let’s explore how to use socket to facilitate communicatio
ns between two separate

One of them plays the role of a server, which is running on a (server) machine
(identified by an IP or DNS name) and
bound to a port number, waiting for client request.
The other program, the client needs to use the know IP/host
name and port number to set up
a connection with the server process and request for a service as provided by the server.

[Use the Knock Knock Cl
ient and Server (as available under kkclient and kkserver folders) to
experience how it works. And read the code for both sides to see what Java classes are used
and how.]


Before adding socket
based features into your chess project, you will need to make
it take
user input for moving chess pieces.
For now, let’s assume users (such as you and your friend)
will follow the rules to play valid moves only.

Let’s try our best not to change your working project: one gold rule in OOAD is the OCP, or
closed pr
inciple. It requires your classes should be open for extension, but closed for
modification. Then how to make the change to allow the user to choose and move a piece?
Think about extension by subclassing that we learned early in the semester. You can write

PlayerChessBoardPanel class that extends your ChessBoardPanel (your class names may
vary). The new chess board can do whatever your original one can do, and it should allow a
user to move a piece by pressing on the
ChessBoardSquare in which the piece st
ands and
then pressing on the target square. How can you do it? Simply adding an action listener to
each of the squares which can handle the events accordingly. [See the PlayerChessBoard
Panel class under the chess folder for detail.]


Another experiment w
ill be helpful for us to see how a domain specific message such as a
chess move (including a start location and a target location, or more complicated as a castle
move that contains twice as much info) and sometimes just a textual content (such as “You
y white and please start the game.”). We may want to send Objects through the socket
connection, instead of sending simple String objects.

Let’s now learn how to use the Object
OutputStream and ObjectInputStream to replace the
PrintWriter and BefferedInput
Reader in the Knock Knock sample app. Again, we are going
to use new classes instead of modifying the existing working classes. [See samples.]


Now it’s time to extend our chess board project to implement the networking functions.
Since it’s entirely new,
it calls for new classes for this purpose:


A new class for creating the Socket instance and sending and receiving messages
(see the ChessGameConnect class in the chess.remote package).


Classes defining app
specific message types (such as a generic GameMes
class and its subclasses TextMessage and MoveMessage).


Also helpful is an interface
that specifies
what a class
that “subscribes to” incoming messages from the server need to do: more
specifically, update with move and text


Write a ChessBoardFrame class that implements the
interface and
uses the ChessGameConnect class to update the game.


Write a server class.


Update the server class such that it can take care of two clients to facilitate a g