Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffling a 2D array in Java

I'm trying to shuffle a 2D object array in Java. I thought Collections.shuffle would do the trick, but it looks like it only shuffles the objects in each row, but does not mix rows together, which I wanted it to do. Any built in methods or easy to implement methods that can shuffle a 2D array for me? The array is cards[13][4].

like image 242
Snowman Avatar asked May 04 '26 17:05

Snowman


1 Answers

It looks like you want to shuffle a deck of cards.

In real world games, we first shuffle the deck, then distribute the cards to the player. You try to change the order of the sequence: you want to distribute a sorted deck to to the players and ask them to exchange cards until all cards are shuffled ;-)

As Stephen C suggested: collect the cards from the players, shuffle the deck and distribute again.

like image 146
Andreas Dolk Avatar answered May 07 '26 06:05

Andreas Dolk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!