Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here’s your task: You’re going to create exactly two classes. One class will act

ID: 3696540 • Letter: H

Question

Here’s your task: You’re going to create exactly two classes. One class will act as the functional server. The main method of this server will setup the connection on a localhost, find the two clients, let them know which one is Player 1 and which one is Player 2 (depending on the order they connect), and then manage the game as it’s being played.

This is important: For each player’s turn they should start by displaying their current score to the user. Then, the user should be prompted to select whether they want to “Hit” or “Stand”. This option selection can be done any way you choose, but invalid entries must be accounted for*. If the player chooses to “Hit”, the server should deal a “card” to the player, which will consist of choosing a random number between 1 and 13. The server sends this “card” to the player, and the player receives the card and adds it to their score. If the card is above a 10, then add a 10 to the player’s score instead of the original value (In a standard Blackjack game, Jacks, Queens, and Kings still count as 10). After they receive the card or if they choose to “Stand”, the next player should take their turn. If either player takes a “Hit”, and the card they’re dealt raises their score total above 21, then the game should immediately end and the other player is the winner (this is known as a “Bust”). Likewise, if both players choose to “Stand”, then both players should be shown the final scores and the game is then over.

*for example, if you choose to say “Enter (1) For a Hit or enter (2) to Stand”, then any entry other than 1 or 2 should be counted as invalid and should loop to allow the player another entry. If you choose something like “Enter (H)it or (S)tand”, then valid entries should be “H”, “S”, and ideally “h” and “s”.

You May: Get user input in any way you choose, though you must check for invalid entries and prompt a re-entry if invalid data is entered; you may choose to implement an additional feature that allows a 1 (Ace) to be worth either 1 or 11, however this is not necessary; you may choose to deal two cards to each player to begin the game, however this is not necessary; you may choose to display the first card drawn to each player to both players, however this is not necessary; you may use driver classes and additional methods other than the main method in each class, however this is not necessary to complete this program.

You May Not: Exceed the scope of a console-based application – this means no GUIs, etc; you may not allow for more than two players by any means; you may not use two different client classes – one client must function as either player; you may not use or copy code from any source, including but not limited to other students, textbooks, online sources, activities from this class or any other class, or any other source. Copying code will result in a 0 grade on this assignment and possible other consequences.

While you’re writing this program, keep the following things in mind:

Socket, ServerSocket, and all forms of I/O must be closed when your connection is terminated.

Your main method must catch any possible exceptions that may be thrown; it cannot throw an exception as this is bad practice.

Please keep your program code neat and organized. Good use of white space goes a long way.

Do not keep any unused variables or imports around – this is bad practice.

Client Output (First Run of Client Class)

Fetching server connection...

Connection Established.

Fetching player ID...

Game Found. You're Player 1.

Waiting for server...

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:0

H

Drew a 9.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:9

H

Drew a 10.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

a

Input is invalid.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Stand.

Game Over.

Player 1's Score:19

Player 2's Score:18

Player 1 won!

Server Output

Server started. Finding Clients...

Found Client 1.

Found Client 2.

Initiating Game...

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 9

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:6

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 19

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:10

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:18

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'S' from Client.

Game Over.

Player 1's Score:19

Player 2's Score:18

I need to know how to do this in Java. Can someone please help me?

Client Output (First Run of Client Class)

Fetching server connection...

Connection Established.

Fetching player ID...

Game Found. You're Player 1.

Waiting for server...

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:0

H

Drew a 9.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:9

H

Drew a 10.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

a

Input is invalid.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Hit.

It's your turn. Enter 'H' for a Hit or 'S' to Stand.

Score:19

S

Chose to Stand at 19.

Waiting for Player 2...

Player 2 took a Stand.

Game Over.

Player 1's Score:19

Player 2's Score:18

Player 1 won!

Server Output

Server started. Finding Clients...

Found Client 1.

Found Client 2.

