Question
JAVAFX Description HiLo is a card game where a card is dealt and the player has to guess whether the next card that is dealt
JAVAFX
Description
HiLo is a card game where a card is dealt and the player has to guess whether the next card that is dealt will be higher or lower than the first card. In the application a card is first dealt on the left hand side. Then, a guess as to whether the next card dealt will be higher or lower is indicated, using the radio buttons in the centre. On clicking the 'Deal Second Card' button, another card is dealt on the right hand side. If the card dealt is in accordance with the guess of the user, then the user wins. Five consecutive, correct guesses constitute an overall game win. A new game is begun using the menu by selecting the 'New Game' item. Note that for the purpose of comparing cards, only the face of the card matters. The suit, i.e, whether Hearts, Spades, Clubs, etc., is unimportant. A second card of equal face value is considered a lose for the purposes of this game.
Requirement
1. Create a project called 'CardsHiLoGUI'. Create a main interface for the application. Remember to create a ToggleGroup for the radio buttons and set the ToggleGroup for each button to ensure mutual exclusivity. At application startup, the card images are populated with default cards. Use comments in your code to explain your approach in relation to the GUI construction.
2. Add a menu bar and 'File' menu. The 'File' menu should contain the items 'New Game', 'Shuffle' and 'Exit'. Implement these menu items fully. Add a 'Help' menu with an 'About' item to show your student name and number in a dialog.
3. Create classes for 'Card' and 'DeckOfCards' to help manage the game. The Card class should include methods such as 'isEqual( )', 'isLower( )', 'isHigher( )' and
'toString( )'. DeckOfCards should include methods such as 'dealTopCard( )', 'isEmpty( )' and 'shuffle( )'. Provide one or more constructors as appropriate for
each class.
4. Implement dealing of each card, determination of a win/lose and user feedback in a label. Store wins and inform the user of an overall game win on achieving 5 consecutive, correct guesses.
5. Add a progress bar and progress indicator to indicate progress towards 5 consecutive wins leading to an overall game win. Provide appropriate feedback on achieving an overall game win. Use a stylesheet to apply an attractive look and feel to the application.
I have completed number 1 and 2, I need help with number 3 please.
Code so far:
package cardshilogui;
import java.io.FileInputStream; import javafx.application.Application; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Menu; import javafx.scene.control.MenuBar; import javafx.scene.control.MenuItem; import javafx.scene.control.RadioButton; import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.stage.Stage; import javafx.scene.layout.VBox; import javafx.scene.layout.HBox; import javafx.scene.image.ImageView; import javafx.scene.image.Image; import javafx.stage.Modality; import javafx.scene.text.TextAlignment; import javafx.scene.control.ProgressBar; import javafx.application.Platform;
public class CardsHiLoGUI extends Application {
//Label Constructors Label firstCardDealt = new Label("First Card Dealt:"); Label secondCardDealt = new Label("Second Card Dealt:"); Label nextCardDealt = new Label("Next Card Dealt:"); Label higherYouWin = new Label("Higher You Win!"); Label higherYouLose = new Label("Lower You Lose!"); Label lowerYouWin = new Label("Lower You Win!"); Label lowerYouLose = new Label("Lower You Lose!"); Label nextCardWillBe = new Label("Next card will be: ");
// Button Constructors Button dealFirstCardB = new Button("<- Deal First Card"); Button dealSecondCardB = new Button("Deal Second Card ->");
// Radio Button Constructors RadioButton higherRB = new RadioButton("Higher"); RadioButton lowerRB = new RadioButton("Lower");
// Toggle Group Constructor ToggleGroup radioGroup = new ToggleGroup();
BorderPane root = new BorderPane();
// Vertical box Constructor (to put in centre with a GridPane layout) GridPane grid = new GridPane(); VBox centreBox = new VBox(10);
// Horizontal Box construtor (to hold menubar) HBox menuBox = new HBox();
// MenuBar constructor MenuBar menuBar = new MenuBar();
// Constructing File Menu and menu items Menu fileMenu = new Menu("File"); MenuItem newGame = new MenuItem("New Game"); MenuItem shuffle = new MenuItem("Shuffle"); MenuItem exit = new MenuItem("Exit");
// Constructing Help menu Menu helpMenu = new Menu("Help"); MenuItem about = new MenuItem("About");
// Constructing ToggleGroup ToggleGroup group = new ToggleGroup(); //Constructing a vertical box to host radio buttons VBox radioBox = new VBox(10);
Image king_of_clubs = new Image("/cards/king_of_clubs.png"); Image hearts = new Image("/cards/4_of_hearts.png"); ImageView imageView1 = new ImageView(king_of_clubs); ImageView imageView2 = new ImageView(hearts);
HBox hbox1 = new HBox(imageView1);
HBox hbox2 = new HBox(imageView2);
//Image Constructor //FileInputStream input = new FileInputStream(""); //Image image = new Image("king_of_hearts.png"); // ImageView imageView1 = new ImageView(image); //imageView1.setImage(image); // HBox hbox = new HBox(imageView1); //HBox hbox = new HBox(imageView1); //ImageView Constructor @Override public void start(Stage primaryStage) throws Exception { setToggleGroup(); addComponents(); setScene(primaryStage); setSpacing(); //FileInputStream input = new FileInputStream("/Users/johnoleary/NetBeansProjects/CardsHiLoGUI/src/cards/king_of_clubs.png");
imageView1.setImage(king_of_clubs); root.setLeft(hbox1); root.setRight(hbox2);
}
public void addComponents() { // Adding menubox to top of borderPane root.setTop(menuBox); // Insering menuBar into menuBox menuBox.getChildren().add(menuBar); // Adding menu items to fileMenu fileMenu.getItems().addAll(newGame, shuffle, exit); // Adding menu items to helpMenu helpMenu.getItems().addAll(about); // Adding menus to the menu bar menuBar.getMenus().addAll(fileMenu, helpMenu); // Adding a VBox to center of borderpane root.setCenter(centreBox); // Adding gridpane layout to this verticalBox centreBox.getChildren().add(grid); // Adding elements/nodes to the grid grid.add(nextCardWillBe, 0, 1); grid.add(radioBox, 0, 2); radioBox.getChildren().add(higherRB); radioBox.getChildren().add(lowerRB); higherRB.setSelected(true);
grid.add(dealFirstCardB, 0, 3); grid.add(dealSecondCardB, 0, 5);
}
public void setToggleGroup() { higherRB.setToggleGroup(group); lowerRB.setToggleGroup(group);
}
public void setSpacing() { grid.setAlignment(Pos.CENTER); grid.setHgap(10); grid.setVgap(10); grid.setPadding(new Insets(0, 25, 25, 25));
// hbBtn.setAlignment(Pos.BOTTOM_LEFT); // Centering our heading label // root.setHalignment(headingLabel, HPos.CENTER); }
public void setScene(Stage primaryStage) { Scene scene = new Scene(root, 500, 350); // width primaryStage.setScene(scene); primaryStage.setTitle("Hi-Lo Card Game"); primaryStage.show();
}
/** * @param args the command line arguments */ public static void main(String[] args) { launch(args); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started