0

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

Joan
  • 35
  • 7

3 Answers3

1

You're not really doing anything with the shuffled list of cards; the instance is essentially transient and therefore garbage-collected. Note that shuffling the list you created out of the array, does not shuffle the array. Instead, you could do this:

List<Card> cardList = Arrays.asList(Cards);
Collections.shuffle(cardList);

CardsR = cardList.toArray(new Card[cardList.size()]);

Your newCard method doesn't need to be this complicated. I assume you just want to return a card from the shuffled array. One way to do this is to maintain an index of the last card taken; you can initialize it to -1 in your constructor. In newCard, you will increment this index and return the card at that index as long as you are not out of bounds. If you are, you might want to print a message saying there are no cards left (for example).

Some other pointers:

  • Make sure you follow Java naming-conventions; field and variable names should not be capitalized.
  • Consider using a List<Card> of cards instead of an array; that way you can simply shuffle instead of needlessly converting back and forth between an array and a list.
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
1

when i call another time, the card returns the same value

Because

            for(int i = 0; i < CardsR.length; i++){  // i = 0 
                while(while1){                       // true
                if(CardsR[i]!=null){                 // true
                    take=Cards[i];                   //
                    while1=false;                    // so no more while loop execution 
                                                     // first card will always returned

I tried to set a null... but then it starts a infinite loop by no reason?

during first call Card c = deckofcards.newCard(); the CardsR[0] was set to null so during second call Card c2 = deckofcards.newCard(); your if condition if(CardsR[i]!=null) will never be executed and now you are stuck in an infinite while loop

  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;

                    }
                }
            }

Solution : simply you can use Random instance to pick and return random cards from array .

How to randomly pick an element from an array

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0

The value of take will always be the first card in the array.

The while loop never terminates when you set the value equal to null because while2 is only set to false when a non-null entry is found. Once a null is found, then nothing happens and the while loop runs forever because there is no way for while2 to be set to false.

A break statement should do the trick here

public Card newCard(){
        Card take=null;
            for(int i = 0; i < CardsR.length; i++){
                if(CardsR[i]!=null){
                    take=Cards[i];
                    CardsR[i]=null;
                    break;
                }
            }
        }
        return take;
    }

A break statement will exit whatever loop it is in.

Alternatively, you can use a List<Card> instead of an array, with which you can remove entries from your shuffled list and return them, removing the need to set and check for null.

private List<Card> cardList;

public Deckofcards() {
        cardList = Collections.shuffle(Arrays.asList(Cards)); //SHUFFLE
        }

public Card newCard(){
    if(!cardList.isEmpty()){
       return cardList.remove(0);        
    }            
    return null;
}
binskits
  • 248
  • 1
  • 3
  • 10