Initiating Game...

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 9

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:6

Player 1's Turn.

Got 'H' from Client.

Player 1's Score: 19

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:10

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'H' from Client.

Player 2's Score:18

Player 1's Turn.

Got 'S' from Client.

Player 2's Turn.

Got 'S' from Client.

Game Over.

Player 1's Score:19

Player 2's Score:18

Explanation / Answer

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Server

{

    private static Socket socket;

    public static void main(String[] args)

    {

        try

        {

            int port = 25000;

            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("Server Started and listening to the port 25000");

            //Server is running always. This is done using this while(true) loop

            while(true)

            {

                //Reading the message from the client

                socket = serverSocket.accept();

                InputStream is = socket.getInputStream();

                InputStreamReader isr = new InputStreamReader(is);

                BufferedReader br = new BufferedReader(isr);

                String number = br.readLine();

                System.out.println("Message received from client is "+number);

                //Multiplying the number by 2 and forming the return message

                String returnMessage;

                try

                {

                    int numberInIntFormat = Integer.parseInt(number);

                    int returnValue = numberInIntFormat*2;

                    returnMessage = String.valueOf(returnValue) + " ";

                }

                catch(NumberFormatException e)

                {

                    //Input was not a number. Sending proper message back to client.

                    returnMessage = "Please send a proper number ";

                }

                //Sending the response back to the client.

                OutputStream os = socket.getOutputStream();

                OutputStreamWriter osw = new OutputStreamWriter(os);

                BufferedWriter bw = new BufferedWriter(osw);

                bw.write(returnMessage);

                System.out.println("Message sent to the client is "+returnMessage);

                bw.flush();

            }

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                socket.close();

            }

            catch(Exception e){}

        }

    }

}

Client.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.InetAddress;

import java.net.Socket;

public class Client

{

    private static Socket socket;

    public static void main(String args[])

    {

        try

        {

            String host = "localhost";

            int port = 25000;

            InetAddress address = InetAddress.getByName(host);

            socket = new Socket(address, port);

            //Send the message to the server

            OutputStream os = socket.getOutputStream();

            OutputStreamWriter osw = new OutputStreamWriter(os);

            BufferedWriter bw = new BufferedWriter(osw);

            String number = "2";

            String sendMessage = number + " ";

            bw.write(sendMessage);

            bw.flush();

            System.out.println("Message sent to the server : "+sendMessage);

            //Get the return message from the server

            InputStream is = socket.getInputStream();

            InputStreamReader isr = new InputStreamReader(is

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.net.ServerSocket;

import java.net.Socket;

public class Server

{

    private static Socket socket;

    public static void main(String[] args)

    {

        try

        {

            int port = 25000;

            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("Server Started and listening to the port 25000");

            //Server is running always. This is done using this while(true) loop

            while(true)

            {

                //Reading the message from the client

                socket = serverSocket.accept();

                InputStream is = socket.getInputStream();

                InputStreamReader isr = new InputStreamReader(is);

                BufferedReader br = new BufferedReader(isr);

                String number = br.readLine();

                System.out.println("Message received from client is "+number);

                //Multiplying the number by 2 and forming the return message

                String returnMessage;

                try

                {

                    int numberInIntFormat = Integer.parseInt(number);

                    int returnValue = numberInIntFormat*2;

                    returnMessage = String.valueOf(returnValue) + " ";

                }

                catch(NumberFormatException e)

                {

                    //Input was not a number. Sending proper message back to client.

                    returnMessage = "Please send a proper number ";

                }

                //Sending the response back to the client.

                OutputStream os = socket.getOutputStream();

                OutputStreamWriter osw = new OutputStreamWriter(os);

                BufferedWriter bw = new BufferedWriter(osw);

                bw.write(returnMessage);

                System.out.println("Message sent to the client is "+returnMessage);

                bw.flush();

            }

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                socket.close();

            }

            catch(Exception e){}

        }

    }

}