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

Intro to java create a blackjack game implementing the following requirements. D

ID: 3763945 • Letter: I

Question

Intro to java create a blackjack game implementing the following requirements. Do not copy and paste already used codes from online, comments are useful. Project Requirements: You need to work on a team (preferably 2 students per team, max 3 students) Each project needs to be designed from scratch and it needs to be written in Java. Each project needs to include the following: a loop either an array or an Array List at least 3 Java classes Use methods to avoid code repetition and to structure your code If a method grows too big, look for ways to break it up into smaller units. Choose descriptive names for your variables and methods

Explanation / Answer

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
  
public class BlackjackGUI extends JApplet {
  
public void init() {

  
setBackground( new Color(130,50,40) );
  
BlackjackCanvas board = new BlackjackCanvas();
getContentPane().add(board, BorderLayout.CENTER);
  
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground( new Color(220,200,180) );
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
  
JButton hit = new JButton( "Hit!" );
hit.addActionListener(board);
buttonPanel.add(hit);
  
JButton stand = new JButton( "Stand!" );
stand.addActionListener(board);
buttonPanel.add(stand);
  
JButton newGame = new JButton( "New Game" );
newGame.addActionListener(board);
buttonPanel.add(newGame);
  
}   

public Insets getInsets() {
  
return new Insets(3,3,3,3);
}


class BlackjackCanvas extends JPanel implements ActionListener {

  
Deck deck;   
BlackjackHand dealerHand;
BlackjackHand playerHand;   

String message;   
boolean gameInProgress;   
Font bigFont;   
Font smallFont;


BlackjackCanvas() {

setBackground( new Color(0,120,0) );
smallFont = new Font("SansSerif", Font.PLAIN, 12);
bigFont = new Font("Serif", Font.BOLD, 14);
doNewGame();
}


public void actionPerformed(ActionEvent evt) {
  
String command = evt.getActionCommand();
if (command.equals("Hit!"))
doHit();
else if (command.equals("Stand!"))
doStand();
else if (command.equals("New Game"))
doNewGame();
}


void doHit() {
  
if (gameInProgress == false) {
message = "Click "New Game" to start a new game.";
repaint();
return;
}
playerHand.addCard( deck.dealCard() );
if ( playerHand.getBlackjackValue() > 21 ) {
message = " Sorry, you lose.";
gameInProgress = false;
}
else if (playerHand.getCardCount() == 3)
{
message = "You win by taking 3 cards without going over 21.";
gameInProgress = false;
}
else {
message = "You have " + playerHand.getBlackjackValue() + ". Hit or Stand?";
}
repaint();
}


void doStand() {

if (gameInProgress == false)
{
message = "Click "New Game" to start a new game.";
repaint();
return;
}
gameInProgress = false;
while (dealerHand.getBlackjackValue() <= 16 && dealerHand.getCardCount() < 3)
dealerHand.addCard( deck.dealCard() );
if (dealerHand.getBlackjackValue() > 21)
message = "You win! Dealer has busted with " + dealerHand.getBlackjackValue() + ".";
else if (dealerHand.getCardCount() == 3)
message = "Sorry, you lose. Dealer took 3 cards without going over 21.";
else if (dealerHand.getBlackjackValue() > playerHand.getBlackjackValue())
message = "Sorry, you lose, " + dealerHand.getBlackjackValue()
+ " to " + playerHand.getBlackjackValue() + ".";
else if (dealerHand.getBlackjackValue() == playerHand.getBlackjackValue())
message = "Sorry, you lose. Dealer wins on a tie.";
else
message = "You win, " + playerHand.getBlackjackValue()
+ " to " + dealerHand.getBlackjackValue() + "!";
repaint();
}


void doNewGame() {
  
if (gameInProgress) {

message = "You still have to finish this game!";
repaint();
return;
}
deck = new Deck();
dealerHand = new BlackjackHand();
playerHand = new BlackjackHand();
deck.shuffle();
dealerHand.addCard( deck.dealCard() );   
dealerHand.addCard( deck.dealCard() );
playerHand.addCard( deck.dealCard() );
playerHand.addCard( deck.dealCard() );
if (dealerHand.getBlackjackValue() == 21) {
message = " you lose game. Dealer has Blackjack.";
gameInProgress = false;
}
else if (playerHand.getBlackjackValue() == 21) {
message = "You win! You have Blackjack.";
gameInProgress = false;
}
else {
message = "You have " + playerHand.getBlackjackValue() + ". Hit or stand?";
gameInProgress = true;
}
repaint();
}   


public void paintComponent(Graphics g) {
  
super.paintComponent(g);


g.setFont(bigFont);
g.setColor(Color.green);
g.drawString(message, 10, getSize().height - 10);

g.drawString("Dealer's Cards:", 10, 23);
g.drawString("Your Cards:", 10, 153);

  
g.setFont(smallFont);
if (gameInProgress)
drawCard(g, null, 10, 30);
else
drawCard(g, dealerHand.getCard(0), 10, 30);
for (int p = 1; p < dealerHand.getCardCount(); p++)
drawCard(g, dealerHand.getCard(p), 10 + p * 90, 30);

  

for (int p = 0; p < playerHand.getCardCount(); p++)
drawCard(g, playerHand.getCard(p), 10 + p * 90, 160);

}   


void drawCard(Graphics g, Card card, int x, int y) {

if (card == null) {

g.setColor(Color.blue);
g.fillRect(x,y,80,100);
g.setColor(Color.white);
g.drawRect(x+3,y+3,73,93);
g.drawRect(x+4,y+4,71,91);
}
else {
g.setColor(Color.white);
g.fillRect(x,y,80,100);
g.setColor(Color.gray);
g.drawRect(x,y,79,99);
g.drawRect(x+1,y+1,77,97);
if (card.getSuit() == Card.DIAMONDS || card.getSuit() == Card.HEARTS)
g.setColor(Color.red);
else
g.setColor(Color.black);
g.drawString(card.getValueAsString(), x + 10, y + 30);
g.drawString("of", x+ 10, y + 50);
g.drawString(card.getSuitAsString(), x + 10, y + 70);
}
}   


}


}
  

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