Answer to Question #287244 in Java | JSP | JSF for hummi

Question #287244

JAVA FX BASED POKER TASK

Please show the output also

 See apps.game.Cards for starter code representing a deck of playing cards.

5 Card Draw Poker has many rules, but to make it simple to implement we will only use some of these rules. Any student who wants to implement more is welcome, but remember that the following rules must be implemented.

  • Game is played with one dealer and one player. The computer will act as the dealer.
  • You can use any image to show cards.
  • All bets are to be made BEFORE each hand is dealt and once the first card is dealt bets cannot be removed. If you do not place a bet the dealer will not deal you any cards. In this case Dealer should win the game.

Values for the Cards

  • The player is given two cards, both face up.
  • The dealer gets two card, one face up and one face down.
  • The suits are ignored, they don't count in the game at all.

The cards with two to nine showing are taken at face value


1
Expert's answer
2022-01-13T08:36:56-0500
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Random;

public class Main extends Application {
    public static final Random RANDOM = new Random();
    public static Label dealerLabel;
    public static Label playerLabel;
    public static ImageView[] dealerCards;
    public static ImageView[] playerCards;
    public static int[] dealerPoints;
    public static int[] playerPoints;
    public static int[] cards;

    public static void dealCards() {
        cards = new int[52];
        for (int i = 0; i < dealerCards.length; i++) {
            int card = dealCard();
            playerCards[i].setImage(new Image(card + ".png"));
            playerCards[i].setVisible(true);
            int faceValue = card % 13;
            playerPoints[i] = faceValue == 0 ? 11 : faceValue < 9 ? faceValue : 10;
            card = dealCard();
            faceValue = card % 13;
            dealerPoints[i] = faceValue == 0 ? 11 : faceValue < 9 ? faceValue : 10;
            if (i < dealerCards.length - 1) {
                dealerCards[i].setImage(new Image(card + ".png"));
            }
            dealerCards[i].setVisible(true);
        }
    }

    public static int dealCard() {
        while (true) {
            int card = RANDOM.nextInt(52);
            if (cards[card] == 0) {
                cards[card]++;
                return card;
            }
        }
    }

    public static void setDefault() {
        playerPoints = new int[2];
        dealerPoints = new int[2];
        playerLabel.setText("Player hand");
        dealerLabel.setText("Dealer hand");
        for (int i = 0; i < playerCards.length; i++) {
            playerCards[i].setVisible(false);
            dealerCards[i].setVisible(false);
        }
    }

    public static int total(int[] values) {
        int total = 0;
        for (int value : values) {
            total += value;
        }
        return total;
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        dealerLabel = new Label("Dealer hand");
        playerLabel = new Label("Player hand");
        playerCards = new ImageView[2];
        dealerCards = new ImageView[2];
        for (int i = 0; i < playerCards.length; i++) {
            playerCards[i] = new ImageView(new Image("card.png"));
            playerCards[i].setVisible(false);
            dealerCards[i] = new ImageView(new Image("card.png"));
            dealerCards[i].setVisible(false);
        }
        TextField bet = new TextField();
        bet.setPromptText("Enter your bet");
        bet.setMaxWidth(100);
        Button playButton = new Button("Play");
        playButton.setOnMouseReleased((MouseEvent mouseEvent) -> {
            if (mouseEvent.getButton() == MouseButton.PRIMARY) {
                int betValue = -1;
                try {
                    betValue = Integer.parseInt(bet.getText());
                    if (betValue > 0) {
                        setDefault();
                        dealCards();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (betValue == -1) {
                    primaryStage.setTitle("Player lose");
                } else {
                    int playerTotal = total(playerPoints);
                    int dealerTotal = total(dealerPoints);
                    playerLabel.setText("Player hand(" + playerTotal + ")");
                    dealerLabel.setText("Dealer hand(" + dealerTotal + ")");
                    if (playerTotal == 21) {
                        primaryStage.setTitle("Player win");
                    } else {
                        primaryStage.setTitle("Player lose");
                    }
                }
            }
        });
        HBox dealerHBox = new HBox(10, dealerCards);
        dealerHBox.setAlignment(Pos.CENTER);
        HBox playerHBox = new HBox(10, playerCards);
        playerHBox.setAlignment(Pos.CENTER);
        VBox vBox = new VBox(10, dealerLabel, dealerHBox, playerLabel, playerHBox, bet, playButton);
        vBox.setAlignment(Pos.CENTER);
        primaryStage.setScene(new Scene(new StackPane(vBox), 800, 600));
        primaryStage.sizeToScene();
        primaryStage.show();
    }

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog