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

So for this project, we’re going to make a game. This game will make use of a se

ID: 3697860 • Letter: S

Question

So for this project, we’re going to make a game. This game will make use of a server to join two clients together for a modified game of Blackjack. I’ll explain how this modified game works below.

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.

Sample Output is included on the next page.

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

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

Solution: Please follow these coding as shown in below..

import java.util.Scanner;

import java.util.Vector;

public class BlackjackConsole

{

   private static Scanner scanner = new Scanner(System.in);

    private int[] deck;   // An array of 52 Cards, representing the deck.

    private int currentPosition; // Current position in the deck

    private Vector hand;   // The cards in the hand.

     public static void main(String[] args)

    {

        new BlackjackConsole().run();

    }

      public void run()

    {

     /*

        This program lets the user play Blackjack. The computer

        acts as the dealer. The user has a stake of $100, and

       The user can leave at any time, or will be kicked out

        when he loses all the money.

        House rules: The dealer hits on a total of 16 or less

        and stands on a total of 17 or more. Dealer wins ties.

        A new deck of cards is used for each game.

         */

         int money;          // Amount of money the user has.

        int bet;            // Amount user bets on a game

        System.out.println("Welcome to the game of blackjack.");

        System.out.println();

        money = 100; // User starts with $100.

        while (true)

        {

            System.out.println("You have " + money + " dollars.");

            do

            {

                System.out.println("How many dollars do you want to bet? (Enter 0 to end.)");

                System.out.print("? ");

                bet = scanner.nextInt();

                if (bet < 0 || bet > money)

                {

               System.out.println("Your answer must be between 0 and " + money + '.');

                }

            } while (bet < 0 || bet > money);

            if (bet == 0)

            {

                break;

            }

            userWins = playBlackjack();

            if (userWins)

            {

                money = money + bet;

            } else

            {

                money = money - bet;

            }

            System.out.println();

            if (money == 0)

            {

                System.out.println("Looks like you've are out of money!");

                break;

            }

        }

        System.out.println();

        System.out.println("You leave with $" + money + '.');

       } // end main()

      private boolean playBlackjack()

    {

        // Let the user play one game of Blackjack.

        // Return true if the user wins, false if the user loses.

        Vector dealerHand;   // The dealer's hand.

        Vector userHand;     // The user's hand.

        // Create an unshuffled deck of cards.

        deck = new int[52];

        int cardCt = 0; // How many cards have been created so far.

        for (int suit = 0; suit <= 3; suit++)

        {

            for (int value = 1; value <= 13; value++)

            {

                deck[cardCt] = value;

                cardCt++;

            }

        }

        currentPosition = 0;

        dealerHand = new Vector();

        userHand = new Vector();

        /* Shuffle the deck, then deal two cards to each player. */

        shuffle();

        dealerHand.addElement(dealCard());

        dealerHand.addElement(dealCard());

        userHand.addElement(dealCard());

        userHand.addElement(dealCard());

         System.out.println();

        System.out.println();

        /* Check if one of the players has Blackjack (two cards totaling to 21).

        The player with Blackjack wins the game. Dealer wins ties.

         */

        if (value(dealerHand) == 21)

        {

            System.out.println("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");

            System.out.println("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");

            System.out.println();

            System.out.println("Dealer has Blackjack. Dealer wins.");

            return false;

        }

        if (value(userHand) == 21)

        {

            System.out.println("Dealer has the " + showCard(getCard(dealerHand, 0)) + " and the " + showCard(getCard(dealerHand, 1)) + ".");

            System.out.println("User has the " + showCard(getCard(userHand, 0)) + " and the " + showCard(getCard(userHand, 1)) + ".");

            System.out.println();

            System.out.println("You have Blackjack. You win.");

            return true;

        }

        /* If neither player has Blackjack, play the game. The user gets a chance

        to draw cards (i.e., to "Hit"). The while loop ends when the user

        chooses to "Stand" or when the user goes over 21.

         */

       while (true)

        {

        /* Display user's cards, and let user decide to Hit or Stand. */

            System.out.println();

            System.out.println();

            System.out.println("Your cards are:");

            for (int i = 0; i < userHand.size(); i++)

            {

                System.out.println("    " + showCard(getCard(userHand, i)));

            }

            System.out.println("Your total is " + value(userHand));

          System.out.println();

            System.out.println("Dealer is showing the " +                      showCard(getCard(dealerHand, 0)));

            System.out.println();

            System.out.print("Hit (H) or Stand (S)? ");

            char userAction; // User's response, 'H' or 'S'.

            do

            {

                userAction = Character.toUpperCase(scanner.next().charAt(0));

                if (userAction != 'H' && userAction != 'S')

                {

                    System.out.print("Please respond H or S: ");

                }

            } while (userAction != 'H' && userAction != 'S');

            /* If the user Hits, the user gets a card. If the user Stands, the

            dealer gets a chance to draw and the game ends.

             */

              if (userAction == 'S')

            {

                // Loop ends; user is done taking cards.

                break;

            } else

            { // userAction is 'H'.

                // Give the user a card. If the user goes over 21, the user loses.

                int newCard = dealCard();

                userHand.addElement(newCard);

                System.out.println();

                System.out.println("User hits.");

                System.out.println("Your card is the " + showCard(newCard));

                System.out.println("Your total is now " + value(userHand));

                if (value(userHand) > 21)

                {

                    System.out.println();

                    System.out.println("You busted by going over 21. You lose.");

                    System.out.println("Dealer's other card was the " + showCard(getCard(dealerHand, 1)));

                    return false;

                }

            }

        } // end while loop

        /* If we get to this point, the user has Stood with 21 or less. Now, it's

        the dealer's chance to draw. Dealer draws cards until the dealer's total is > 16.

         */

        System.out.println();

        System.out.println("User stands.");

        System.out.println("Dealer's cards are");

        System.out.println("    " + showCard(getCard(dealerHand, 0)));

        System.out.println("    " + showCard(getCard(dealerHand, 1)));

        while (value(dealerHand) <= 16)

        {

            int newCard = dealCard();

            System.out.println("Dealer hits and gets the " + showCard(newCard));

            dealerHand.addElement(newCard);

        }

        System.out.println("Dealer's total is " + value(dealerHand));

        /* Now, the winner can be declared. */

        System.out.println();

        if (value(dealerHand) > 21)

        {

            System.out.println("Dealer busted by going over 21. You win.");

            return true;

        } else

        {

            if (value(dealerHand) == value(userHand))

            {

                System.out.println("Dealer wins on a tie. You lose.");

                return false;

            } else

            {

                if (value(dealerHand) > value(userHand))

                {

             System.out.println("Dealer wins, " + value(dealerHand) + " points to " +     value(userHand) + ".");

                    return false;

                } else

                {

                    System.out.println("You win, " + value(userHand) + " points to " + value(dealerHand) + ".");

                    return true;

                }

            }

        }

    } // end playBlackjack()

