Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Stone, Paper, Scissors Game Help


Hi everyone,
I wrote this programme as its part of my studies project, I have coded the Client and the Server both.
this programme shoudl workd this way:

Enter your name
Harris
Enter your move stone, paper, scissors
paper
Server chose : stone
round: 1 server stone, client paper, Result 1
you won

and in case of draw

Enter your name
Harris
Enter your move stone, paper, scissors
paper
Server chose : paper
round: 1 server paper, client paper, Result 1
Draw: play again

the problem I am facing is that it gives the choice for only one move, even in case of a draw the programme both ends, but i want that when its a draw the client get onther move up to the time that there is a result, I am posting my client and server code hope someone can help with it.

Server 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 */);


            String serverMove=getServerMove();
            out.println(serverMove);


           //out.println("Hello! You are connected to the Server");

           String name = in.nextLine();
           System.out.println(name);

           String ClientMove = in.nextLine();
           System.out.println(ClientMove);

          String win=determineRoundWinner(ClientMove, serverMove);

           //int winner=determineRoundWinner(ClientMove,serverMove);
           out.println(win);
        
         }
         finally
         {
            s.close();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }

    static String getServerMove() {
double compMoveInt;
String compMove;

// Generate a random move for the
// computer.
compMoveInt = Math.random();

// Convert the random integer into a
// string that represents the computer's
// move.
if(compMoveInt >= 0.0 && compMoveInt <= 0.33) {
    compMove = "stone";
}
else if (compMoveInt >= 0.3 && compMoveInt <= 0.6) {
    compMove = "paper";
}
else {
    compMove = "scissors";
}

return compMove;
    }
        /**
     * Compare the user's move to the computer's
     * move to determine the winner of this round.
     *
     * @param userMove the user's move.
     * @param compMove the computer's move.
     * @return  1 if the computer wins.
     *          0 if it is a tie.
     *         -1 if the user wins.
     */
    static String determineRoundWinner(String ClientMove,
    String compMove) {
String win = null;
// Check for ties.
if (compMove.equals(ClientMove)) {
   win="Draw";
}

// if it is not a tie check for all the ways the
// computer can win.
// Rock smashes scissors...
else if (compMove.equals("stone") &&
ClientMove.equals("scissors")) {
    win="Compuer Wins Better Luck Next Time :)";
           
}
// Paper covers rock...
else if (compMove.equals("paper") &&
ClientMove.equals("stone")) {
    win="Compuer Wins Better Luck Next Time :)";
}
// Scissors cut paper...
else if (compMove.equals("scissors") &&
ClientMove.equals("paper")) {
    win="Compuer Wins Better Luck Next Time :)";
}
// Its not a tie and the computer did not
// win so the user must have won!
else {
    win="You Win ";
}

return win;
    }
   
     
}

Client 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");
                    //result=null;
                    Scanner user = new Scanner(System.in);
                    String UserChoice = user.nextLine();

                    //sending User Choice to the Server
                    out.println(UserChoice);

                    //Catching server move to screen
                    String ServerMove = in.nextLine();
                    //System.out.println(ServerMove);

                    System.out.println ("Your move = " + UserChoice + " Server move = "+ServerMove);

                    String winnerResult = in.nextLine();
                    if(winnerResult.equals("Draw"))
                    {
                        System.out.println("Its a Draw try again");
                    }
                    else
                    System.out.println(winnerResult);
                   
    }

            while (in.hasNextLine());
            {

            }

         }
         finally
         {
     //       s.close();
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}