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

This project involves writing a java program to simulate a blackjack card game.

ID: 3860901 • Letter: T

Question

This project involves writing a java program to simulate a blackjack card game. You will use a simple
console-based user interface to implement this game.


A simple blackjack card game consists of a player and a dealer. A player is provided with a sum of
money with which to play. A player can place a bet between $0 and the amount of money the player
has. A player is dealt cards, called a hand. Each card in the hand has a point value. The objective of the
game is to get as close to 21 points as possible without exceeding 21 points. A player that goes over is
out of the game. The dealer deals cards to itself and a player. The dealer must play by slightly different
rules than a player, and the dealer does not place bets. A game proceeds as follows: A player is dealt
two cards face up. If the point total is exactly 21 the player wins immediately. If the total is not 21, the
dealer is dealt two cards, one face up and one face down. A player then determines whether to ask the
dealer for another card (called a “hit”) or to “stay” with his/her current hand. A player may ask for
several “hits.” When a player decides to “stay” the dealer begins to play. If the dealer has 21 it
immediately wins the game. Otherwise, the dealer must take “hits” until the total points in its hand is
17 or over, at which point the dealer must “stay.” If the dealer goes over 21 while taking “hits” the
game is over and the player wins. If the dealer’s points total exactly 21, the dealer wins immediately.
When the dealer and player have finished playing their hands, the one with the highest point total is the
winner. Play is repeated until the player decides to quit or runs out of money to bet.

You must use an object-oriented solution for implementing this game.

Explanation / Answer

public class Blackjack { public static void main(String[] args) { int money; // Amount of money the player has. int bet; // Amount player bets on a game. boolean playerwins; // Did the player win the game? TextIO.putln("----Welcome to the game of Blackjack----"); TextIO.putln(); money = 100; // we assume player starts with $100. while (true) { TextIO.putln("player have : $" + money); do { TextIO.putln("How many dollars does player want to bet? (Enter 0 to end)"); TextIO.put("? "); bet = TextIO.getlnInt(); if (bet < 0 || bet > money) TextIO.putln("player has to be bet between $0 and $" + money); } while (bet < 0 || bet > money); if (bet == 0) break; playerwins = playBlackjack(); if (playerwins) money = money + bet; else money = money - bet; TextIO.putln(); if (money == 0) { TextIO.putln("Looks like player are out of money!!! :-( "); break; } } TextIO.putln(); TextIO.putln("player leaves with $" + money); } // end main() static boolean playBlackjack() { // Let the player play one game of Blackjack. // Return true if the player wins, false if the player loses. Deck deck; // A deck of cards, A new deck for each game. BlackjackHand dealerHand; // The dealer's hand. BlackjackHand playerHand; // The player's hand. deck = new Deck(); dealerHand = new BlackjackHand(); playerHand = new BlackjackHand(); /* Shuffle the deck, then deal two cards to each player. */ deck.shuffle(); dealerHand.addCard( deck.dealCard() ); dealerHand.addCard( deck.dealCard() ); playerHand.addCard( deck.dealCard() ); playerHand.addCard( deck.dealCard() ); TextIO.putln(); TextIO.putln(); /* Check if one of the players has Blackjack (two cards totaling to 21). The player with Blackjack wins the game. */ if (dealerHand.getBlackjackValue() == 21) { TextIO.putln("Dealer has the " + dealerHand.getCard(0) + " and the " + dealerHand.getCard(1) + "."); TextIO.putln("player has the " + playerHand.getCard(0) + " and the " + playerHand.getCard(1) + "."); TextIO.putln(); TextIO.putln("Dealer has Blackjack. Dealer wins."); return false; } if (userHand.getBlackjackValue() == 21) { TextIO.putln("Dealer has the " + dealerHand.getCard(0) + " and the " + dealerHand.getCard(1) + "."); TextIO.putln("player has the " + playerHand.getCard(0) + " and the " + playerHand.getCard(1) + "."); TextIO.putln(); TextIO.putln("player has Blackjack. player wins."); return true; } /* If neither player has Blackjack, play the game. First the player gets a chance to draw cards (i.e., to "hit"). The while loop ends when the player chooses to "stay". If the player goes over 21, the player loses immediately. */ while (true) { /* Display player's cards, and let player decide to hit or stay. */ TextIO.putln(); TextIO.putln(); TextIO.putln("player's cards are:"); for ( int i = 0; i
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