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

Draft source code/images: http://www.filedropper.com/draft Implement a simplifie

ID: 3547648 • Letter: D

Question



Draft source code/images: http://www.filedropper.com/draft

Implement a simplified Baccarat game with betting between 1 user (the "player") and the computer (the "banker") for 10 rounds. Baccarat is a comparing card game played between the two hands, the "player" and the "banker." Each baccarat coup has three possible outcomes: "player" (player has the higher score), "banker," and "tie."

The cards have a point value: cards 29 are worth face value (in points); 10s, Js, Qs and Ks have no point value (i.e. are worth zero); Aces are worth 1 point. Hands are valued according to the rightmost digit of the sum of their constituent cards: for example, a hand consisting of 2 and 3 is worth 5, but a hand consisting of 6 and 7 is worth 3 (i.e. the 3 being rightmost digit in the combined points total: 13).

The player starts with 50 $1 chips. The user must place a bet of maximum 5 chips for each round. Each baccarat coup has three possible outcomes: "player" wins (even win: 1:1), "banker," wins and "tie." Betting example: Lets say that you have $50 and you bet $5. If you win, you will get your $5 bet back plus another $5. Your purse will be $55. If you loose, you will loose your $5 bet. Your purse will be $45.

For each round (coup), two cards are dealt face up to each hand ("player" and "banker"). If either Player or Banker or both achieve a total of 8 or 9 at this stage, the coup is finished and the result is announced: Player win, a Banker win, or tie. If neither the Player nor Banker is dealt a total of 8 or 9 in the first two cards (known as a "natural"), the tableau is consulted, first for Player's rule, then Banker's.

If Player has an initial total of 05, he draws a third card. If Player has an initial total of 6 or 7, he stands.

If Player stood pat (i.e., has only two cards), the banker regards only his own hand and acts according to the same rule as Player. That means Banker draws a third card with hands 05 and stands with 6 or 7. If Player drew a third card, the Banker acts according to the following more complex rules:
- If Player drew a 2 or 3, Banker draws with 04, and stands with 57.
- If Player drew a 4 or 5, Banker draws with 05, and stands with 67.
- If Player drew a 6 or 7, Banker draws with 06, and stands with 7.
- If Player drew an 8, Banker draws with 02, and stands with 37.
- If Player drew an ace, 9, 10, or face-card, the Banker draws with 03, and stands with 47.

The program will deal the cards according to the tableau and will announce the winning hand: either Player or Banker.

Explanation / Answer

import java.util.*;

/**
* The class <code>Baccarat</code> is to implement the Baccarat card game.
*/

public class Baccarat {
    // -----------------------------------------------------------------
    // class private instance variables
    // -----------------------------------------------------------------
    private CardDeck deck;           // deck for the Baccarat game
    private int reShufflePoint;      // re-shuffle point for the deck

    /**
     * boolean to indicate if details printing of game is requested
     */
    public boolean printDetails;

    /**
     * default Baccarat constructor to use one pack of cards.
     */
    public Baccarat () { this(1); }

    /**
     * Baccarat constructor to use p packs of cards.
     */
    public Baccarat ( int p ) {
        deck = new CardDeck(p);
        reShufflePoint = deck.deckSize()*3/4;
        deck.shuffle();
        printDetails = true;
    }

