Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using JAVA FX how to combine the two games below into one interface that allows the user to pick which specific game they would like

Using JAVA FX how to combine the two games below into one interface that allows the user to pick which specific game they would like to play. they should be able to choose and then when the game is done , switch to another game if wanted.

Black jack javafx-first program

package application;

import java.util.Arrays;

import java.util.ArrayList;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.layout.HBox;

import javafx.scene.layout.GridPane;

import javafx.scene.control.Label;

import javafx.scene.control.Labeled;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.stage.Stage;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

public class Main extends Application {

public String btHitY;

public int NumberPlayerCards;

public int NumberDealerCards;

public int NUMBER_OF_CARDS;

public int PlayerCards[];

public int DealerCards[];

public Image imagesP[];

public Image imagesD[];

public int deck[];

public String URLBase;

public GridPane pane;

public Main() {

this.btHitY = " ";

this.btHitY = new String();

}

@Override // Override the start method in the Application class

public void start(Stage primaryStage) {

//Create array deck, suit string, and rank string

deck = new int[52];

String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};

String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

int NUMBER_OF_CARDS = 4;

//Initialize the cards

for (int i = 0; i < deck.length; i++)

deck[i] = i;

//Shuffle the cards

for (int i = 0; i < deck.length; i++) {

//Generate an index randomly

int index = (int)(Math.random() * deck.length);

int temp = deck[i];

deck[i] = deck[index];

deck[index] = temp;

}

int NumberPlayerCards = 2;

int NumberDealerCards = 2;

int[] PlayerCards = new int[50];

int[] DealerCards = new int[50];

//Display the first cards

for (int i = 0; i < 8; i++) {

String suit = suits[deck[i] / 13];

String rank = ranks[deck[i] % 13];

System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);

}

for (int i = 0; i < NumberPlayerCards; i++)

PlayerCards[i] = deck[i * 2];

for (int i = 0; i < NumberDealerCards; i++)

DealerCards[i] = deck[i * 2 + 1];

// Create a pane to hold the image views

GridPane pane = new GridPane();

pane.setAlignment(Pos.CENTER);

pane.setPadding(new Insets(5, 5, 5, 5));

pane.setHgap(5);

pane.setVgap(5);

Image[] imagesP = new Image[50];

Image[] imagesD = new Image[50];

for (int i = 0; i < NumberPlayerCards; i++) {

int cardForPrint = PlayerCards[i] + 1;

System.out.println(URLBase + cardForPrint + ".png");

imagesP[i] = new Image(URLBase + cardForPrint + ".png");

}

for (int i = 0; i < NumberDealerCards; i++) {

int cardForPrint = DealerCards[i] + 1;

System.out.println(URLBase + cardForPrint + ".png");

imagesD[i] = new Image(URLBase + cardForPrint + ".png");

}

//rotate flag image to cover dealer card

Image flag = new Image("http://www.cs.armstrong.edu/liang/common/image/us.gif");

ImageView imageFlag = new ImageView(flag);

imageFlag.setRotate(90);

imageFlag.setFitHeight(75);

imageFlag.setFitWidth(95);

pane.add(new Label("Player Cards"), 0, 0);

pane.add(new ImageView(imagesP[0]), 1, 0);

pane.add((imageFlag), 1, 1);

pane.add(new Label("Dealer Cards"), 0, 1);

pane.add(new ImageView(imagesP[1]), 2, 0);

pane.add(new ImageView(imagesD[1]), 2, 1);

Button btHit = new Button("Hit");

Button btStay = new Button("Stay");

pane.add(btHit, 1, 2);

pane.add(btStay, 2, 2);

// Create a scene and place it in the stage

Scene scene = new Scene(pane, 1200, 700);

primaryStage.setTitle("Black Jack"); // Set the stage title

primaryStage.setScene(scene); // Place the scene in the stage

primaryStage.show(); // Display the stage

HitHandlerClass handlerHit = new HitHandlerClass();

btHitY = " ";

btHit.setOnAction(handlerHit);

/* if (btHitY.equals("Hit")); {

NumberPlayerCards = NumberPlayerCards + 1;

NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;

PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];

for (int j = 0; j < NumberPlayerCards; j++){

System.out.println(PlayerCards[j]);

}

System.out.println(NumberPlayerCards);

int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;

imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");

pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);

btHitY = " ";

primaryStage.show();

} */

}

/**

* The main method is only needed for the IDE with limited

* JavaFX support. Not needed for running from the command line.

* @param args

*/

public static void main(String[] args) {

launch(args);

}

class HitHandlerClass implements EventHandler {

@Override

public void handle(ActionEvent e) {

NumberPlayerCards = NumberPlayerCards + 1;

NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;

PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];

for (int j = 0; j < NumberPlayerCards; j++){

System.out.println(PlayerCards[j]);

}

System.out.println(NumberPlayerCards);

int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;

imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");

pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);

btHitY = " ";

}

}

}

Next game

package application;

import java.util.Arrays;

import java.util.ArrayList;

import javafx.collections.FXCollections;

import javafx.collections.ObservableList;

import javafx.application.Application;

import javafx.geometry.Pos;

import javafx.geometry.Insets;

import javafx.scene.Scene;

import javafx.scene.layout.HBox;

import javafx.scene.layout.GridPane;

import javafx.scene.control.Label;

import javafx.scene.control.Labeled;

import javafx.scene.control.Button;

import javafx.scene.control.TextField;

import javafx.scene.image.Image;

import javafx.scene.image.ImageView;

import javafx.stage.Stage;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

public class Main extends Application {

public String btHitY;

public int NumberPlayerCards;

public int NumberDealerCards;

public int NUMBER_OF_CARDS;

public int PlayerCards[];

public int DealerCards[];

public Image imagesP[];

public Image imagesD[];

public int deck[];

public String URLBase;

public GridPane pane;

public Main() {

this.btHitY = " ";

this.btHitY = new String();

}

@Override // Override the start method in the Application class

public void start(Stage primaryStage) {

//Create array deck, suit string, and rank string

deck = new int[52];

String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};

String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

int NUMBER_OF_CARDS = 4;

//Initialize the cards

for (int i = 0; i < deck.length; i++)

deck[i] = i;

//Shuffle the cards

for (int i = 0; i < deck.length; i++) {

//Generate an index randomly

int index = (int)(Math.random() * deck.length);

int temp = deck[i];

deck[i] = deck[index];

deck[index] = temp;

}

int NumberPlayerCards = 2;

int NumberDealerCards = 2;

int[] PlayerCards = new int[50];

int[] DealerCards = new int[50];

//Display the first cards

for (int i = 0; i < 8; i++) {

String suit = suits[deck[i] / 13];

String rank = ranks[deck[i] % 13];

System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);

}

for (int i = 0; i < NumberPlayerCards; i++)

PlayerCards[i] = deck[i * 2];

for (int i = 0; i < NumberDealerCards; i++)

DealerCards[i] = deck[i * 2 + 1];

// Create a pane to hold the image views

GridPane pane = new GridPane();

pane.setAlignment(Pos.CENTER);

pane.setPadding(new Insets(5, 5, 5, 5));

pane.setHgap(5);

pane.setVgap(5);

Image[] imagesP = new Image[50];

Image[] imagesD = new Image[50];

for (int i = 0; i < NumberPlayerCards; i++) {

int cardForPrint = PlayerCards[i] + 1;

System.out.println(URLBase + cardForPrint + ".png");

imagesP[i] = new Image(URLBase + cardForPrint + ".png");

}

for (int i = 0; i < NumberDealerCards; i++) {

int cardForPrint = DealerCards[i] + 1;

System.out.println(URLBase + cardForPrint + ".png");

imagesD[i] = new Image(URLBase + cardForPrint + ".png");

}

//rotate flag image to cover dealer card

Image flag = new Image("http://www.cs.armstrong.edu/liang/common/image/us.gif");

ImageView imageFlag = new ImageView(flag);

imageFlag.setRotate(90);

imageFlag.setFitHeight(75);

imageFlag.setFitWidth(95);

pane.add(new Label("Player Cards"), 0, 0);

pane.add(new ImageView(imagesP[0]), 1, 0);

pane.add((imageFlag), 1, 1);

pane.add(new Label("Dealer Cards"), 0, 1);

pane.add(new ImageView(imagesP[1]), 2, 0);

pane.add(new ImageView(imagesD[1]), 2, 1);

Button btHit = new Button("Hit");

Button btStay = new Button("Stay");

pane.add(btHit, 1, 2);

pane.add(btStay, 2, 2);

// Create a scene and place it in the stage

Scene scene = new Scene(pane, 1200, 700);

primaryStage.setTitle("Black Jack"); // Set the stage title

primaryStage.setScene(scene); // Place the scene in the stage

primaryStage.show(); // Display the stage

HitHandlerClass handlerHit = new HitHandlerClass();

btHitY = " ";

btHit.setOnAction(handlerHit);

/* if (btHitY.equals("Hit")); {

NumberPlayerCards = NumberPlayerCards + 1;

NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;

PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];

for (int j = 0; j < NumberPlayerCards; j++){

System.out.println(PlayerCards[j]);

}

System.out.println(NumberPlayerCards);

int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;

imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");

pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);

btHitY = " ";

primaryStage.show();

} */

}

/**

* The main method is only needed for the IDE with limited

* JavaFX support. Not needed for running from the command line.

* @param args

*/

public static void main(String[] args) {

launch(args);

}

class HitHandlerClass implements EventHandler {

@Override

public void handle(ActionEvent e) {

NumberPlayerCards = NumberPlayerCards + 1;

NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;

PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];

for (int j = 0; j < NumberPlayerCards; j++){

System.out.println(PlayerCards[j]);

}

System.out.println(NumberPlayerCards);

int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;

imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");

pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);

btHitY = " ";

}

}

}

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_2

Step: 3

blur-text-image_3

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

Database Systems For Advanced Applications Dasfaa 2023 International Workshops Bdms 2023 Bdqm 2023 Gdma 2023 Bundlers 2023 Tianjin China April 17 20 2023 Proceedings Lncs 13922

Authors: Amr El Abbadi ,Gillian Dobbie ,Zhiyong Feng ,Lu Chen ,Xiaohui Tao ,Yingxia Shao ,Hongzhi Yin

1st Edition

3031354141, 978-3031354144

More Books

Students also viewed these Databases questions

Question

What design characteristics make for a successful Web page?

Answered: 1 week ago

Question

Complexity of linear search is O ( n ) . Your answer: True False

Answered: 1 week ago