2014-09-20 29 views
0

我正在嘗試編寫一些代碼來創建兩個數組,其中一個具有套裝,一個具有值,一副紙牌。然後隨機洗牌,並打印出四個玩家中每一個玩家的牌。如果任何人都可以給我一些關於如何從我所擁有的東西開始的想法,我會很感激。我遇到的主要問題是如何切換陣列(隨機播放)以及如何顯示玩家獲得(處理)的內容。使用數組處理來自一組卡片的Java卡片中的四個隨機手牌

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

改組數組並不難,請參閱[this](http://stackoverflow.com/a/3732080/3755692)回答。你是什​​麼意思_「如何顯示玩家得到(處理)」_? – msrd0 2014-09-20 09:42:01

+0

而你錯過了';'這一行:'count = count - 1'。順便說一句,我會用'count - '替換這個# – msrd0 2014-09-20 09:43:46

+0

IIWY我會創建'Card'和'Deck'類和'SUITE'和'VALUE'枚舉。然後,一切都在一起;在洗牌時不需要跟蹤一堆東西。 – ChiefTwoPencils 2014-09-20 09:44:10

回答

-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; 
     } 

也許這有幫助嗎?不知道是否正確,但是,對不起。

+0

它給了我一個線索,但我不認爲我會讓我的工作。 – civitas 2014-09-21 01:29:25