데크 셔플 링 메서드를 만들려고 노력 중입니다. Stack Overflow에 대한 답변을 검색했지만 알아 내지 못했습니다.배열에 루핑 셔플 메서드를 만드는 방법
셔플 방법을 만들고 싶습니다. 이 메서드는 배열에서 0과 갑판 크기 사이의 임의의 두 숫자를 선택합니다. 그런 다음 두 숫자를 매개 변수로 swap() 메서드에 전달해야합니다. 스왑 메서드가 TIMES_TO_SHUFFLE 번 호출되도록 루프를 만들 싶습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?
이것은 데크 클래스입니다.
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;
}
}
을 [Collections.shuffle()] (http://download.oracle.com/javase/6/docs/api/java/ 적용 util/Collections.html # shuffle % 28java.util.List, % 20java.util.Random % 29)를 갑판에 추가하십시오. – Basilevs