2 public interface BlackjackEngine ( 3 public static final int DRAN 4 public sta
ID: 3888779 • Letter: 2
Question
2 public interface BlackjackEngine ( 3 public static final int DRAN 4 public static final int LESS THAN 21 2; 5 public static final int BUST 6 public static final int BLACKJACK 17 public static final int HAS 21 18public static final int DEALER WON 19 public static final int PLAYER WON 2 public static final int GAME IN PROGRESs 8; 21 22 /** 23 24 *@return number of decks 25 26 public int getNumberOfDecks ) 27 Returns the number of decks being used. 29 1 30 31 : Creates and shuffles the card deck(s) using a random number generaor. 32 public void createAndShuffleGameDeck) 34 35 * Returns the current deck of cards. 36 37 38 39 public Card[] getGameDeck); ereturn Card array representing deck of cards. 40 41 42 ** 43 Creates a new deck of cards, and assigns cards to the dealer and player. * A total of four cards are dealt in the following order: " Player (face up), Dealer (face down), Player (face up), Dealer (face up). " Once the cards have been dealt, the game's status will be GAME_IN PROGRESS. 45 46 47 48 Delete the bet amount from the account. 49 public void deal): 50 51 * 52 53 * Returns dealer's cards .@return Card array representing the dealer's cards. 55 public Card[] getDealerCards): 56 58 Returns an array representing the possible value (s) associated with the " dealer's cards if the cards represent a value less than or equal to 21. 59 ereturn Integer array representing the possible value(s) or null if cards " represent a value higher than 21. The array wi11 have a size of 1 if onl " one value is associated with the set of cards, and a size of two if two " values are possible. Por the case of an array of size two, the smaller value " must appear in the first array entry 64 65 67 public int[] getDealerCardsTotal): " Returns an integer value that can assume the values LESS THAN 21 the dealen's cards have a value less than 21Explanation / Answer
BlackJackApp.java
import com.almasb.blackjack.Deck;
import com.almasb.blackjack.Hand;
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;
/**
*
* @author Akshay Bisht
*/
public class BlackjackApp extends Application {
private Deck dck = new Deck();
private Hand dealr, playa;
private Text mssg = new Text();
private SimpleBooleanProperty played = new SimpleBooleanProperty(false);
private HBox dCrd = new HBox(20);
private HBox pCrd = new HBox(20);
private Parent createContent() {
dealr = new Hand(dCrd.getChildren());
playa = new Hand(pCrd.getChildren());
Pane rt = new Pane();
rt.setPrefSize(800, 600);
Region bg = new Region();
bg.setPrefSize(800, 600);
bg.setStyle("-fx-bg-color: rgba(0, 0, 0, 1)");
HBox rtLay = new HBox(5);
rtLay.setPadding(new Insets(5, 5, 5, 5));
Rectangle ltBG = new Rectangle(550, 560);
ltBG.setArcWidth(50);
ltBG.setArcHeight(50);
ltBG.setFill(Color.GREEN);
Rectangle rtBG = new Rectangle(230, 560);
rtBG.setArcWidth(50);
rtBG.setArcHeight(50);
rtBG.setFill(Color.ORANGE);
VBox ltVBox = new VBox(50);
ltVBox.setAlignment(Pos.TOP_CENTER);
Text dealScre = new Text("Dealer: ");
Text playaSc = new Text("Player: ");
ltVBox.getChildren().addAll(dealScre, dCrd, mssg, pCrd, playaSc);
VBox rtVbox = new VBox(20);
rtVbox.setAlignment(Pos.CENTER);
final TextField tf = new TextField("BET");
tf.setDisable(true);
tf.setMaxWidth(50);
Text txt = new Text("MONEY");
Button playBtn = new Button("PLAY");
Button hitBtn = new Button("HIT");
Button stndBtn = new Button("STAND");
HBox btnHBox = new HBox(15, hitBtn, stndBtn);
btnHBox.setAlignment(Pos.CENTER);
rtVbox.getChildren().addAll(tf, playBtn, txt, btnHBox);
rtLay.getChildren().addAll(new StackPane(ltBG, ltVBox), new StackPane(rtBG, rtVbox));
rt.getChildren().addAll(bg, rtLay);
playBtn.disableProperty().bind(played);
hitBtn.disableProperty().bind(played.not());
stndBtn.disableProperty().bind(played.not());
playaSc.textProperty().bind(new SimpleStringProperty("Player: ").concat(playa.valueProperty().asString()));
dealScre.textProperty().bind(new SimpleStringProperty("Dealer: ").concat(dealr.valueProperty().asString()));
playa.valueProperty().addListener((obs, old, newValue) -> {
if (newValue.intValue() >= 21) {
quitGame();
}
});
dealr.valueProperty().addListener((obs, old, newValue) -> {
if (newValue.intValue() >= 21) {
quitGame();
}
});
playBtn.setOnAction(event -> {
strtGame();
});
hitBtn.setOnAction(event -> {
playa.takeCard(dck.drawingCards());
});
stndBtn.setOnAction(event -> {
while (dealr.valueProperty().get() < 17) {
dealr.takeCard(dck.drawingCards());
}
quitGame();
});
return rt;
}
private void strtGame() {
played.set(true);
mssg.setText("");
dck.refilling();
dealr.res();
playa.res();
dealr.takeCard(dck.drawingCards());
dealr.takeCard(dck.drawingCards());
playa.takeCard(dck.drawingCards());
playa.takeCard(dck.drawingCards());
}
private void quitGame() {
played.set(false);
int dealVal = dealr.valueProperty().get();
int playVal = playa.valueProperty().get();
String winningSide = "Exceptional case: d: " + dealVal + " p: " + playVal;
if (dealVal == 21 || playVal > 21 || dealVal == playVal
|| (dealVal < 21 && dealVal > playVal)) {
winningSide = "DEALER";
}
else if (playVal == 21 || dealVal > 21 || playVal > dealVal) {
winningSide = "PLAYER";
}
mssg.setText(winningSide + " 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);
}
}
Card.java
package com.almasb.blackjack;
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;
/**
*
* @author Akshay Bisht
*/
public class Card extends Parent {
private static final int CARD_WIDTH = 100;
private static final int CARD_HEIGHT = 140;
double length;
void add(Card crds) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
enum Suitss {
DIL, EET, HUKUM, KALAPAAN;
final Image img;
Suitss() {
this.img = new Image(Card.class.getResourceAsStream("images/".concat(name().toLowerCase()).concat(".png")),
32, 32, true, true);
}
}
enum ranks {
TOO(2), TREE(3), CHAAR(4), PAACH(5), CHEH(6), SAAT(7), AATH(8), NAU(9), DAS(10),
GHULAM(10), RANI(10), RAJA(10), IKKA(11);
final int val;
ranks(int val) {
this.val = val;
}
String displayName() {
return ordinal() < 9 ? String.valueOf(val) : name().substring(0, 1);
}
}
public final Suitss suits;
public final ranks ranking;
public final int val;
public Card(Suitss suits, ranks ranking) {
this.suits = suits;
this.ranking = ranking;
this.val = ranking.val;
Rectangle bg = new Rectangle(CARD_WIDTH, CARD_HEIGHT);
bg.setArcWidth(20);
bg.setArcHeight(20);
bg.setFill(Color.WHITE);
Text txt1 = new Text(ranking.displayName());
txt1.setFont(Font.font(18));
txt1.setX(CARD_WIDTH - txt1.getLayoutBounds().getWidth() - 10);
txt1.setY(txt1.getLayoutBounds().getHeight());
Text txt2 = new Text(txt1.getText());
txt2.setFont(Font.font(18));
txt2.setX(10);
txt2.setY(CARD_HEIGHT - 10);
ImageView iv = new ImageView(suits.img);
iv.setRotate(180);
iv.setX(CARD_WIDTH - 32);
iv.setY(CARD_HEIGHT - 32);
getChildren().addAll(bg, new ImageView(suits.img), iv, txt1, txt2);
}
@Override
public String toString() {
return ranking.toString() + " of " + suits.toString();
}
}
Deck.java
package com.almasb.blackjack;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import com.almasb.blackjack.Card.ranks;
/**
*
* @author Akshay Bisht
*/
public class Hand {
private ObservableList<Node> crds;
private SimpleIntegerProperty val = new SimpleIntegerProperty(0);
private int aces = 0;
public Hand(ObservableList<Node> crds) {
this.crds = crds;
}
public void takeCard(Card crds) {
crds.add(crds);
if (crds.ranking == ranks.IKKA) {
aces++;
}
if (val.get() + crds.val > 21 && aces > 0) {
val.set(val.get() + crds.val - 10); //then count ace as '1' not '11'
aces--;
}
else {
val.set(val.get() + crds.val);
}
}
public void res() {
crds.clear();
val.set(0);
aces = 0;
}
public SimpleIntegerProperty valueProperty() {
return val;
}
}
Hand.java
package com.almasb.blackjack;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import com.almasb.blackjack.Card.ranks;
/**
*
* @author Akshay Bisht
*/
public class Hand {
private ObservableList<Node> crds;
private SimpleIntegerProperty val = new SimpleIntegerProperty(0);
private int aces = 0;
public Hand(ObservableList<Node> crds) {
this.crds = crds;
}
public void takeCard(Card crds) {
crds.add(crds);
if (crds.ranking == ranks.IKKA) {
aces++;
}
if (val.get() + crds.val > 21 && aces > 0) {
val.set(val.get() + crds.val - 10); //then count ace as '1' not '11'
aces--;
}
else {
val.set(val.get() + crds.val);
}
}
public void res() {
crds.clear();
val.set(0);
aces = 0;
}
public SimpleIntegerProperty valueProperty() {
return val;
}
}
Rate an upvote.......Thankyou
Hope this helps.....
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.