Let's start, 3 classes (Card,Start,Deckofcards)
1º Card:
public class Card {
private String type;
private int value;
private String number;
public Card(String number,String type,int value) {
this.type=type;
this.value=value;
this.number=number;
}
public String gettype() {
return type;
}
public String getNumber() {
return number;
}
public int getValue() {
return value;
}
@Override
public String toString() {
return getNumber() + " de " + gettype() + " --> VALUE " + getValue() ;
}
}
2º Start
public class Start {
public static void main(String[] args) {
Deckofcards deckofcards = new Deckofcards();
Card c = deckofcards.newCard();
String s =c.toString();
System.out.println(s);
Card c2 = deckofcards.newCard();
String s2 =c2.toString();
System.out.println(s2);
}
}
Then, the problem is here
Deckofcards:
public class Deckofcards {
private Card CardsR[];
private final Card[] Cards = {
new Card("A","heart",1),
new Card("2","heart",2),
new Card("3","heart",3),
new Card("4","heart",4),
new Card("5","heart",5),
new Card("6","heart",6),
new Card("7","heart",7),
new Card("8","heart",8),
new Card("9","heart",9),
new Card("10","heart",10),
new Card("J","heart",10),
new Card("Q","heart",10),
new Card("K","heart",10),
new Card("As","Diamond",1),
new Card("2","Diamond",2),
new Card("3","Diamond",3),
new Card("4","Diamond",4),
new Card("5","Diamond",5),
new Card("6","Diamond",6),
new Card("7","Diamond",7),
new Card("8","Diamond",8),
new Card("9","Diamond",9),
new Card("10","Diamond",10),
new Card("J","Diamond",10),
new Card("Q","Diamond",10),
new Card("K","Diamond",10),
new Card("A","clover",1),
new Card("2","clover",2),
new Card("3","clover",3),
new Card("4","clover",4),
new Card("5","clover",5),
new Card("6","clover",6),
new Card("7","clover",7),
new Card("8","clover",8),
new Card("9","clover",9),
new Card("10","clover",10),
new Card("J","clover",10),
new Card("Q","clover",10),
new Card("K","clover",10),
new Card("A","Spades",1),
new Card("2","Spades",2),
new Card("3","Spades",3),
new Card("4","Spades",4),
new Card("5","Spades",5),
new Card("6","Spades",6),
new Card("7","Spades",7),
new Card("8","Spades",8),
new Card("9","Spades",9),
new Card("10","Spades",10),
new Card("J","Spades",10),
new Card("Q","Spades",10),
new Card("K","Spades",10),
};
//Sorry if the translation its not correct, this is only a little part of a
//big code.
public Deckofcards() {
Collections.shuffle(Arrays.asList(Cards)); //SHUFFLE
CardsR=Cards.clone(); //COPY
}
public Card newCard(){
boolean while1 = true;
Card take=null;
for(int i = 0; i < CardsR.length; i++){
while(while1){
if(CardsR[i]!=null){
take=Cards[i];
//CardsR[i]=null;
while1=false;
}
}
}
return take;
}
}
Let's explain.
I have an array of cards, that i mix. OK. ALL CORRECT
Then i call to recive a card (Class: start). OK. ALL CORRECT
--- THEN --- when i call another time, the card returns the same value... I tried to set a null... but then it starts a infinite loop by no reason?.
Any possible solution? Thanks