DESCRIPTION HiLo is a card game where a card is dealt and the player has to gues
ID: 3702740 • Letter: D
Question
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. 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 (JAVAFX)
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. 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. Create classes for 'Card' and 'DeckOfCards' to help manage the game. The Cardclass 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. 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. 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.
Explanation / Answer
program:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.TextAlignment;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ProgressBar;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.application.Platform;
public class CardsHiLoGUI extends Application {
Label lblFirstCard, lblSecondCard, lblPrompt, lblResult, lblpBar;
Image imgFirstCard, imgSecondCard;
ImageView imvFirstCard, imvSecondCard;
Button btnDealFirst, btnDealSecond;
RadioButton rdoHigher, rdoLower;
ToggleGroup hiloGroup;
MenuBar mBar;
Menu menuFile;
Menu menuAbout;
MenuItem mItemNew;
MenuItem mItemShuffle;
MenuItem mItemExit;
MenuItem mItemAbout;
// Progressbar
ProgressBar pBar;
public CardsHiLoGUI() {
}
public void init() {
// create Game
Game game = new Game();
// Labels
lblFirstCard = new Label("First Card Dealt:");
lblSecondCard = new Label("Second Card Dealt:");
lblPrompt = new Label("The next Card will be:");
lblResult = new Label("The result is:");
lblpBar = new Label("5 correct guesses in a row needed to win");
// Images
imgFirstCard = new Image(getClass().getResource("cards.png").toString());
imgSecondCard = new Image(getClass().getResource(" cards.png").toString());
imvFirstCard = new ImageView(imgFirstCard);
imvSecondCard = new ImageView(imgSecondCard);
// Buttons
btnDealFirst = new Button("<- Deal First Card");
btnDealFirst.setOnAction(ae -> btnFirstClick(game));
btnDealSecond = new Button("Deal Second Card ->");
// Configure button size
btnDealFirst.setMinWidth(160);
btnDealSecond.setMinWidth(160);
btnDealSecond.setDisable(true);
btnDealSecond.setOnAction(ae -> btnSecondClick(game));
// Instantiate radio buttons
rdoHigher = new RadioButton("Higher");
rdoLower = new RadioButton("Lower");
hiloGroup = new ToggleGroup();
rdoHigher.setToggleGroup(hiloGroup);
rdoLower.setToggleGroup(hiloGroup);
rdoHigher.setSelected(true);
// Menu
mBar = new MenuBar();
menuFile = new Menu("File");
menuAbout = new Menu("About");
// menu item new game
mItemNew = new MenuItem("New game");
mItemNew.setOnAction(ae -> gameStart(game));
// menu item exit game
mItemExit = new MenuItem("Exit");
mItemExit.setOnAction(ae -> Platform.exit());
// menu item about
mItemAbout = new MenuItem("About");
mItemAbout.setOnAction(ae -> showAbout());
// add menus to menu bar
mBar.getMenus().add(menuFile);
mBar.getMenus().add(menuAbout);
// add entries to menu
menuFile.getItems().add(mItemNew);
menuFile.getItems().add(mItemExit);
menuAbout.getItems().add(mItemAbout);
// Progress bar
pBar = new ProgressBar(0);
pBar.setPrefWidth(480);
}// init
@Override
public void start(Stage primaryStage) throws Exception {
// Set the title
primaryStage.setTitle("Cards HiLo Game");
// Create a layout
GridPane gp = new GridPane();
gp.add(lblFirstCard, 0, 0);
gp.add(lblSecondCard, 2, 0);
gp.add(lblPrompt, 1, 1);
// Vbox
VBox playButtonBox = new VBox();
VBox vBoxMain = new VBox();
VBox vBoxUpper = new VBox();
vBoxUpper.getChildren().add(lblpBar);
vBoxUpper.getChildren().add(pBar);
vBoxMain.getChildren().add(mBar);
vBoxMain.getChildren().add(vBoxUpper);
vBoxMain.getChildren().add(gp);
// Padding & Spacing
gp.setVgap(10);
gp.setHgap(10);
gp.setAlignment(Pos.BASELINE_CENTER);
gp.setPadding(new Insets(10));
playButtonBox.setPadding(new Insets(5));
vBoxUpper.setPadding(new Insets(10));
playButtonBox.setSpacing(5);
vBoxUpper.setSpacing(5);
// add images
gp.add(imvFirstCard, 0, 1);
gp.add(imvSecondCard, 2, 1);
playButtonBox.getChildren().add(lblPrompt);
playButtonBox.getChildren().add(rdoHigher);
playButtonBox.getChildren().add(rdoLower);
playButtonBox.getChildren().add(btnDealFirst);
playButtonBox.getChildren().add(btnDealSecond);
gp.add(playButtonBox, 1, 1);
// Create a scene
Scene scene = new Scene(vBoxMain, 500, 350);
scene.getStylesheets().add("./stylesheet.css");
// Set the scene
primaryStage.setScene(scene);
// Show the stage
primaryStage.show();
}
public void stop() {
}// stop
public void showAbout() {
// create stage
Stage stageAbout = new Stage();
// set title
stageAbout.setTitle("About");
// create vbox and label for stage
VBox vBoxAbout = new VBox();
vBoxAbout.setPadding(new Insets(10));
vBoxAbout.setAlignment(Pos.TOP_CENTER);
// create button ok and Label
Button btnOK = new Button("OK");
// Create and style label for about box
Label lblAbout = new Label("Cards Hi Low Game");
lblAbout.setPadding(new Insets(10));
lblAbout.setTextAlignment(TextAlignment.CENTER);
// add label and button to about box
vBoxAbout.getChildren().add(lblAbout);
vBoxAbout.getChildren().add(btnOK);
// when button clicked, close about box
btnOK.setOnAction(ae -> stageAbout.close());
// create scene
Scene sceneAbout = new Scene(vBoxAbout);
sceneAbout.getStylesheets().add("./stylesheet.css");
// set scene
stageAbout.setScene(sceneAbout);
// show stage
stageAbout.show();
} // show about
/*
* Method to handle end of game
*/
public void alertGameEnd(Game game) {
// create stage
Stage stageAlert = new Stage();
// set title
stageAlert.setTitle("Alert");
// create vbox and label for stage
VBox vBoxAlert = new VBox();
vBoxAlert.setPadding(new Insets(10));
vBoxAlert.setAlignment(Pos.TOP_CENTER);
// create button ok and Label
Button btnOK = new Button("OK");
Label lblAlert = new Label("You ran out of cards. Start new Game");
lblAlert.setPadding(new Insets(10));
lblAlert.setTextAlignment(TextAlignment.CENTER);
vBoxAlert.getChildren().add(lblAlert);
vBoxAlert.getChildren().add(btnOK);
// close dialog when ok button is clicked
btnOK.setOnAction(ae -> {
game.restart();
stageAlert.close();
});
// create scene
Scene sceneAlert = new Scene(vBoxAlert);
sceneAlert.getStylesheets().add("./stylesheet.css");
// set scene
stageAlert.setScene(sceneAlert);
// show stage
stageAlert.show();
} // show alert
-
------------------------------- Method to show a dialog with a message (wrong/correct)---------------------
public void showMessage(String message, Game game) {
// create stage
Stage stageMessage = new Stage();
// set title
stageMessage.setTitle("Info");
// create vbox and label for stage
VBox vBoxMessage = new VBox();
vBoxMessage.setPadding(new Insets(10));
vBoxMessage.setAlignment(Pos.TOP_CENTER);
vBoxMessage.setMinWidth(150);
// create button ok and Label
Button btnOK = new Button("OK");
// create and style label
Label lblMessage = new Label(message);
lblMessage.setPadding(new Insets(10));
lblMessage.setTextAlignment(TextAlignment.CENTER);
// add label to vbox
vBoxMessage.getChildren().add(lblMessage);
vBoxMessage.getChildren().add(btnOK);
// on click close dialog
btnOK.setOnAction(ae -> {
btnFirstClick(game);
stageMessage.close();
});
// create scene
Scene sceneMessage = new Scene(vBoxMessage);
sceneMessage.getStylesheets().add("./stylesheet.css");
// set scene
stageMessage.setScene(sceneMessage);
// show stage
stageMessage.show();
} // show Message
public void btnFirstClick(Game game) {
// game.getDeck().getCurrentCard());
// System.out.println("Next Card before dealing card 1: " +
// game.getDeck().getNextCard());
// take a card from the top of the deck
Card card = game.getDeck().dealTopCard();
game.getDeck().setFirstCard(card);
// System.out.println("First card: "+ card.toString());
// change image to display the first card
Image img1 = new Image(getClass().getResource(card.toString()).toString());
imvFirstCard.setImage(img1);
// show default image in second card imageview
Image img2 = new Image(getClass().getResource("cards.png").toString());
imvSecondCard.setImage(img2);
btnDealFirst.setDisable(true);
btnDealSecond.setDisable(false);
} // buttonFirstClick
public void btnSecondClick(Game game) {
// when second button is clicked
// deal top card
Card card = game.getDeck().dealTopCard();
System.out.println("Second Card: " + card.toString());
// store card in variable secondCard
game.getDeck().setSecondCard(card);
// show image of second card
Image img2 = new Image(getClass().getResource(card.toString()).toString());
imvSecondCard.setImage(img2);
// disable second button and enable first
btnDealSecond.setDisable(true);
btnDealFirst.setDisable(false);
System.out.println("Current Card after dealing second card: " + game.getDeck().getCurrentCard());
System.out.println("Next Card after dealing second card: " + game.getDeck().getNextCard());
boolean isWinner = false;
if (rdoLower.isSelected()) {
if (game.evaluateWin(0) == true) {
isWinner = true;
}
} else if (rdoHigher.isSelected()) {
if (game.evaluateWin(1) == true) {
isWinner = true;
}
;
}
// correct
if (isWinner == true && (game.getWinCounter() < 5)) {
showMessage("Correct", game);
// else show message wrong
} else if (isWinner == false) {
showMessage("Wrong", game);
}
pBar.setProgress((double) game.getWinCounter() / 5);
if (game.getWinCounter() == 5) {
showMessage("You won the game. Play again", game);
gameStart(game);
}
if (game.getDeck().isEmpty() == true) {
alertGameEnd(game);
gameStart(game);
}
}
public void gameStart(Game game) {
game.restart();
// replace both imv with default image
Image img = new Image(getClass().getResource("cards.png").toString());
imvFirstCard.setImage(img);
imvSecondCard.setImage(img);
btnDealSecond.setDisable(true);
btnDealFirst.setDisable(false);
pBar.setProgress(0);
}
public static void main(String[] args) {
launch(args);
}
}
******
public class Card {
private int rank;
private int suit;
public Card() {
}
public Card(int rank, int suit) {
super();
this.rank = rank;
this.suit = suit;
}
public String toString() {
String cardSuit = "";
String cardRank = "";
String cardString;
int cs = getSuit();
int cr = getRank();
switch (cs) {
case 0: cardSuit="hearts";
break;
case 1: cardSuit="diamonds";
break;
case 2: cardSuit="clubs";
break;
case 3: cardSuit="spades";
break;
default: cardSuit="n/a";
}
switch (cr) {
case 1: cardRank="ace";
break;
case 2: cardRank="2";
break;
case 3: cardRank="3";
break;
case 4: cardRank="4";
break;
case 5: cardRank="5";
break;
case 6: cardRank="6";
break;
case 7: cardRank="7";
break;
case 8: cardRank="8";
break;
case 9: cardRank="9";
break;
case 10:cardRank="10";
break;
case 11:cardRank="jack";
break;
case 12:cardRank="queen";
break;
case 13:cardRank="king";
break;
default:cardRank="n/a";
}
cardString = "./cards/" + cardRank + "_of_" + cardSuit + ".png";
return cardString;
}
public boolean rankIsLessThan (Card second){
if (this.rank < second.getRank()){
return true;
} else {
return false;
}
}
public boolean rankIsGreaterThan (Card second){
if (this.rank > second.getRank()){
return true;
} else {
return false;
}
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public int getSuit() {
return suit;
}
public void setSuit(int suit) {
this.suit = suit;
}
}
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class DeckOfCards {
private int currentCard;
private int nextCard;
private int[] suits;
private int[] ranks;
private Card[] deck;
private Card firstCard;
private Card secondCard;
public DeckOfCards() {
super();
this.suits = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
this.ranks = new int[] { 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0,
1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 };
this.deck = new Card[52];
for (int i = 0; i < this.deck.length; i++) {
this.deck[i] = new Card(suits[i], ranks[i]);
}
this.currentCard = 0;
this.nextCard = 1;
}
----------------------------------------------------------Method to shuffle the deck--------------------------------------
public void shuffle() {
Card tempStore;
for (int i = 0; i < this.deck.length; i++) {
int random = (int) ((Math.random() * 52));
tempStore = deck[i];
deck[i] = deck[random];
deck[random] = tempStore;
}
}
public boolean isEmpty() {
if (this.nextCard > 50) {
return true;
} else {
return false;
}
}
public Card dealTopCard() {
this.nextCard++;
this.currentCard = nextCard - 1;
return deck[currentCard];
}
public int getCurrentCard() {
return currentCard;
}
public void setCurrentCard(int currentCard) {
this.currentCard = currentCard;
}
public int getNextCard() {
return nextCard;
}
public void setNextCard(int nextCard) {
this.nextCard = nextCard;
}
public Card getFirstCard() {
return firstCard;
}
public void setFirstCard(Card firstCard) {
this.firstCard = firstCard;
}
public Card getSecondCard() {
return secondCard;
}
public void setSecondCard(Card secondCard) {
this.secondCard = secondCard;
}
}
----------------------------------------------------------------------
public class Game {
private DeckOfCards deck;
private int winCounter;
public Game() {
super();
this.deck = new DeckOfCards();
deck.shuffle();
this.winCounter = 0;
}
public boolean evaluateWin(int hiLo) {
if (hiLo == 0) {
if (this.deck.getSecondCard().rankIsLessThan(this.getDeck().getFirstCard())) {
this.winCounter++;
return true;
} else {
this.winCounter = 0;
return false;
}
} else if (hiLo == 1) {
if (this.deck.getSecondCard().rankIsGreaterThan(this.deck.getFirstCard())) {
this.winCounter++;
return true;
} else {
this.winCounter = 0;
return false;
}
} else {
this.winCounter = 0;
return false;
}
}
public void restart() {
deck.shuffle();
this.getDeck().setCurrentCard(0);
this.getDeck().setNextCard(0);
this.getDeck().setFirstCard(null);
this.getDeck().setSecondCard(null);
}
public DeckOfCards getDeck() {
return deck;
}
public void setDeck(DeckOfCards deck) {
this.deck = deck;
}
public int getWinCounter() {
return winCounter;
}
public void setWinCounter(int winCounter) {
this.winCounter = winCounter;
}
}
.root{
-fx-base: rgb(21, 225, 15);
-fx-background: rgb(0, 0, 0);
}
.button {
-fx-base: rgb(224, 224, 224);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.