    public int dealCard()

    {

        // Deals one card from the deck and returns it.

        if (currentPosition == 52)

        {

            shuffle();

        }

        currentPosition++;

        return deck[currentPosition - 1];

    }

    public void shuffle()

    {

        // Put all the used cards back into the deck, and shuffle it into

        // a random order.

        for (int i = 51; i > 0; i--)

        {

            int rand = (int) (Math.random() * (i + 1));

            int temp = deck[i];

            deck[i] = deck[rand];

            deck[rand] = temp;

        }

        currentPosition = 0;

    }

    public int getCard(Vector hand, int position)

    {

        // Get the card from the hand in given position, where positions

        // are numbered starting from 0. If the specified position is

        // not the position number of a card in the hand, then null

        // is returned.

        if (position >= 0 && position < hand.size())

        {

            return ((Integer)hand.elementAt(position)).intValue();

        } else

        {

            return 0;

        }

    }

     public int value(Vector hand)

   {

        // Returns the value of this hand for the

        // game of Blackjack.

        int val;      // The value computed for the hand.

        boolean ace; // This will be set to true if the

        //   hand contains an ace.

        int cards;    // Number of cards in the hand.

        val = 0;

        ace = false;

        cards = hand.size();

         for (int i = 0; i < cards; i++)

        {

            // Add the value of the i-th card in the hand.

            int card;    // The i-th card;

            int cardVal; // The blackjack value of the i-th card.

            card = getCard(hand, i);

            cardVal = getCardValue(card); // The random value, 1 to 13.

            if (cardVal > 10)

            {

                cardVal = 10;   // For a Jack, Queen, or King.

            }

            if (cardVal == 1)

            {

                ace = true;     // There is at least one ace.

            }

            val = val + cardVal;

        }

         // Now, val is the value of the hand, counting any ace as 1.

        // If there is an ace, and if changing its value from 1 to

        // 11 would leave the score less than or equal to 21,

        if (ace == true && val + 10 <= 21)

        {

            val = val + 10;

        }

        return val;

    }

    public int getCardValue(int card)

    {

        int result = card;

        switch (card)

        {

            case 11:

            case 12:

            case 13:

                result = 10;

        }

        return result;

    }

    public String showCard(int card)

    {

        switch (card)

        {

            case 1:

                return "Ace";

            case 2:

                return "2";

            case 3:

                return "3";

            case 4:

                return "4";

            case 5:

                return "5";

            case 6:

                return "6";

            case 7:

                return "7";

            case 8:

                return "8";

            case 9:

                return "9";

            case 10:

                return "10";

            case 11:

                return "Jack";

            case 12:

                return "Queen";

            case 13:

                return "King";

            default:

                return "??";

        }

    }

} // end class

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote