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

So i designed a blackjack card game in java using javafx. Below is the code for

ID: 3850642 • Letter: S

Question

So i designed a blackjack card game in java using javafx. Below is the code for it:

Hand.java class:

import Card.Rank;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ObservableList;
import javafx.scene.Node;

public class Hand
{

private ObservableList cards;
private SimpleIntegerProperty value = new SimpleIntegerProperty(0);

private int aces = 0;

public Hand(ObservableList cards)
{
this.cards = cards;
}

public void takeCard(Card card)
{
cards.add(card);

if (card.rank == Rank.ACE)
{
aces++;
}

if (value.get() + card.value > 21 && aces > 0)
{
value.set(value.get() + card.value - 10);
aces--;
}
else
{
value.set(value.get() + card.value);
}
}

public void reset()
{
cards.clear();
value.set(0);
aces = 0;
}

public SimpleIntegerProperty valueProperty()
{
return value;
}
}

Deck.java class:

import Card.Rank;
import Card.Suit;

public class Deck
{

private Card[] cards = new Card[52];

public Deck()
{
refill();
}

public final void refill()
{
int i = 0;
for (Suit suit : Suit.values())
{
for (Rank rank : Rank.values())
{
cards[i++] = new Card(suit, rank);
}
}
}

public Card drawCard()
{
Card card = null;
while (card == null)
{
int index = (int)(Math.random()*cards.length);
card = cards[index];
cards[index] = null;
}
return card;
}
}

Card.java class:

import javafx.scene.Parent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

public class Card extends Parent
{

private static final int CARD_WIDTH = 100;
private static final int CARD_HEIGHT = 140;

enum Suit
{
HEARTS, DIAMONDS, CLUBS, SPADES;

final Image image;

Suit()
{
this.image = new Image(Card.class.getResourceAsStream("images/".concat(name().toLowerCase()).concat(".png")),
32, 32, true, true);
}
}

enum Rank
{
TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10),
JACK(10), QUEEN(10), KING(10), ACE(11);

final int value;
Rank(int value)
{
this.value = value;
}

String displayName()
{
return ordinal() < 9 ? String.valueOf(value) : name().substring(0, 1);
}
}

public final Suit suit;
public final Rank rank;
public final int value;

public Card(Suit suit, Rank rank)
{
this.suit = suit;
this.rank = rank;
this.value = rank.value;

Rectangle bg = new Rectangle(CARD_WIDTH, CARD_HEIGHT);
bg.setArcWidth(20);
bg.setArcHeight(20);
bg.setFill(Color.WHITE);

Text text1 = new Text(rank.displayName());
text1.setFont(Font.font(18));
text1.setX(CARD_WIDTH - text1.getLayoutBounds().getWidth() - 10);
text1.setY(text1.getLayoutBounds().getHeight());

Text text2 = new Text(text1.getText());
text2.setFont(Font.font(18));
text2.setX(10);
text2.setY(CARD_HEIGHT - 10);

ImageView view = new ImageView(suit.image);
view.setRotate(180);
view.setX(CARD_WIDTH - 32);
view.setY(CARD_HEIGHT - 32);

getChildren().addAll(bg, new ImageView(suit.image), view, text1, text2);
}

@Override
public String toString()
{
return rank.toString() + " of " + suit.toString();
}
}

BlackjackMain.java class:

import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class BlackjackMain extends Application
{

private Deck deck = new Deck();
private Hand dealer, player;
private Text message = new Text();

private SimpleBooleanProperty playable = new SimpleBooleanProperty(false);

private HBox dealerCards = new HBox(20);
private HBox playerCards = new HBox(20);

private Parent createContent() {
dealer = new Hand(dealerCards.getChildren());
player = new Hand(playerCards.getChildren());

Pane root = new Pane();
root.setPrefSize(800, 600);

Region background = new Region();
background.setPrefSize(800, 600);
background.setStyle("-fx-background-color: rgba(0, 0, 0, 1)");

HBox rootLayout = new HBox(5);
rootLayout.setPadding(new Insets(5, 5, 5, 5));
Rectangle leftBG = new Rectangle(550, 560);
leftBG.setArcWidth(50);
leftBG.setArcHeight(50);
leftBG.setFill(Color.GREEN);
Rectangle rightBG = new Rectangle(230, 560);
rightBG.setArcWidth(50);
rightBG.setArcHeight(50);
rightBG.setFill(Color.ORANGE);

VBox leftVBox = new VBox(50);
leftVBox.setAlignment(Pos.TOP_CENTER);

Text dealerScore = new Text("Dealer: ");
Text playerScore = new Text("Player: ");

leftVBox.getChildren().addAll(dealerScore, dealerCards, message, playerCards, playerScore);

VBox rightVBox = new VBox(20);
rightVBox.setAlignment(Pos.CENTER);

final TextField bet = new TextField("BET");
bet.setDisable(true);
bet.setMaxWidth(50);
Text money = new Text("MONEY");

Button btnPlay = new Button("PLAY");
Button btnHit = new Button("HIT");
Button btnStand = new Button("STAND");

HBox buttonsHBox = new HBox(15, btnHit, btnStand);
buttonsHBox.setAlignment(Pos.CENTER);

rightVBox.getChildren().addAll(bet, btnPlay, money, buttonsHBox);

rootLayout.getChildren().addAll(new StackPane(leftBG, leftVBox), new StackPane(rightBG, rightVBox));
root.getChildren().addAll(background, rootLayout);

btnPlay.disableProperty().bind(playable);
btnHit.disableProperty().bind(playable.not());
btnStand.disableProperty().bind(playable.not());

playerScore.textProperty().bind(new SimpleStringProperty("Player: ").concat(player.valueProperty().asString()));
dealerScore.textProperty().bind(new SimpleStringProperty("Dealer: ").concat(dealer.valueProperty().asString()));

player.valueProperty().addListener((obs, old, newValue) ->
{
if (newValue.intValue() >= 21)
{
endGame();
}
});

dealer.valueProperty().addListener((obs, old, newValue) ->
{
if (newValue.intValue() >= 21)
{
endGame();
}
});

btnPlay.setOnAction(event ->
{
startNewGame();
});

btnHit.setOnAction(event ->
{
player.takeCard(deck.drawCard());
});

btnStand.setOnAction(event ->
{
while (dealer.valueProperty().get() < 17)
{
dealer.takeCard(deck.drawCard());
}

endGame();
});

return root;
}

private void startNewGame()
{
playable.set(true);
message.setText("");

deck.refill();

dealer.reset();
player.reset();

dealer.takeCard(deck.drawCard());
dealer.takeCard(deck.drawCard());
player.takeCard(deck.drawCard());
player.takeCard(deck.drawCard());
}

private void endGame()
{
playable.set(false);

int dealerValue = dealer.valueProperty().get();
int playerValue = player.valueProperty().get();
String winner = "Exceptional case: d: " + dealerValue + " p: " + playerValue;

if (dealerValue == 21 || playerValue > 21 || dealerValue == playerValue
|| (dealerValue < 21 && dealerValue > playerValue))
{
winner = "DEALER";
}
else if (playerValue == 21 || dealerValue > 21 || playerValue > dealerValue)
{
winner = "PLAYER";
}

message.setText(winner + " WON");
}

@Override
public void start(Stage primaryStage) throws Exception
{
primaryStage.setScene(new Scene(createContent()));
primaryStage.setWidth(800);
primaryStage.setHeight(600);
primaryStage.setResizable(false);
primaryStage.setTitle("BlackJack");
primaryStage.show();
}

public static void main(String[] args)
{
launch(args);
}
}

BELOW IS WHAT I NEED TO FIX, PLEASE HELP ME!!!!

***************************I need to load the list(s) from a file, like a txt file or csv file, so my code might need to be changed a bit. I was thinking about loading names and then saving how many wins or losses to that same file for every player.

***************************I need to sort the list in some meaningful way using a Collections sort method

