Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need to finish this by tomorrow!!!!!!!! So i designed a blackjack card game in java using javafx. Below is the code for it: Hand.java class:

Need to finish this by tomorrow!!!!!!!!

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 isnt 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 dont 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.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Eric Redmond ,Jim Wilson

1st Edition

1934356921, 978-1934356920

More Books

Students also viewed these Databases questions

Question

Define audit trail. LO1.

Answered: 1 week ago