2

I was developing a simple application that It is simple game of cards, and I had created an array which contains all the card of the game but the problem is that I don't know why I can't shuffle this array?

I tried to use the RANDOM but I didn't succeed.

public class Mazzo {
    private Carta[] carteNelMazzo ;

    public Mazzo(){// create the deck
        carteNelMazzo = creaMazzo();
        mescolaMazzo(carteNelMazzo);
    }

   /**methods of the deck */

    public Carta PescaCarta (){

        return (carteNelMazzo==null||(carteNelMazzo.length>0)) ? pescaCarta(carteNelMazzo) : null;
    }

    public Carta pescaBriscola(){
        return (carteNelMazzo==null||(carteNelMazzo.length>0)) ? carteNelMazzo[carteNelMazzo.length-1] : null;
    }
    /**
     * @param carte deve avere lunghezza maggiore uguale ad 1
     * @return la prima carta del mazzo
     */
    private Carta pescaCarta(Carta[] carte){
        Carta[] nuoveCarte=new Carta[carte.length-1];
        Carta pescata= carte[0];
        System.arraycopy(carte,1,nuoveCarte,0,carte.length);
        carte = nuoveCarte;
        return pescata;
    }

    private Carta[] creaMazzo(){
        ArrayList<Carta> nuovoMazzo=new ArrayList<>();
        for(int i =0; i<4; i++){
            // selezione del seme

            for(int j = 0;j<10;j++){
                // creation of the card from another calss
                Carta nuovaCarta= new Carta(Carta.SEME.values()[i],j);
                nuovoMazzo.add(nuovaCarta);
            }
        }
        return (Carta[]) nuovoMazzo.toArray();
    }

//shuffle deck

    private void mescolaMazzo(Carta[] carte){

       Random rand = new Random();
       int elements = (int) (40 * Math.random());

       int elements = carteNelMazzo.length;
    }
}

At the end I want this array with all the cards remix at random.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
serf
  • 49
  • 8
  • 2
    This question is not related to android-studio. – Samuel Philipp Apr 16 '19 at 18:25
  • Use the [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) from Knuth, The Art of Computer Programming, Volume 2, *Seminumerical Algorithms*. – David Conrad Apr 16 '19 at 18:28
  • Excuse me why not? – serf Apr 16 '19 at 18:29
  • If you overcome your obsession for `arrays` you could use the `shuffle` method from the `Collecions` class. – Timothy Truckle Apr 16 '19 at 18:29
  • Can I have some code pls? – serf Apr 16 '19 at 18:30
  • Some unrelated hints: don't mix List/ArrayList and arrays. Either stay with arrays, or just use collection classes like List/ArrayList. You are learning stuff, so focus one one concept. Then: dont ask people to drop you code. Learning programming is about doing things yourself. Yes, sometimes that takes hours. Reading other people's code ... gets you to a solution faster. But it slows down your learning. – GhostCat Apr 16 '19 at 18:33

4 Answers4

5

This here:

int elements = (int) (40 * Math.random());
int elements = carteNelMazzo.length;

doesn't shuffle anything. It assigns a random number to a local variable which is then thrown away. ( I actually think this shouldn't even compile, as you declare the same local variable twice in the same scope )

What you need instead: to create a function that maps your 40 indexes to new values, like:

0, 1, 3, ... 39 

becomes

14, 2, 7, ...

An easy way to get there: Collections.shuffle(someList);.

For more ideas (also using arrays directly), see here.

But as you are probably doing this to learn things, I suggest you carefully digest what I told you upfront. Start by thinking how you would could shuffle a list of cards "manually" (not by touching them, but when you are told the order they have how you could "mentally" re-order them). From there, think how you could instruct a computer to do that.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
2

use:

Collections.shuffle(<<arrayname>>);
Nikhil S
  • 3,786
  • 4
  • 18
  • 32
1

You can try something like this:

private void shuffleCards (Card[] cards) {

    for (int i = 0; i < cards.length; i++) {
        Card temp = cards[i];

        //random index of the array
        int rnd = Math.floor(Math.random() * cards.length);
        cards[i] = cards[rnd];
        cards[rnd] = temp;
    }
}

PS.: if this code throws an ArrayIndexOutOfBoundsException change the line int rnd = Math.floor(Math.random() * cards.length); to int rnd = Math.floor(Math.random() * (cards.length - 1));

Henrique Sabino
  • 546
  • 3
  • 19
0

Here's one I learned many years ago.

  int[] cards = IntStream.rangeClosed(0, 51).toArray();
  Random r = new Random();

  for (int i = cards.length - 1; i >= 0; i--) {
     int idx = r.nextInt(i + 1);
     int card = cards[idx];
     cards[idx] = cards[i];
     cards[i] = card;
  }

And then there's always the Collections.shuffle() class.

WJS
  • 36,363
  • 4
  • 24
  • 39