Results 1 to 2 of 2
  1. #1
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default 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();
          }
       }
    }

  2. #2
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Rock, Paper, Scissor Game Help
    By CYANiDE in forum New To Java
    Replies: 6
    Last Post: 10-28-2009, 05:20 PM
  2. Rock Paper Scissors
    By 54byler in forum Advanced Java
    Replies: 2
    Last Post: 04-23-2009, 07:23 AM
  3. trying to get code in paper form....
    By machine949 in forum New To Java
    Replies: 1
    Last Post: 04-22-2009, 06:09 PM
  4. Need Help with Rock paper and Scissors Java Game
    By kingsun in forum New To Java
    Replies: 3
    Last Post: 11-17-2008, 04:35 AM
  5. Need help with Rock Paper Scissors Game
    By GettinGwap in forum New To Java
    Replies: 12
    Last Post: 10-19-2008, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •