Java 洗牌卡组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24520782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java shuffle card deck
提问by kingofkenya
I am having problem getting my code to shuffle the deck. I think i have the syntax for collections.shuffle wrong. The code I currently have is not shuffling the deck. Below please find the deck class and the card class. The deck class holds the collections.shuffle method.
我在让我的代码洗牌时遇到问题。我想我对 collections.shuffle 的语法有误。我目前拥有的代码没有洗牌。请在下面找到甲板类和卡片类。甲板类拥有 collections.shuffle 方法。
Deck class
甲板类
public class Deck {
private Card[] deck = new Card[52];
private int topCard;
Deck() {
topCard = 0;
for (int i = 0; i < deck.length; i++)
deck[i] = new Card(i);
}
public void shuffle() {
topCard = 0;
Collections.shuffle(Arrays.asList(deck));
}
public Card dealCard() {
Card theCard;
if (topCard < deck.length) {
theCard = deck[topCard];
topCard++;
}
else
theCard = null;
return theCard;
}
}
Card Class
卡片类
public class Card {
private int cardNum;
final static String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
final static String[] ranks = {"Ace", "2", "3","4","5","6","7","8", "9","10", "Hyman", "Queen", "King"};
Card (int theCard) {
setCardNum (theCard);
}
public void setCardNum (int theCard) {
cardNum = (theCard >= 0 && theCard <= 52)? theCard: 0;
}
public int getCardNum() {
return cardNum;
}
public String toString() {
return ranks[cardNum%13] + " of " + suits[cardNum/13];
}
public String getSuit() {
return suits[cardNum/13];
}
public String getRank() {
return ranks[cardNum%13];
}
public int getValue() {
return cardNum%13;
}
}
回答by Varun R
Here is a sample code for a similar question where we were asked not to use any built-in function to shuffle the deck but was allowed to use Math.random().
这是一个类似问题的示例代码,我们被要求不要使用任何内置函数来洗牌,但允许使用 Math.random()。
import java.util.*;
class Card {
private String rank;
private String suit;
public Card(String r,String s) {
rank=r;
suit=s;
}
public String getRank() {
return rank;
}
public String getSuit() {
return suit;
}
public String toString() {
return rank+" of "+suit;
}
}
class Deck {
private ArrayList<Card> deck;
private String[] ranks ={"ACE","2","3","4","5","6","7","8","9","10","Hyman","QUEEN","KING"};
private String[] suits ={"SPADE","HEART","CLUB","DIAMOND"};
public Deck() {
deck = new ArrayList<Card>();
for(int i=0;i<suits.length;i++) {
for(int j=0;j<ranks.length;j++) {
deck.add(new Card(ranks[j],suits[i]));
}
}
}
public void showCards() {
System.out.println("\n\n Showing Cards !!!");
int i=1;
for(Card c:deck) {
System.out.println("Card "+(i++)+" : "+c);
}
}
public void shuffle() {
ArrayList<Card> temp = new ArrayList<Card>();
while(!deck.isEmpty()) {
int loc=(int)(Math.random()*deck.size());
temp.add(deck.get(loc));
deck.remove(loc);
}
deck=temp;
}
}
public class Game {
public static void main(String[] args) {
Deck myDeck = new Deck();
myDeck.showCards();
myDeck.shuffle();
myDeck.showCards();
}
}