Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVAFX ONLY(no swing or awt): HERE IS THE STARTER CLASSES THAT I HAVE ALREADY: CARDS.MODEL PACKAGE: CARDS CLASS: import java.util.Comparator; public class Card { public

JAVAFX ONLY(no swing or awt):

HERE IS THE STARTER CLASSES THAT I HAVE ALREADY:

CARDS.MODEL PACKAGE:

CARDS CLASS:

import java.util.Comparator;

public class Card

{

public final int SUIT_SIZE = 13;

public static final int CLUB = 0;

public static final int DIAMOND = 1;

public static final int HEART = 2;

public static final int SPADE = 3;

private int suit; // clubs = 0, diamonds = 1, hearts = 2, spades = 3

private int rank; // deuce = 0, three = 1, four = 2, ..., king = 11, ace = 12

private boolean isFaceUp = true; // not used for our program

// create a new card based on integer 0 = 2C, 1 = 3C, ..., 51 = AS

public Card(int card) {

rank = card % SUIT_SIZE;

suit = card / SUIT_SIZE;

}

public int getRank() {

return rank;

}

public int getSuit() {

return suit;

}

public boolean isFaceUp()

{

return isFaceUp;

}

public void flip()

{

isFaceUp = !isFaceUp;

}

// represent cards like "2H", "9C", "JS", "AD"

public String toString() {

String ranks = "23456789TJQKA";

String suits = "CDHS";

return ranks.charAt(rank) + "" + suits.charAt(suit);

}

}

DECK CLASS:

import java.util.ArrayList; import java.util.Random;

public class Deck { // list of cards still in the deck private ArrayList deck = new ArrayList<>();

// list of cards being used private ArrayList used = new ArrayList<>();

// used to shuffle the deck Random dealer = new Random(); public Deck() { // builds the deck for (int i = 0; i < 52; i++) { deck.add(new Card(i)); } } public ArrayList deal(int handSize) { ArrayList hand = new ArrayList<>(); // do we need more cards? If so, shuffle if (deck.size() < handSize) { shuffle(); }

for (int i=0; i < handSize; i++) { Card next = deck.remove(deck.size() - 1); hand.add(next); used.add(next); } return hand; } public void shuffle() { deck.addAll(used); for (int i=0; i < deck.size() - 1; i++) { int swap = dealer.nextInt(deck.size() - i) + i; if (swap != i) { Card tmp1 = deck.get(i); Card tmp2 = deck.get(swap); deck.set(i, tmp2); deck.set(swap, tmp1); } } } // just used for testing public static void showHand(ArrayList hand) { for (Card c : hand) { System.out.printf(" %s",c); } System.out.println(); } // just used for testing public static void main(String args[]) { Deck deck = new Deck();

deck.shuffle(); ArrayList hand = deck.deal(5); showHand(hand);

deck.shuffle(); hand = deck.deal(5); showHand(hand); }

}

CARDS.VIEW PACKAGE:

HANDDISPLAYWINDOW CLASS:

import cards.model.Card; import cards.model.Deck; import javafx.geometry.Orientation; import javafx.scene.Scene; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.BorderPane; import javafx.stage.Stage;