***************************I also need to separate each class into either the cards.model package or cards.view package and leave the main class where it is (keep the model and view separate) (your view has lots of code that should be in the model; there isn't a good distinction in package layout)

PLEASE HELP ME AS SOON AS POSSIBLE AS I AM STUCK ON FIGURING OUT THESE THREE THINGS.

Here are the instructions for everything I need to do:

have three classes in addition to the MainClass; there should be some sort of association between the classes

have a list of at least one of these classes

load the list(s) from a file

sort the list in some meaningful way using a Collections sort method, not one you develop yourself

provide a JavaFX user interface which provides a means to

select particular entries in the list

interact with those entries

retrieve them and demonstrate that the any modifications still exist

do something interesting with your classes. I realize that is nebulous, but just showing me a car object, changing the color and showing the car again isn’t sufficient. The complexity of your program should resemble that of our banking program at a minimum; there should be some interaction between the three (or more) objects

follow the principles we have learned in class, e.g.

separation of concern (keep the model and view separate)

avoiding repetition of code

efficiency

maintainability

correctness!

Requirements for a grade of B

In addition to the requirements for a C, incorporate two of the following:

Add exception handling to your application. When information is read from the file system, report when invalid information is read, showing the line number and what is incorrect about the line. If this is not applicable to your program, find some other location where exceptions can occur and catch them.

Use an interface you that you design; it should be meaningful and something that could legitimately belong to more than one class.

Inheritance with at least two child classes; again it should be meaningful, not inheritance for the sake of inheritance.

Pattern matching used in some useful way

A singleton class

Requirements for a grade of A

In addition to the requirements for a B, incorporate at least one of the following:

An Observer pattern using Observer and Observable from java.util

An Observer pattern that you design instead of using Observer/Observable.

Threads. I don’t expect this one, but thought I would offer it; if you use Threads they should have some reasonable purpose, not simply something to check off a list for an ‘A’.

A reasonable use of pattern matching

Saving the state of the objects to a file, then reading that state back into your program. (Consider the comma separated files we have used already; check out PrintWriter which has the same functionality as System.out but writes to a file instead of the console.)

Explanation / Answer

Blackjack program..

************************************************************************

public class ProjectBlakcJack extends App {
   public void start(Stage primaryStage) throws Exception{
      
       DeckOfCards deck = new DeckOfCards();
      
       HBox text = new HBox();
      Label label1 = new Label("Let's play some black jack!");
      text.getChildren().addAll(label1);
      
       HBox cards = new HBox();
   Image img = new Image("**************");
   ImageView imgView = new ImageView(img);
   Image img0 = new Image("******");
   ImageView imgView0 = new ImageView(img0);
   Image img1 = new Image("******");
   ImageView imgView1 = new ImageView(img1);
   Image img2 = new Image("************");
   ImageView imgView2 = new ImageView(img2);
   Image img3 = new Image("*******");
   ImageView imgView3 = new ImageView(img3);
   cards.getChildren().addAll(imgView, imgView0, imgView1, imgView2,imgView3);

       HBox hb = new HBox(50);
      
       Button btn1 = new Button("Shuffle");
       Button btn2 = new Button("Deal");
       hb.getChildren().addAll(btn1,btn2);
      
       BorderPane pane = new BorderPane();
       pane.setTop(text);
       pane.setCenter(cards);
      pane.setBottom(hb);
      
       btn2.setOnAction((clicked) -> {System.out.println("Hit me");});
       btn1.setOnAction((clicked) -> {deck.shuffle();});
  
   Scene scene = new Scene(pane, 360, 150);
   primaryStage.setTitle("Card Game");
   primaryStage.setScene(scene);
   primaryStage.show();
   }
  
   public static void main(String[] args){
       Application.launch(args);
   }

   public class DeckOfCards {

       int[] deck;
       int next_card = 0;

      
       public static final int SIZE_OF_DECK = 52;

       public DeckOfCards(){
           this(1);
       }
      
       public DeckOfCards(int num_decks){

               this.deck = new int[num_decks*DeckOfCards.SIZE_OF_DECK];
              
               for(int i = 0; i<this.deck.length; i++){
                   int k = i%DeckOfCards.SIZE_OF_DECK;
                   this.deck[i] = k;
               }
              
       }

       public void shuffle(){

           for(int i = 0; i<this.deck.length; i++){
               int j = (int)(Math.random()*this.deck.length);
               int temp = this.deck[i];
               this.deck[i] = this.deck[j];
               this.deck[j] = temp;
           }
           this.next_card = 0;
           System.out.println("Shuffled");
          
       }
      
       public Card nextCard(){
           return new Card(this.deck[this.next_card++]);
       }
      
       public boolean hasNext(){
           return this.next_card<this.deck.length;
       }
      
      
   }
}

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