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; // the order of checking is important 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"); }
private Parent createContent() throws IOException { dealer = new Hand(dealerCards.getChildren()); player = new Hand(playerCards.getChildren()); Pane root = new Pane(); root.setPrefSize(windowWidth, windowHeight); Region background = new Region(); background.setPrefSize(windowWidth, windowHeight); background.setStyle("-fx-background-image: url('res/images/casino.jpg')"); 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); // Canvas canvas = new Canvas(560, 560); BufferedImage imgi; GraphicsContext gcfx = canvas.getGraphicsContext2D(); // Graphics gc = canvas.getGraphics(); Image zbyszek = new Image("/res/images/table.png", 560, 560, true, false); gcfx.setFill(DARKSLATEGRAY); gcfx.fillRoundRect(0, 0, 550, 300, 10, 100); gcfx.setFill(BLACK); gcfx.drawImage(zbyszek, 0, 280); // try { // imgi = ImageIO.read(Card.class.getResource("/res/images/table.png")); // // gcfx.drawImage(imgi, 300, 300, null); // canvas.setVisible(true); // } catch (Exception e) { // } Rectangle rightBG = new Rectangle(230, 560); rightBG.setArcWidth(50); rightBG.setArcHeight(50); rightBG.setFill(Color.ORANGE); // LEFT 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); // RIGHT 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); rightVBox.getChildren().addAll(btnPlay, buttonsHBox); // ADD BOTH STACKS TO ROOT LAYOUT rootLayout .getChildren() .addAll(new StackPane(canvas, leftVBox), new StackPane(rightBG, rightVBox)); root.getChildren().addAll(background, rootLayout); // BIND PROPERTIES 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(); } }); // INIT BUTTONS 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; }