4

For an exercise in a poker game:
I create an initial array of objects that contains 7 cards. Each card is designated by its value (7, 8, 9, 10, jack...) and its suit (club, diamond...).
I create a second array that counts the number of cards of the same value. For example, if I have three cards of value 7, then in my second table, an integer of value 3 will fit in the box for cards of value 7.

My question:
In a third array Card[] cards = new Card[5], how can I copy these 3 cards of value 7 to the beginning of this one? I'm not looking for the solution but an idea.

The first array:

Card[] sevencards = new Card[7];

The following method counts the number of cards of the same value:

private int[] nbCardsvalue(Card[] sevencards){
        int[] nbvalue = new int[13];
        for (int i = 0; i < sevencards).length; i++){
            if (sevencards[i].getValue() == 2){
                nbvalue [0]++;
            }else if (sevencards[i].getValue() == 3){
                nbvalue [1]++;
            }else if (sevencards[i].getValue() == 4){
                nbvalue [2]++;
            }else if (sevencards[i].getValue() == 5){
                nbvalue [3]++;
            }else if (sevencards[i].getValue() == 6){
                nbvalue [4]++;
            }else if (sevencards[i].getValue() == 7){
                nbvalue [5]++;
            }else if (sevencards[i].getValue() == 8){
                nbvalue [6]++;
            }else if (sevencards[i].getValue() == 9){
                nbvalue [7]++;
            }else if (sevencards[i].getValue() == 10){
                nbvalue [8]++;
            }else if (sevencards[i].getValue() == 11){
                nbvalue [9]++;  
            }else if (sevencards[i].getValue() == 12){
                nbvalue [10]++; 
            }else if (sevencards[i].getValue() == 13){
                nbvalue [11]++;
            }else if (sevencards[i].getValue() == 14){
                nbvalue [12]++;
            }else{
                return nbvalue ;
            }
        }
        return nbvalue ;
    }

To clarify:

  • my first array contains 7 poker cards,
  • my second array counts the number of cards of the same value,
  • the third array, of size 5, contains the five best cards of my first array or the best combination to be able to compare the strength of the hands of the players of the poker game.

For example:
If in my first array sevencards[], I have 3 cards of value 7 (i.e. a three of a kind).
In my second array nbvalue[], I will have nbvalue[5] = 3, which indicates that I have 3 cards of value 7.

I need a third array (e.g. Main[]) where I will put my combination, i.e. three of a kind. My three 7s at the beginning of this array.
I can't figure out how to get those 3 cards of value 7 and put them in my Main[] array.

This is my class Card:

public class Card implements Comparable<Card>{
    private final Suit suit
    public final int Jack = 11;
    public final int Queen = 12;
    public final int King = 13;
    public final int Ace = 14;
    private final int value;

    public Card (Suit s, int v){
        this.suit = s;
        this.value = v;
    }
    public Couleur getSuit(){
        return suit
    }
    public int getValue(){
        return value;
    }
    public String toString(){
        String v = String.valueOf(value);
        String s = suit.toString();
        if (value >= 2 && value <= 10){
            return v + " de " + s;        
        }else if (value == 11){
            return "Jack of " + s;
        }else if (value == 12){
            return "Queen of " + s;
        }else if (value == 13){
            return "King of " + s;  
        }else if (value == 14){
            return "Ace of " + s;
        }
        return null;
    }
    public int compareTo(Card c2){
        if (this.value > c2.value){
            return 1;
        }else if (this.value < c2.value){
            return -1;
        }else {
            return 0;
        }
    }
}
Community
  • 1
  • 1
Matt
  • 41
  • 2
  • 1
    note the logic `nbvalue[sevencards[i].getValue() - 2]++` for ALL the if, just use one if to verify the value is between 2 and 14 then apply that. it'll simplify that code – azro Dec 31 '21 at 11:51

1 Answers1

2

You ask "How can I copy these 3 cards of value 7 to the beginning of [a third array]", but you don't elaborate on what "copy" means in the terms of your logic. More specifically, in your code (in the state that you are describing) you have one instance (I am assuming) of a Card that returns the value 7 when getValue() is called on it, and you have an integer of value 3 in another array at a specifc position (at index 5), which it seems to mean the amount of cards of that type.

