首先,我有一个牌号为2-14的牌组,或者Card []对象中的[0] - [12],以及钻石[13]的值(等级)2-14等等 - [25],Hearts [26] - [38],黑桃[39] - [51]。
然后,我必须在shuffle()类中使用Math.random(),例如...
,将这些卡用给定值洗牌。//Holds an array of card objects
private Card [] cards = new Card [52];
//Holds number of cards remaining in deck
private int count;
public Deck()
{
for (int i = 0; i <= 12; i++) {
cards[i] = new Card(i+2, 'C');
count = 52-13;
}
for (int i = 13; i <= 25; i++) {
cards[i] = new Card(i-13+2, 'D');
count = 39-13;
}
for (int i = 26; i <= 38; i++) {
cards[i] = new Card(i-26+2, 'H');
count = 26-13;
}
for (int i = 39; i <= 51; i++) {
cards[i] = new Card(i-39+2, 'S');
count = 13-13;
}
}
public void shuffle()
{
for (int i = 0; i <= 51; i++)
if (i <= 12)
cards[i] = new Card((int)(Math.random() * 52), 'C');
else if (i <= 25)
cards[i] = new Card((int)(Math.random() * 52), 'D');
else if (i <= 38)
cards[i] = new Card((int)(Math.random() * 52), 'H');
else if (i <= 51)
cards[i] = new Card((int)(Math.random() * 52), 'S');
}
这会将数字2-14的值更改为0-51之间的随机整数,我唯一不理解的部分就是如何取出替换我的值2-14的随机整数并将其设置为如果(例如)卡[14] = 35那么用卡[35]交换卡[14]。我没有使用Collections或ArrayList的经验。