0% found this document useful (0 votes)
46 views

Cse 438 L

The document contains code for a server and client for a rock paper scissors game. The server code defines variables for the port number and version, gets the port from the user, then continuously accepts connections from two clients. It reads their inputs, determines a winner, and sends each client their result. The client code connects to the server, gets input from the user, sends it to the server, and displays the result received from the server.

Uploaded by

Zobayer Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Cse 438 L

The document contains code for a server and client for a rock paper scissors game. The server code defines variables for the port number and version, gets the port from the user, then continuously accepts connections from two clients. It reads their inputs, determines a winner, and sends each client their result. The client code connects to the server, gets input from the user, sends it to the server, and displays the result received from the server.

Uploaded by

Zobayer Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Server.

java

package PaperScissorsStone;

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Server {

    private static Integer port = 1337;

    private static Double versionNumber = 1.0;

    private static String welcomeMsg = "--- Welcome to Paper Scissors Stone Server V. " + versionNumber
+ " --- \n";

  

    private static boolean validPort(Integer x) {

        return x >= 1 && x <= 65535 ? true : false;

  }

    private static int getPort() {

        Integer input;

        Scanner sc = new Scanner(System.in);

        do {

            System.out.print("Please select a port by entering an integer value between 1 and 65535 or\n");
            System.out.print("insert \"0\" in order to continue with the default setting (" + Server.port + "): ");

            input = sc.nextInt();

        } while (input != 0 && !Server.validPort(input));

        sc.close();

        return input == 0 ? Server.port : input;

  }

    public static void main(String args[]) throws Exception {

        String resClient_1 = "";

        String resClient_2 = "";

        String inputClient_1;

        String inputClient_2;

        System.out.println(Server.welcomeMsg);

        Server.port = Server.getPort();

        ServerSocket welcomeSocket = new ServerSocket(Server.port);

        System.out.println("\nOk, we're up and running on port " + welcomeSocket.getLocalPort() + " ...");

        while (!welcomeSocket.isClosed()) {

            Socket client_1 = welcomeSocket.accept();

            if (client_1.isConnected()) {

                System.out.println("\nPlayer one (" + (client_1.getLocalAddress().toString()).substring(1) + ":"


                        + client_1.getLocalPort() + ") has joined ... waiting for player two ...");

      }

            DataOutputStream outClient_1 = new DataOutputStream(client_1.getOutputStream());

            BufferedReader inClient_1 = new BufferedReader(new


InputStreamReader(client_1.getInputStream()));

            Socket client_2 = welcomeSocket.accept();

            if (client_2.isConnected()) {

                System.out.println("Player two (" + (client_2.getLocalAddress().toString()).substring(1) + ":"

                        + client_1.getLocalPort() + ") has joined ... lets start ...");

      }

            DataOutputStream outClient_2 = new DataOutputStream(client_2.getOutputStream());

            BufferedReader inClient_2 = new BufferedReader(new


InputStreamReader(client_2.getInputStream()));

            inputClient_1 = inClient_1.readLine();

            inputClient_2 = inClient_2.readLine();

            if (inputClient_1.equals(inputClient_2)) {

                resClient_1 = "Draw";

                resClient_2 = "Draw";

                System.out.println("It's a draw.");

      }

      

            else if (inputClient_1.equals("R") && inputClient_2.equals("S")) {

                resClient_1 = "You win";

                resClient_2 = "You lose";

                System.out.println("Player one wins.");


      }

            else if (inputClient_1.equals("S") && inputClient_2.equals("R")) {

                resClient_1 = "You lose";

                resClient_2 = "You win";

                System.out.println("Player two wins.");

      }

      

            else if (inputClient_1.equals("R") && inputClient_2.equals("P")) {

                resClient_1 = "You lose";

                resClient_2 = "You win";

                System.out.println("Player two wins.");

      }

      

            else if (inputClient_1.equals("P") && inputClient_2.equals("R")) {

                resClient_1 = "You win";

                resClient_2 = "You lose";

                System.out.println("Player one wins.");

      }

            else if (inputClient_1.equals("S") && inputClient_2.equals("P")) {

                resClient_1 = "You win";

                resClient_2 = "You lose";

                System.out.println("Player one wins.");

      }

      

            else if (inputClient_1.equals("P") && inputClient_2.equals("S")) {

                resClient_1 = "You lose";

                resClient_2 = "You win";

                System.out.println("Player two wins.");

      }
            outClient_1.writeBytes(resClient_1.toUpperCase());

            outClient_2.writeBytes(resClient_2.toUpperCase());

            client_1.close();

            client_2.close();

            System.out.println("\nWaiting for new players ...\n");

    }

  }

Client.java

package PaperScissorsStone;

import java.io.*;

import java.net.*;

class Client {

    private static String host = "localhost";

    private static Integer port = 1337;

  
    private static Double versionNumber = 1.0;

    private static String msgWelcome = "--- Welcome to Paper Scissors Stone V. "

        + versionNumber + " --- \n";

    private static String msgRules = "\nRule set:\n - (R)ock beats (S)cissors\n - (S)cissors beats (P)aper\n -
(P)aper beats (R)ock\n";

    public static void main(String args[]) throws Exception {

    String input = "";

    String response;

    System.out.println(Client.msgWelcome);

    BufferedReader inFromUser = new BufferedReader(new InputStreamReader(

        System.in));

    Socket clientSocket = new Socket(Client.host, Client.port);

    DataOutputStream outToServer = new DataOutputStream(

        clientSocket.getOutputStream());

    BufferedReader inFromServer = new BufferedReader(new InputStreamReader(

        clientSocket.getInputStream()));

    do {

        if (input.equals("-rules")) {

        System.out.println(Client.msgRules);

    }
        System.out

            .println("Start the game by selecting (R)ock (P)aper, (S)cissors");

        System.out.print("or type \"-rules\" in order to see the rules: ");

        input = inFromUser.readLine();

    } while (!input.equals("R") && !input.equals("P") && !input.equals("S"));

    // Transmit input to the server and provide some feedback for the user

    outToServer.writeBytes(input + "\n");

    System.out

        .println("\nYour input ("

            + input

            + ") was successfully transmitted to the server. Now just be patient and wait for the result ...");

    response = inFromServer.readLine();

    System.out.println("Response from server: " + response);

    clientSocket.close();

  }

You might also like