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

In JAVA please. No JAVAFX In this project you will create a solution designed to

ID: 3919584 • Letter: I

Question

In JAVA please. No JAVAFX

In this project you will create a solution designed to play Blackjack. We will use simple

rules of the game. Assume there exists a player and a dealer. The winner of the game is

the player who gets closest to 21 without exceeding 21 or “going bust”. If both players

have the same points, then neither player wins the hand. An ace has a value of 1 or 11.

Face cards have a value of 10. The dealer must take a card if the dealer’s hand is worth

16 or less. The dealer may not take a card if the hand is worth 17 or more. As the player,

you may take a card at any time.

Allow the user to play as many hands as he/she wants.

Keep track of how many hands the dealer wins and how many hands the player wins.

How to get started;

Create a class called Card.

The card has three fields; a value , a suit and a face.

The card class has a constructor that takes three values for the three fields.

Create a no-args constructor.

The Card class has three get methods to return the values of each of the fields.

The Card class has a toString( ) method.

The Card class has a compareTo( ) method that uses the value of the card to determine

the ordering.

Create an equals( ) method that returns true if the parameter Card has the same suit and

the same face as the invoking Card.

Create a class called Deck.

A deck has two fields- an array of Cards and an int which

holds the subscript of the Card at the top of the deck.

The Deck class will have only one constructor, which will create the array. Try to think

of a way to code this without writing 52 statements. (not required)

The Deck class also has a method getTopCard( ) which returns a

copy

of the Card that is

at the top of the array.

The Deck class has a method called shuffle( ) which shuffles the array of Cards. To

shuffle, generate random numbers and use these to exchange array elements. Exchange

1000 times in this method

Create the driver class - call it Game21.

This is where your game logic will go. As a hint, you will need to create two Deck

instances and then call the shuffle method( ) for each. When you are testing as you write,

before you shuffle( ) you may want to print out the entire Deck to make sure you have

created the array of Cards correctly.

Each player is dealt two cards to start. Then the rules stated above are followed. Only the

user has the choice whether to deal another card or quit. The dealer (computer) must be

dealt another card from its deck if its total is 16 or under and must quit if its hand is 17 or

over.

Once a hand is won, either by the computer or the player, allow the player to decide if

he/she wants to play another hand. Keep a running total of the number of hands won by

the computer and the number of hands won by the player. Print these results to a file once

the user decides to quit. Each time the program is run, the results of the session should be

appended to the same output file.

Explanation / Answer

import static java.lang.System.out;

import static java.lang.System.in;

import java.util.Random;

import java.util.Scanner;

class PlayTwentyOne {

