0

I have an object array in which each object in the array has an integer component along with a string and a buffered image component. My code creates a deck of fifty two cards each with a unique integer. I want to make a program that finds cards based on there integer value.

So for example if the integer 10 corresponds to the king of spades, the user can enter in the number 10 and the program will find the king of spades and move it to the top of the deck. How can I find a card based on only its' integer value?

public class Card_Class {
    private String suit, face;
    private int value;
    public BufferedImage cardImage;

public Card_Class(String suit, String face, int value, BufferedImage card) {
    this.suit = suit;
    this.face = face;
    this.value = value;
    cardImage = card;

}

public static void main(String[] args) throws IOException {

    Card_Class HeartKing = new Card_Class("Hearts", "King", 13, ImageIO.read(new File("HeartKing.jpg")));
    }
}

Here is the deck constructor:

public class Deck {

public static Card_Class[] deckOfCards;
private int currentCard;

public Deck() throws IOException {

    String[] faces = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    String[] suits = {"Diamonds", "Clubs", "Hearts", "Spades"};
    deckOfCards = new Card_Class[52];
    currentCard = 0;

    final int width = 88;
    final int height = 133;
    final int rows =4;
    final int columns = 13;

    BufferedImage bigImage = ImageIO.read(new File("AllCards.png"));
    BufferedImage tempCardImage;

    for(int suit=0;suit <4; suit++){

        for(int face=0;face<13;face++){
            tempCardImage = bigImage.getSubimage(
                    face*width+(face*10)+8,
                    suit*height+(suit*10)+38,
                    width,
                    height);
            deckOfCards[(face+(suit*13))] = new Card_Class(suits[suit],faces[face],(13*suit+face),tempCardImage);
        }
    }     
   }
4
  • What is your question? Commented Nov 22, 2017 at 14:59
  • how do I call on a card based on its integer value? Commented Nov 22, 2017 at 15:00
  • 1
    You can change the deckOfCards from an array to a HashMap, where the card# is the key value. Commented Nov 22, 2017 at 15:04
  • @JuanCarlosMendoza I don't think that will work because each index of the array is an object. Also the card with integer value 12 doesn't correspond with the twelfth entry in the array once it has been shuffled. Commented Nov 22, 2017 at 15:08

1 Answer 1

1

You can do something like this:

 private Card_Class find(int number){
    Predicate<Card_Class> p1 = c -> c.value == number;
    return Arrays.stream(deckOfCards).anyMatch(p1);
 }

Your predicate is the matching criteria, in this case being the value matching the given number.

Sign up to request clarification or add additional context in comments.

8 Comments

I'm not familiar with Predicate. I will do some research and see if this can solve my problem.
Predicate was introduced in Java 8, should work for you ;)
Thanks @Stefan ! Do you know if this will work in a GUI where the user clicks the image of a card and the program finds the integer value of that card?
Depends on your implementation. What data do you have when clicking the card?
I haven't written the code for it yet, but the plan is to remake a version of a solitaire game where the user clicks on a card and the program will look at the integer value of that card and determine if that value is one less than the integer value of a second card.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.