public class HandDisplayWindow { private final int SUIT_SIZE = 13; private Image[] cardImages = new Image[4*SUIT_SIZE]; private Stage myStage; ListView listView = new ListView<>(); Deck deck; int handSize;

public HandDisplayWindow(Stage stage, int size) { handSize = size; myStage = stage; myStage.setTitle("Card Hand"); BorderPane pane = new BorderPane(); Scene scene = new Scene(pane); myStage.setScene(scene); listView.setCellFactory(param -> new ListCell() { private ImageView imageView = new ImageView();

@Override public void updateItem(Card card, boolean empty) { super.updateItem(card, empty); if (empty) { setGraphic(null); } else { // determine the index of the card int index = card.getSuit() * SUIT_SIZE + card.getRank(); imageView.setImage(cardImages[index]); imageView.setPreserveRatio(true); imageView.setFitWidth(50); setGraphic(imageView); } } });

listView.setOrientation(Orientation.HORIZONTAL); pane.setCenter(listView);

myStage.setHeight(150); myStage.setWidth(68 * handSize);

loadImages(); }

private void loadImages() { String resourceDir = "file:resources/cardspng/"; char[] suits = { 'c', 'd', 'h', 's' }; char[] rank = { '2', '3', '4', '5', '6', '7', '8', '9', '0', 'j', 'q', 'k', 'a' }; int slot = 0; // load images for (int s = 0; s < 4; s++) { for (int r = 0; r < SUIT_SIZE; r++) { String path = resourceDir + suits[s] + rank[r] + ".png"; cardImages[slot] = new Image(path); slot++; } } }

public void show(Deck deck) { this.deck = deck; if (deck != null) listView.getItems().setAll(deck.deal(handSize)); myStage.show(); } }

MAINAPPLICATION CLASS:

import cards.model.Deck; import javafx.application.Application; import javafx.stage.Stage;

public class MainApplication extends Application {

@Override public void start(Stage primaryStage) throws Exception { HandDisplayWindow ui = new HandDisplayWindow(primaryStage, 13); Deck deck = new Deck(); deck.shuffle(); ui.show(deck); }

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

}

*************DIRECTIONS ARE BELOW ON WHAT I ACTUALLY NEED TO DO, THAT I DO NOT KNOW HOW TO DO:

*************NOW, WHAT I ACTUALLY NEED HELP WITH IS THE FOLLOWING:

Implement the Comparable interface in the Card class. (15 pts)

Sort the cards lowest to highest based on suit within rank, e.g.

2 clubs

2 diamonds

2 hearts

2 spades

3 clubs

3 diamonds

etc.

Add a static nested class to the Card class that implements the Comparator interface. (15 pts)

Sort the cards lowest to highest based on rank within suit, e.g.

2 clubs

3 clubs

4 clubs

5 clubs

etc.

In the UI, add the following to your interface.

Deal Button (10 pts)

Implement the Deal action handler using an inner class (not local). You may want to pull some of the code from the show method that is already there. Or better, put the common code into a class and call it.

Fish Sort Button (15 pts)

Sort the code in a way that would be useful playing Go Fish. I.e. with all of the cards grouped together. Implement the action handler using a lambda expression in the UI. The code in the lambda expression should depend on the Card class for the actual sorting (i.e. the natural order). It must not call any other method in the UI class.

Spades Sort Button (15 pts)

Order the cards in such a way that they would be useful playing Spades; i.e. with the cards sorted by rank within suit and the suits in the order provided by the nested class in #2 above. You actually want to reverse this sort before displaying. Implement the action handler using an anonymous class in the UI. The code in the anonymous class should depend on the Card class for the actual sorting; i.e. the Comparator that you added above.

Reverse Toggle Button (10 pts)

If this toggle is checked, the order of the sort is reversed, e.g.

AS, AH, 8H, 8D, 8C

becomes

8C, 8D, 8H, AH, AS

Clicking the toggle should have no effect. However, pushing the Spades Sort buttons should result in a reversed ordering if the toggle button is selected. Unchecking the button and pushing Spades Sort should result in the original sort. Note that this does not merely reverse the order; if the button is checked pushing the Spades Sort button any number of times should always result in the same ordering. You do not have to apply this to the Fish Sort, but see below. This action can be accomplished in a number of ways. Some are simpler than others, some more efficient.

Extra Credit

Fish Sort and Reverse Toggle (3 pts)

Find a means to reverse sort that does not require reversing after sorting for the Fish Sort. That is, the sort itself should do the reverse. Do not add code outside the lambda expression. It should check whether the toggle button is checked and act appropriately. You must keep the Fish Sort action in a lambda expression. This may take some research.

Bridge Sort Button (10 pts)

This sort is similar to the Spade sort where all of the cards are grouped by suit. However, the groups of suits should then be sorted by how many are in each suit. E.g. if there are 2 spades, 5 hearts, 3 diamonds and 3 clubs, the hearts would be first, followed by the diamonds, clubs and spades in that order. This will take some thinking on your part; it isnt a simple problem. You can do this in an inner class, anonymous class or even a static method in the Card classor something else you think of.

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

Step: 3

blur-text-image

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

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students also viewed these Databases questions