    public static void main(String args[]) {

        Random rnd = new Random();

        Scanner myScanner = new Scanner(in);

        Scanner myScanner2 = new Scanner(in);

        int ccrd, cttl = 0;

        int pcd=0, pttl = 0;

        int points;                             // value of a card

        int aces;                           // how many aces do you have

        char replyCard, RPA = 'y';

        int bet=0 , mpl = 200;

        wkw whoWins;                        // wkw is a class: public enum wkw {computer, you, nowbody}

         

        String nameCard = " ";

         

        out.println("you have " + mpl + " euro");

         

        while (RPA == 'y' && mpl >= 0){       // Draw 2 cards for player.

            aces = 0;

            pttl = 0;

            cttl = 0;

            bet = 0;

            replyCard = 'y';

            whoWins = wkw.nowbody;

             

                while (bet == 0){

                     

                    out.println("How much do you want to bet?");

                    bet = myScanner.nextInt();

                    }

                         

                for (int count = 0; count < 2; count++){

                     

                    pcd = rnd.nextInt(13) + 1;

                 

                    switch (pcd) {

                    case 1:

                        nameCard = "ace";

                        aces = +1;

                        points = 11;

                        break;

                    case 2:

                        nameCard = "two";

                        points = 2;

                        break;

                    case 3:

                        nameCard = "tree";

                        points = 3;

                        break;

                    case 4:

                        nameCard = "for";

                        points = 4;

                        break;

                    case 5:

                        nameCard = "five";

                        points = 5;

                        break;

                    case 6:

                        nameCard = "six";

                        points = 6;

                        break;

                    case 7:

                        nameCard = "seven";

                        points = 7;

                        break;

                    case 8:

                        nameCard = "eight";

                        points = 8;

                        break;

                    case 9:

                        nameCard = "nine";

                        points = 9;

                        break;

                    case 10:

                        nameCard = "ten";

                        points = 10;

                        break;

                    case 11:

                        points = 10;

                        nameCard = "Jack";

                        break;

                    case 12:

                        points = 10;

                        nameCard = "queen";

                        break;

                    case 13:

                        points = 10;   

                        nameCard = "king";

                        break;

                    default:

                        points = 1000;

                        break;

                    }

                 

                    pttl += points;

                    while (pttl> 21 && aces >=1){

                        pttl = pttl -10;

                        aces = aces -1;

                    }

                   System.out.println("Kaart getrokken: " + nameCard + ". Total points: " + pttl);

               }

                if (pttl == 21){

                    System.out.println(":) You have 21");

                    replyCard = 'n';

                    whoWins = wkw.you;

                }

                

                while (replyCard == 'y' & whoWins == wkw.nowbody){      //Draw cards for player

                 

                    System.out.println("Do you want a card? y/n");

                    replyCard = myScanner2.findWithinHorizon(".",0).charAt(0);

                     

                    if (replyCard == 'y'){

                     

                        pcd = rnd.nextInt(13) + 1;

                         

                        switch (pcd) {

                        case 1:

                            nameCard = "ace";

                            aces = +1;

                            points = 11;

                            break;

                        case 2:

                            nameCard = "two";

                            points = 2;

                            break;

                        case 3:

                            nameCard = "tree";

                            points = 3;

                            break;

                        case 4:

                            nameCard = "for";

                            points = 4;

                            break;

                        case 5:

                            nameCard = "five";

                            points = 5;

                            break;

                        case 6:

                            nameCard = "six";

                            points = 6;

                            break;

                        case 7:

                            nameCard = "seven";

                            points = 7;

                            break;

                        case 8:

                            nameCard = "eight";

                            points = 8;

                            break;

                        case 9:

                            nameCard = "nine";

                            points = 9;

                            break;

                        case 10:

                            nameCard = "ten";

                            points = 10;

                            break;

                        case 11:

                            points = 10;

                            nameCard = "Jack";

                            break;

                        case 12:

                            points = 10;

                            nameCard = "queen";

                            break;

                        case 13:

                            points = 10;   

                            nameCard = "king";

                            break;

                        default:

                            points = 1000;

                            break;

                        }

                         

                        pttl += points;

                        while (pttl> 21 && aces >=1){

                            pttl = pttl -10;

                            aces = aces -1;

                        }

                        System.out.println("Draw card " + nameCard + ". Total points: " + pttl);

                        

                        if (pttl == 21){

                            System.out.println(":) Je hebt 21");

                            replyCard = 'n';

                            whoWins = wkw.you;

                             

                        } else if (pttl > 21){

                            replyCard = 'n';

                            whoWins = wkw.computer;

                             

                        } else {

                            replyCard = 'y';

                            whoWins = wkw.nowbody;

                        }  

                    }

                }

                System.out.println("total Player 1: "+ pttl + " ppoints");

                

                //Computers turn:

                aces = 0;

                 

                if (whoWins == wkw.nowbody){

                    out.println();

                    out.println("Computer:");

                }

                 

                while ((whoWins == wkw.nowbody) && (cttl <= pttl)) { //Draw cards for computer till he wins or >21

                    ccrd = rnd.nextInt(13) + 1;

                     

                    switch (ccrd) {

                    case 1:

                        nameCard = "ace";

                        aces = +1;

                        points = 11;

                        break;

                    case 2:

                        nameCard = "two";

                        points = 2;

                        break;

                    case 3:

                        nameCard = "tree";

                        points = 3;

                        break;

                    case 4:

                        nameCard = "for";

                        points = 4;

                        break;

                    case 5:

                        nameCard = "five";

                        points = 5;

                        break;

                    case 6:

                        nameCard = "six";

                        points = 6;

                        break;

                    case 7:

                        nameCard = "seven";

                        points = 7;

                        break;

                    case 8:

                        nameCard = "eight";

                        points = 8;

                        break;

                    case 9:

                        nameCard = "nine";

                        points = 9;

                        break;

                    case 10:

                        nameCard = "ten";

                        points = 10;

                        break;

                    case 11:

                        points = 10;

                        nameCard = "Jack";

                        break;

                    case 12:

                        points = 10;

                        nameCard = "queen";

                        break;

                    case 13:

                        points = 10;   

                        nameCard = "king";

                        break;

                    default:

                        points = 1000;

                        break;

                    }

                    while (cttl> 21 && aces >=1){

                        cttl = cttl -10;

                        aces = aces -1;

                    }

                    cttl += points;

                    System.out.println("Draw Card: " + nameCard + ". Total points: " + cttl);

                }

                 

                if (whoWins == wkw.nowbody){               

                System.out.print("total Computer:" );

                System.out.print(cttl);

                out.println();

                }

                 

                if (cttl >= pttl){

                    whoWins = wkw.computer;

                }

                if (cttl >= 22){

                    whoWins = wkw.you;

                }

                if (whoWins == wkw.you){

                    out.println();

                    System.out.println("Yee, you win!");

                    mpl = mpl + bet;

                } else if (whoWins == wkw.computer){

                    out.println();

                    System.out.println("Computer wins");

                    mpl = mpl - bet;

                } else {

                    out.println();

                    System.out.println("there is something wrong with my code.");

                }

             

            out.println();

            out.println("You have " + mpl + " euro");

            out.println ("Do you want to play again? y/n");

            RPA = myScanner2.findWithinHorizon(".",0).charAt(0);

                         

        }

     }

}

// this code prints the output on the screen, you may write it to a file using FileOuptutStream.

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