    /**
     * to play one game of Baccarat. * It returns an integer value:
     *       a positive value - the banker wins,
     *       a negative value - the player wins,
     *       zero - a draw.
     */
    public int game() {
        // ------------------------------------------------------
        // if necessary, reshuffle the deck.
        // ------------------------------------------------------
        if ( deck.nCardsLeft() <= reShufflePoint ) deck.shuffle();

        // ------------------------------------------------------
        // create two Baccarat hands: deal two cards to the
        // banker and the player respectively.
        // ------------------------------------------------------
        BaccaratHand bankerHand = new BaccaratHand();
        BaccaratHand playerHand = new BaccaratHand();
        bankerHand.takeACard(deck.draw());
        playerHand.takeACard(deck.draw());
        bankerHand.takeACard(deck.draw());
        playerHand.takeACard(deck.draw());

        int bankerValue = bankerHand.handValue();
        int playerValue = playerHand.handValue();

        // ------------------------------------------------------
        // apply rule to see if the player draws a third card
        // ------------------------------------------------------
        int playerThirdCardValue = -1;
        if ( BaccaratRules.doesPlayerDraw(bankerValue,playerValue) ) {
            playerHand.takeACard(deck.draw());
            playerValue = playerHand.handValue();
            playerThirdCardValue=BaccaratHand.cardValue(playerHand.hand[2]);
        }

        // ------------------------------------------------------
        // apply rule to see if the banker draws a third card
        // ------------------------------------------------------
        if ( BaccaratRules.doesBankerDraw(bankerValue,
                           playerValue,playerThirdCardValue) ) {
            bankerHand.takeACard(deck.draw());
            bankerValue = bankerHand.handValue();
        }

        // ------------------------------------------------------
        // print details if necessary and return the value
        // difference between the banker's hand and the player's.
        // ------------------------------------------------------
        if ( printDetails ) printGame(bankerHand, playerHand);
        return bankerValue-playerValue;
    }

    /**
     * to print the details of the game
     */
    public static void printGame
               ( BaccaratHand bankerHand, BaccaratHand playerHand ) {
        // -------------------------------------------------------
        // print out the first two cards of banker and player
        // -------------------------------------------------------
        System.out.println("                Player Hand          Banker Hand ");
        System.out.println("First card: "+leftJustify(playerHand.hand[0]+"")+
                                          bankerHand.hand[0]);
        System.out.println("Second card: "+leftJustify(playerHand.hand[1]+"")+
                                          bankerHand.hand[1]);
        // -------------------------------------------------------
        // print out the third card of banker and player
        // -------------------------------------------------------
        System.out.print ("Third card: ");
        if ( playerHand.nCards == 3 )
            System.out.print(leftJustify(playerHand.hand[2]+""));
        else
            System.out.print("No card drawn       ");
        if ( bankerHand.nCards == 3 )
            System.out.println(leftJustify(bankerHand.hand[2]+""));
        else
            System.out.println("No card drawn");
        // -------------------------------------------------------
        // print out the hand values of banker and player
        // and the result of this game.
        // -------------------------------------------------------
        int playerValue = playerHand.handValue();
        int bankerValue = bankerHand.handValue();
        System.out.println("Hand value: "+playerValue+"                   "+
                                           bankerValue);
        if ( playerValue > bankerValue )
            System.out.println("---Player wins---");
        else if ( playerValue < bankerValue )
            System.out.println("---Banker wins---");
        else
            System.out.println("---tie---");
        System.out.println();
    }

    static private String leftJustify(String s) {
        return (s+"                    ").substring(0,20);
    }

    // ----------------------------------------------------------
    // main method to test the Baccarat class.
    // ----------------------------------------------------------
    public static void main (String[] str) {
        Baccarat baccarat = new Baccarat(1);
        int n = (str.length <= 0) ? 5 : Integer.parseInt(str[0]);
        for ( int i=0; i<n; i++ ) baccarat.game();
    }
}

// ==============================================================
// BaccaratHand class to implement a hand for the Baccarat game.
// ==============================================================
class BaccaratHand {
    Card[] hand;         // cards for the hand
    int nCards;          // number of cards for the hand

    // ---------------------------------------------------------
    // BaccaratHand constructor
    // ---------------------------------------------------------
    public BaccaratHand() {
        hand = new Card[3];
        nCards = 0;
    }

    // ---------------------------------------------------------
    // to return the value of this Baccarat hand
    // ---------------------------------------------------------
    public int handValue() {
        int v = 0;
        for ( int i=0; i<nCards; i++ )
            v += cardValue(hand[i]);
        return v%10;
    }

    // ---------------------------------------------------------
    // to return the value of a card for the Baccarat game.
    // ---------------------------------------------------------
    public static int cardValue ( Card card ) {
        int v = card.getValue();
        if ( v > 9 ) v = 0;
        return v;
    }

    // ---------------------------------------------------------
    // to take a card for this Baccarat hand from the parameter
    // ---------------------------------------------------------
    public void takeACard ( Card card ) {
        if ( nCards < 3 ) {
           hand[nCards] = card;
           nCards++;
        }
    }
}

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