Results 1 to 2 of 2
Thread: Stone, Paper, Scissors game help
- 12-18-2009, 11:08 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
Stone, Paper, Scissors game help
I am trying to code the famous stone, paper, scissors game, I am posting my client and server code, the problem i am facing is I dnt know how to send the server choice which I created using Math.random and if statment through the network to the client, and then compare it with client move and show on screen the result can anyon help me with this please?
tnx in advance
Server Code
Java Code: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); // wait for client connection Socket s = svrs.accept(); try { InputStream inStream = s.getInputStream(); OutputStream outStream = s.getOutputStream(); /** * Once the client is connected to the server, to make sure that the connection is well established * the Server sents a message of confirmation to the client"Hello you are connected to the Server" */ Scanner in = new Scanner(inStream); PrintWriter out = new PrintWriter(outStream, true /* autoFlush */); //out.println("Hello! You are connected to the Server"); String name = in.nextLine(); System.out.println(name); } finally { s.close(); } } catch (IOException e) { e.printStackTrace(); } } public void Serverchoice(){ double serverChoice; serverChoice = Math.random(); if(serverChoice >= 0.0 || serverChoice <= 0.33) { String ServerMove = "stone"; } else if (serverChoice >= 0.3 || serverChoice <= 0.6) { String ServerMove = "paper"; } else if (serverChoice >= 0.6 || serverChoice <= 1.0) { String ServerMove = "scissors"; } } Scanner sc = new Scanner(System.in); String ServChoice = sc.nextLine(); }
Client Code
Java Code: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) { Object result; try { //Socket s = new Socket("wwwcache.westminster.ac.uk", 3128); //Socket s = new Socket("161.74.26.25", 8080); /** * Creating a new cokkect called s, and it will connecte to localhost port 8189 */ Socket s = new Socket("localhost", 8189); try { InputStream inStream = s.getInputStream(); Scanner in = new Scanner(inStream); PrintWriter out = new PrintWriter(s.getOutputStream(),true); // String line = in.nextLine(); // System.out.println(line); /** * Asking for player's Name and then send it to the server */ System.out.println( "Enter Your Name"); Scanner sc = new Scanner(System.in); String playerName = sc.nextLine(); out.println(playerName); //String plaame = sc.nextLine(); do{ System.out.println("Enter stone, paper or scissors"); String UserChoice = sc.nextLine(); out.print(UserChoice); System.out.println ("Your move = " + UserChoice + "Server move = "); } while (in.hasNextLine()); { } } finally { // s.close(); } } catch (IOException e) { e.printStackTrace(); } } }
- 12-19-2009, 04:33 PM #2
Member
- Join Date
- Dec 2009
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
Rock, Paper, Scissor Game Help
By CYANiDE in forum New To JavaReplies: 6Last Post: 10-28-2009, 04:20 PM -
Rock Paper Scissors
By 54byler in forum Advanced JavaReplies: 2Last Post: 04-23-2009, 06:23 AM -
trying to get code in paper form....
By machine949 in forum New To JavaReplies: 1Last Post: 04-22-2009, 05:09 PM -
Need Help with Rock paper and Scissors Java Game
By kingsun in forum New To JavaReplies: 3Last Post: 11-17-2008, 03:35 AM -
Need help with Rock Paper Scissors Game
By GettinGwap in forum New To JavaReplies: 12Last Post: 10-19-2008, 06:15 PM


LinkBack URL
About LinkBacks

Bookmarks