2014-11-22 41 views
0

我被困在嘗試製作甲板洗牌方法。我搜索堆棧溢出的答案,但一直沒能弄明白。如何在數組中創建循環洗牌方法

我想創建一個shuffle方法。該方法將從數組中選取兩個隨機數字,數字介於0和甲板大小之間。然後我希望這兩個數字作爲參數傳遞給我的swap()方法。我想創建一個循環,以便交換方法被稱爲TIMES_TO_SHUFFLE次。做這個的最好方式是什麼?

這是Deck類。

import java.util.ArrayList; 

/** 
* Deck of cards. 
* @author Stefan 
* @version 2014.11.19 
*/ 
public class Deck { 
    private ArrayList<Card> deck; 

    public static final int TIMES_TO_SHUFFLE = 10; 

    /** 
    * Constructor for objects of class Deck 
    * Creates a new container for Card objects 
    */ 
    public Deck() { 
     deck = new ArrayList<Card>();  
    } 

    /** 
    * Swap two cards that the user chooses. 
    */ 
    public void swap(int indexA, int indexB) { 
     Card temp = deck.get (indexA); 
     deck.set(indexA, deck.get (indexB)); 
     deck.set(indexB, temp); 
    } 

    /** 
    * Shuffles two cards by passing parameters to the swapCards method 
    */ 
    private void shuffle() { 

    } 

    /** 
    * Add a card to the deck. 
    * @param Card to be added 
    */ 
    public void addCard(Card cardToAdd) { 
     deck.add(cardToAdd); 
    } 

    /** 
    * Take the first card from the deck. 
    * @return Card or null 
    */ 
    public Card takeCard() { 
     if(deck.isEmpty()) { 
      return null; 
     } else { 
     // get the top card 
      return deck.remove(0); 
     } 
    } 

    /** 
    * Show the contents of the deck. 
    */ 
    public void showDeck() { 
     for(Card eachCard : deck) { 
      System.out.println(eachCard.getDescription()+ 
      " of " + eachCard.getSuit()); 
     } 
    } 
} 

這是卡類。

/** 
* Card class - a typical playing card. 
* 
* @author Stefan 
* @version 2014.11.18 
*/ 
public class Card { 
    private String suit; 
    private int value; 
    private String description; 

    /** 
    * @default constructor 
    */ 
    public Card(){ 
    } 

    /** 
    * Constructor for objects of class Card 
    * @param suit e.g. "Hearts" 
    * @param value e.g. 10 
    * @param description e.g. "Ten" 
    */ 
    public Card(String description, String suit, int value) { 
     this.suit   = suit; 
     this.value   = value; 
     this.description = description; 
    } 

    /** 
    * @return the suit 
    */ 
    public String getSuit() { 
     return suit; 
    } 

    /** 
    * @param suit the suit to set 
    */ 
    public void setSuit(String suit) { 
     this.suit = suit; 
    } 

    /** 
    * @return the value 
    */ 
    public int getValue() { 
     return value; 
    } 

    /** 
    * @param value the value to set 
    */ 
    public void setValue(int value) { 
     this.value = value; 
    } 

    /** 
    * @return the description 
    */ 
    public String getDescription() { 
     return description; 
    } 

    /** 
    * @param description the description to set 
    */ 
    public void setDescription(String description) { 
     this.description = description; 
    } 

} 
+2

應用[Collections.shuffle()](http://download.oracle.com/javase/6/docs/api/java/ util/Collections.html#shuffle%28java.util.List,%20java.util.Random%29)。 – Basilevs 2014-11-22 05:29:00

回答

0

混洗方法將看起來像這樣:

private void shuffle() { 
     Deck ob = new Deck(); 
     int x = deck.size() - 1; 
     for(int i = 0; i < TIMES_TO_SHUFFLE + 1; i ++){ 
     int r1 = (int) Math.floor(Math.random() * x); 
     int r2 = (int) Math.floor(Math.random() * x); 
     ob.swap(r1, r2); 
} 
    } 
+0

謝謝你的幫助! – 2014-11-22 06:52:46

+0

如何接受我的回答 – rert588 2014-11-24 08:24:50

+0

我接受了你的回答。我對這個網站相當陌生,所以我不知道所有的程序。 – 2014-11-26 06:34:07