Problem. Write a networked program to run a game of Janken or paper, stone, scissors. In the basic version a single player (client) plays against an automated server and their moves are relayed via a Socket connection. The task will require a range of basic Java programming, but concentrates on Socket level networking.

Basic Program. You must write two programs a JankenServer and JankenClient, possibly relying on additional classes to manage the state of the game.

  • Sockets The first thing the players must do is enter their names. The Client and Server must set up a connection and exchange this information. I strongly advise you to get this working by modifying the SocketTest.java and EchoServer.java from the course exercise codes before moving on to the game aspects of the assignment. You should have this part complete and tested first.
  • Play The game consists of players choosing one of paper, stone or scissors. Paper beats stone, stone beats scissors and scissors beat paper. If both players make the same choice there is a draw and the play is repeated until one player wins. In the basic version the server makes random choices. Before writing code, think carefully about the pattern of messages and the way they are to be encoded.
  • Publishing Results The results of each game are published on a Web server. This is accomplished simply by the server writing (appending) the win/loose result and player name to a local file which happens to lie in the publishable directory of a Web server running on the same machine.

Notes

  • Study the codes SocketTest.java and EchoServer.java that are available from the course exercises, and use them as a basis for your own code.
  • Use telnet and write your own short codes for testing network connections as you proceed. It is also helpful for diagnostics to print on screen whatever you send or receive from the network.
  • Use Math.random() to generate a random number between 0 and 1 in order to let the Server select moves automatically.
  • It is possible to test the whole setup on a single machine. Download the TeenyWeb.java Web server from the course exercise codes and run it on some port (e.g. 38187), a file junk.html should be visible to a browser, or other test program on http://localhost:38187/junk.html. The JunkenServer should be started on another port (e.g. 38189) that is used for the Socket connection. In the Solaris lab, you can run Client and Server on separate machines and rely on the University Web server by putting junk.html in your public_html directory (provided permissions have been set correctly).
  • Exceptions should be treated carefully, as there are lots of potential problems when running on a network.

Examples of Program Extensions. A full solution to the basic game will give you a comfortable pass for this programming assignment component but ‘merit’ and ‘distinction’ levels of assessment will be reserved for extensions to this basic scheme. Some suggestions are given below but please neither feel that you have to attempt them all nor feel restricted to these. If you are unsure about a particular extension you have in mind, email me.

  • Arrange for the Client to contact the Web server using the URLConnection class to show past scores.
  • Publish the results as an html file – not quite as straightforward as it sounds.
  • Use the sample JankenGUIClient.java available from the course exercise codes.
  • Allow multiple players to play against the server. Either independently, or all responding to the same server moves so that in each round, only those who drew or won continue to the next round. The second possibility is substantially more difficult and may involve some synchronization.
  • Allow players to play (in pairs) directly against each other. In this case the clients still first connect to the server which then exchanges their contact information but play occurs through a direct connection between the players.

SocketTest.java

import java.io.*; import java.net.*; import java.util.*; /** * This program makes a socket connection to the atomic clock in Boulder, Colorado, and prints the * time that the server sends. * @version 1.20 2004-08-03 * @author Cay Horstmann */ public class SocketTest { public static void main(String[] args) { try { Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13); try { InputStream inStream = s.getInputStream(); Scanner in = new Scanner(inStream); while (in.hasNextLine()) { String line = in.nextLine(); System.out.println(line); } } finally { s.close(); } } catch (IOException e) { e.printStackTrace(); } } }

EchoServer.java

import java.io.*; import java.net.*; import java.util.*; /** * This program implements a simple server that listens to port 8189 and echoes back all client * input. * @version 1.20 2004-08-03 * @author Cay Horstmann */ public class EchoServer { public static void main(String[] args) { try { // server socket is not a Socket // ServerSocket svrs = new ServerSocket(8189); ServerSocket svrs = new ServerSocket(32112); // wait for client connection Socket s = svrs.accept(); try { InputStream inStream = s.getInputStream(); OutputStream outStream = s.getOutputStream(); Scanner in = new Scanner(inStream); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); out.println("Hello! Enter BYE to exit."); // echo client input boolean done = false; while (!done && in.hasNextLine()) { String line = in.nextLine(); out.println("Echo: " + line); if (line.trim().equals("BYE")) done = true; } } finally { s.close(); } } catch (IOException e) { e.printStackTrace(); } } }
Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.