OBJECTIVE: The objective of the assignment is to write a GUI app using Java comp
ID: 3866997 • Letter: O
Question
OBJECTIVE:
The objective of the assignment is to write a GUI app using Java components (Java FX) to create the card game of your choice.
INSTRUCTIONS:
You are to create an application to play any card game that you choose. You should design the classes that will be needed for the game. The only restrictions are:
1. Use the Java GUI classes to make an attractive display of the cards. You do not have to use actual card images but to something that resembles a card. For example, you could do a rectangle about the size of a card and place the card information inside. You should use color to make it attractive.
2. Use any card game that you wish. The only requirement there is that it be a real card game. The design is totally up to you.
The following are the requirements of this program
1. the game must work without errors and without referencing external libraries outside JavaFX and Java 8.1.
2. The game must use a classic 52 card deck format (Ex: Black Jack, Go Fish, War, etc).
3. Do not create a program to play more complex games (Ex. Cardfight Vanguard, Magic the Gathering, Yu-Gi-Oh, etc.).
2. The program must have a clean and Attractive GUI.
The type of game you use does not count into the grade. Black jack is preferred, due to its simplicity and since it's players don't need a hand.
Explanation / 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;
/**
* Game's logic and UI
*
* @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 com.almasb.blackjack.Card.ranks;
import com.almasb.blackjack.Card.Suitss;
/**
*
* @author Akshay Bisht
*/
public class Deck {
private Card[] crds = new Card[52];
public Deck() {
refilling();
}
public final void refilling() {
int uu = 0;
for (Suitss suits : Suitss.values()) {
for (ranks ranking : ranks.values()) {
crds[uu++] = new Card(suits, ranking);
}
}
}
public Card drawingCards() {
Card crds = null;
while (crds == null) {
int indices = (int)(Math.random()*crds.length);
}
return crds;
}
}
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;
}
}
Please rate the answer if it helped.......Thankyou
Hope it helps......
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.