bild
Skolan för
elektroteknik
och datavetenskap
 

Lab assignment 3 - RPS-client (ROCK, PAPER, SCISSORS)

Goals

  • To understand the principles for a Java server program handling several clients.
  • To know how to program a text client and a graphical client that connect to the server, communicate with the server and disconnect.
  • To understand how to use different listener objects for different listening tasks in the program.
In addition, this assignment provides additional training on structuring the logical part of a program and on GUI:s.

A graphical client program where a user plays against a server

In lecture 8, a server program playing ROCK-PAPER-SCISSORS with connecting clients is demonstrated. Here is the code: Server4713.java. Your task is to write the graphical client program where a user can play against the server. The game is well known. On the web page Rock, paper, scissors is various information, e.g. three gif images. The images are also availble here on the lab web page a bit further down.

Ther server program is currently (2014-04-20 at 13.10) running on u-shell.csc.kth.se, port 4713.

If you are unable to connect to the server (it is not running or your internet is temporarily unavailable), you may as an emergency solution run the server program locally, on your own computer. Instructions are here: localserver.html

If you suspect that the server program is not running at all, please notify the course responsible person (Ann).

A text client

Start out by testing a simple text client that opens a connection with
   try {
       Socket socket=new Socket("gru-ld03.csc.kth.se",4713);
       BufferedReader in=new BufferedReader
           (new InputStreamReader(socket.getInputStream()));
       PrintWriter ut=new PrintWriter(socket.getOutputStream());
       ut.println("Charlotta"); ut.flush();
       System.out.println(in.readLine());
   }
The client sends a name (here it is "Charlotta") to the server listening on port 4713 on the chosen computer (gru-ld03 ...). The client then prints the answer sent by ther server. Remember to "flush" the output! (ut.flush();). Include the code above in an executable program. Write a class with only the method main(). Make some additional prints and readlines in order to see how it works. To disconnect from the server, send null, the empty String ("") or one of the characters Ctrl-D or Ctrl-Z. Make sure the simple text client works before you move on to the graphical client.

The graphical program

The program must have at least two separate classes (an inner class is not considered "separate"). One of the classes represents the board and there must be one board object for the user and one board object for the computer. The following detailed instructions are based on the supplied class Gameboard. It is not required that you use Gameboard, but the intention is to make the task easier for you by giving you this class.

A class for the board

There must be one board for yourself and one for the computer. These must be objects of the same class. The board must contain seven fields. From top to bottom these are: name of the player, last move, buttons for ROCK, PAPER, SCISSORS, a field with win,lose or draw and finally a field with the current score. All fields except the topmost one may be updated during the game. The user plays by clicking its buttons. Clicking on the computers buttons has no effect but the computers moves must visible on the buttons (explained below). Use class Gameboard for the board. Please note that image files must be available. Use the following or find images of you own choice! Some students have experienced problems with the images when using the Eclipse IDE. If the images are stored in another directory than the program, the path to the images in Gameboard should be changed. The given class assumes that code and images are in the same directory.
STEN PÅSE SAX

The main class

A "skeletal" version of the main program is provided. Run this program and you will get a conception of what the final program should look like. RPSSkel. Download both classes and some image files and run the program. Extend the skeletal program in small steps to the final one.

The game

When playing the RPS game, the user presses the buttons and at the third click, the program will detect which button is clicked, register which of ROCK, PAPER and SCISSORS that was chosen. It does not matter which of the buttons are pressed the first two times but texts "ONE ..." and "TWO ..." must appear int the result field. The inoperative pressings are meant to resemble the routine of the manual game where you beat your fist in the air twice (counting one, two) and after that (on three) you show your chosen sign. At the users third pressing, the computer also shows its sign (i.e. the program reads from the server). The program determines if it is a draw or who has won and updates the result fields and the score field(s).

Requirements

  • When user and computer are playing (i.e. every third button pressing), the chosen signs must be shown on the boards by changing the background color of the appropriate button and by printing the text ROCK, PAPER or SCISSORS in the field above the buttons. On the next button clicking ("ONE ..." in the next move) the colour is changed back to original.
  • In the field just under the button, display texts "ONE ...", "TWO..." and on three, display if the player won, lost or if it was a draw.
  • The score must be updated in the bottommost field (for the winner).
  • In each game, the program must detect the winner (or draw). Do this in a simple and clear manner! It is not necessary to test all combinations of ROCK/PAPER/SCISSORS. Two if-statements (and some && and ||) are sufficient. Another option is to create an auxiliary data strucure that allows you to find the winner without testing.
  • The GUI must have a button for closing the program. Pressing this button disconnects the client from the server and stops the program. The button is inluded in the skeletal program but has no listener.
  • Let different listeners listen to the close-button and the game-buttons! They have different roles in the program. Suggestions on how to do this: read here.

Redovisning

  • Demonstrate your program. The requirements in the above list must be met.
  • You must be able to explain all parts of the program, also the given class Gameboard, if you have chosen to use it.
  • Show a UML class diagram for the program. The diagram must contain the most important classes and interfaces and all public methods (constructors excluded). Variables may be left out.

 

Extra assignment for higher grades

Content: Extend the GUI with some functionality. Choose ONE of the following:

Alternative 1: Add sound effects! Let the user choose in the GUI if he/she wants sound effects or not. With sound ON, let it be different sounds for win, lose and draw (and optionally for other things). It must be possible to choose sound OFF in the GUI at any time during the game.

Alternative 2: Extend the GUI so that the server connection is done in the GUI. Let the user choose server computer and port number, either by typing them into text fields or by a menu choice. If connection fails, the program must say so and not "crash".

Copyright © Sidansvarig: Ann Bengtsson <ann@nada.kth.se>
Uppdaterad 2014-05-09