So copying a single reference of a Card three (3) times, would in your code, probably mean just making 3 new instances of the Card class (by calling the Card constructor, i.e. new Card(/* parameters */), with specific parameters, so that the created instances would represent cards with value 7, i.e. that calls to getValue() would return 7 for all 3 newly created Card instances. If you update you post with the code of your Card class, I can elaborate on that a bit more. Right now I don't know what constructors you have available, but you can always add more if needed - you could add a specific constructor, sometimes called a "copy constructor", which would make a new instance of a card of the same kind).

When you make each of the 3 Card instances, assign them to the array cards at desired indices.

Since the amount of cards to create (copy) and place into the cards array might be dynamic (might be less or more than 3 sometimes) you could do this part with a loop and inside the loop you keep creating new Card instances and assigning them to some index in the array. The index variable would be the looping variable as well, starting with 0 and going up to (not including) the amount of cards you would have to add to the beginning of your cards array.

But, depending on your use case, you maybe don't have to copy the card and make multiple instances of it, you could as well just assign the same reference, of the Card instance that you already have (the one of value 7), to multiple places in the cards array, without making "copies". That could achieve a similar result logic-wise (again, disclaimer, depending on your use case).

EDIT: Code of the Card class was provided. Making a new instance (a copy) of a card of the same kind, would go like this

// The card reference I want to copy
Card someCard;
// This reference could be obtained in various ways, but one way would be from the sevencards array like this
someCard = sevencards[i];

// then we make 3 copies (we get 3 new references)
Card cardCopy1 = new Card(someCard.getSuit(), someCard.getValue());
Card cardCopy2 = new Card(someCard.getSuit(), someCard.getValue());
Card cardCopy3 = new Card(someCard.getSuit(), someCard.getValue());

// we assign the references to the other main/cards array, starting at the beginning, so at indices 0, 1 and 2
cards[0] = cardCopy1;
cards[1] = cardCopy2;
cards[2] = cardCopy3;

Of course this sample code always makes 3 copies, to make an arbitrary number of copies the code above would have to be changed into a loop (a for loop or a while loop, up to your preference).

EDIT: Here is how you can do it with some helper methods

Add a public Card clone() method to your Card class (this would not be needed, but since you return your Suit value as a Couleur type and the constructor only accepts a Suit type, we cannot reuse that constructor anyway).

// Add this inside the Card class
public Card clone() {
    return new Card(suit, value);
}

Then in your code create 2 new methods

    public Card findCardWithValue(int targetValue, Card[] array) {
        Card targetCard = null;
        for (Card card : array) {
            if (card.getValue() == targetValue) {
                targetCard = card;
            }
        }
        return targetCard;
    }

    public void copyCardIntoArray(Card card, int times, int startIndex, Card[] array) {
        while (times > 0 && startIndex < array.length) {
            // if you want to just reference to the same card instance you can just do this
            // array[startIndex] = card;
            // if you want to actually make new instances (copies / clones) then do this
            array[startIndex] = card.clone();

            startIndex += 1; // move the index 1 forward
            times -= 1; // reduce times by 1
        }
    }

Then in your code you call both those methods on e after another and pass the desired parameter values. And check the return value for null, just in case if the array does not contain a value with the value specified.

        Card[] cards = new Card[5];

        int targetValue = 7;
        Card card = findCardWithValue(targetValue, sevencards);

        if (card != null) {
            // your nbvalue index is always 2 less than the card value
            int howManyTimesToCopy = nbvalue[card.getValue() - 2];
            copyCardIntoArray(card, howManyTimesToCopy, 0, cards);
        } else {
            // card with specified value was not found in the sevencards array
        }
Ma3x
  • 5,761
  • 2
  • 17
  • 22
  • I edit my initial post. Thanks a lot for your help! – Matt Dec 31 '21 at 13:17
  • I read the clarification, yes, that is how I understood it as well, so the advice above still applies. But you still did not show us how your Card class code and constructors look, so I cannot provide more tips in that area (without writing the code itself). Let me know if you are still stuck and if you would like a code sample for Card instantiation (copy) and assigning to the array. – Ma3x Dec 31 '21 at 13:35
  • I edit my post with my class Card. Thanks in advance ;-) – Matt Dec 31 '21 at 13:48
  • @Matt see my edit. Is the sample code enough to give the idea of what needs to be done? You can now convert it to a loop, to be able to add the correct amount of cards to the array based on the value in your nbvalue array. – Ma3x Dec 31 '21 at 14:09
  • Thanks for your help ;-) But I think I am not clear in what I am looking to do. I need to write / copy the three cards of value 7 from my sevencards[] array into the third Main[] array. But, in your code, I don't know what position the card is in, so I can't set someCard = sevencards[i]. My second array is used as a pivot to know the number of cards of the same value. How can I find my 3 cards of value 7 in sevencards knowing that I don't know their position in sevencards? – Matt Dec 31 '21 at 15:11
  • @Matt You have been clear, I understood exactly what you want to do, but you asked for tips instead of code :) That is why I provided guidance oh how you could approach the problem. But if you want the code, here is the code. Happy New Year :) – Ma3x Dec 31 '21 at 15:59