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;
}
}
}