我正在尝试编写一些代码来创建两个数组,一个用套装,一个用值,一副牌。然后它随机地随机洗牌并打印出四个玩家中的每个玩家的牌。如果有人能给我一些关于如何从我拥有的东西的想法,我将不胜感激。我遇到的主要问题是如何切换阵列(shuffle),然后如何显示玩家得到的东西(交易)。
public class BridgeHands
{
private card[] deck;
private int cardsUsed;
public static void dealHands()
{
final int SUITS = 4; // number of suits in a standard deck
final int CARDS_PER_SUIT = 13; // number of cards in each suit
final int CARDS = SUITS*CARDS_PER_SUIT; // number of cards in a standard deck
deck = new Card[CARDS];
int cardCt = 0;
for(int suit = 0; suit < SUITS; suit++)
{
for(int value = 1; value < CARDS_PER_SUIT; value++)
{
deck[cardCt] = new Card(value,suit);
cardCt++;
}
cards used = 0;
}
}
public void shuffle()
{
for(int i = deck.length-1; i > 0; i--)
{
int rand = (int)(Math.random()*(i+1));
Card temp = deck[i];
deck[i] = deck[rand];
deck [rand] = temp;
}
cards used = 0;
}
public int cardsLeft()
{
int cardsUsed = 0;
return deck.length - cardsUsed;
}
public Card dealCard()
{
if(cardsUsed == deck.length)
throw new IllegalStateException("No cards are left in the deck.");
cardsUsed++;
return deck[cardsUsed - 1];
}
final int HANDS = 4;
final int CARDS_PER_HAND = CARDS/HANDS;
public static String cardName(int suit, int value)
{
final String[] CARD_NAMES =
{"not used", "ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};
final String[] SUIT_NAMES =
{"spades", "clubs", "diamonds", "hearts"};
return CARD_NAMES[value] + " of " + SUIT_NAMES[suit];
}
}
答案 0 :(得分:-1)
int cards = 52;
int card1, card2
while (cards > 1)
for( int k=0; k<100; k++ )
{
card1 = rand.nextInt(52);
card2 = rand.nextInt(52);
int keepSuit = dk[card1];
dk[card1] = dk[card2];
dk[card2] = keepSuit;
}
也许这会有帮助吗?不确定它是否正确,抱歉。