Bridge game shuffle simulation

2 views (last 30 days)
Valentin Spanu
Valentin Spanu on 20 Oct 2017
Edited: Guillaume on 20 Oct 2017
I need to write a code that simulate bridge game. I want to do it generating the deck using characters and i want to process it using characters. I have generated the deck like this:
c = ['23456789TJQKA']';
s = ['CDHS']';
d = [repmat(c,4,1),repmat(s,13,1)];
I need help shuffling the deck in 4 even hands, and than arranging each hand by card rank,

Answers (1)

Guillaume
Guillaume on 20 Oct 2017
Edited: Guillaume on 20 Oct 2017
Shuffling can easily be achieved by using a random permutation of the rows generated with randperm. Sorting can easily be achieved by using sortrows. If you want to sort first by suit then by rank specify [2 1] for the column argument. The only difficulty is that you're using letters for the card values and suits and that the order of these letters does not reflect the actual card value. The answer is simple, use the card value directly and replace these with letters at the end only for display.
Two notes:
The brackets in [''] are completely unnecessary. c = '23456789TJQKA'; does the same with less character and less time (since the brackets cause matlab to call the horzcat function)
The construction of your deck works because 13 and 4 are coprime. The proper way to construct the cartesian product of two set is to use ndgrid:
[cards, suits] = ndgrid('23456789YJQKA', 'CDHS'); %but as said, to make sorting easier I'd use ndgrid(1:13, 1:4)
deck = [cards(:), suits(:)];

Categories

Find more on Card games in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!