Bridge game shuffle simulation

10 ビュー (過去 30 日間)
Valentin Spanu
Valentin Spanu 2017 年 10 月 20 日
編集済み: Guillaume 2017 年 10 月 20 日
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,

回答 (1 件)

Guillaume
Guillaume 2017 年 10 月 20 日
編集済み: Guillaume 2017 年 10 月 20 日
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(:)];

カテゴリ

Help Center および File ExchangeCard games についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by