Lab assignment 3 - Rock, paper, scissorsA grafic client program where a user plays against a serverIn lecture 5 or 6 a server program was written which plays "rock, paper,
scissors" with its clients. The code is available
here. Your task is to write a graphical client program using Swing
components, namely This program connects to a server. If the server is down, start it on the computer you're working on. Intructions for running a local server are available here: localserver.html Server program free from swedish letters. A version of the server without åäöÅÄÖ runs through port 4713, its code is available here: Server4713.java. It plays STEN, SAX, PASE. Of course you may copy it and replace the swedish words with english.A simple clientStart off by making a client without the graphics, using the following code to open a connection to the server.try{ Socket socket=new Socket("p6-ray.nada.kth.se",4712); BufferedReader in=new BufferedReader(new InputStreamReader( socket.getInputStream())); PrintWriter ut=new PrintWriter(socket.getOutputStream()); ut.println("Charlotta"); ut.flush(); System.out.println(in.readLine()); }If the server is down, try the one at rungner by replacing "p6-ray.nada.kth.se" with "rungner.nada.kth.se". The client should send your name to the server listening on port 4712 and display the answer from the server. Remember to do ut.flush() , or the name won't be sent. The code above
must be supplemented before it runs and in order to test the server.
Send a few messages to the server and verify that it answers with
"STEN", "SAX" or "PÅSE" (or your replacements, if you chose to
change the server). To close the connection, send some input that
the server reads as null or as the empty string.
You can e.g. use an empty
String (""), Ctrl-D och Ctrl-Z. Once the simple client works
you can focus completely on the graphical interface.
The Swing componentsYour program could be apublic class Client extends JFrame implements ActionListener{where the left half of the window is your game area and the right half is that of the computer. Use BoxLayout since this often gives good results.
Each game area is a vertical Box which contains, in order from
top to bottom:
JLabel
but JTextField is sometimes preferred when you create
components that are initially empty (no text). It is mainly a matter
of taste which you use.
Define a class Box box=Box.createHorizontalBox();you can do this: GameArea me=new GameArea("Me:"); GameArea you=new GameArea("Computer:"); box.add(me); box.add(Box.createHorizontalStrut(20)); //Spacer box.add(you);The Box-object may now be added to the JFrame by calling the method add. JPanel and GridLayoutIf Java and its graphic components are new to you, it may be a better idea to use a JPanel with a GridLayout. This layout is more basic and easier to manage but less flexible thanBoxLayout but it is certainly
possible to create
a nice looking layout with this one too! The game area should be divided
into five fields where the tree buttons will occupy the three middle fields.
For the upper and lower fields, use JPanel s with
GridLayout with subdivision into two or more fields.
The gameThe user clicks the "ROCK", "PAPER" and "SCISSORS"-buttons and the third click is registered as the user's move. The two preceding clicks must be counted and texts "ONE..." and "TWO..." respectively displayed in the result field. The third click also causes the computer to make its move and the program to settle the winner or that the game is undecided (no winner or loser). Fields for Message, Result and Score must be updated. The game goes on forever if you do not program it to stop which is up to you.
Hints and advice
Lab demonstration
As extra assignment, choose one of the following::Extra assignment, alt. 1 Add sound effects! This may require headphones on some computers. There must be at least three different sounds (move, win, lose). It must be possible to interactively turn the sound on and off while playing.Extra assignment, alt. 2 Extends the graphical interface of the program so that the server connection is initiated interactively. Let the user choose server and port number, either from predefined alternatives or by writing into TextFields. |