简体   繁体   English

Java随机播放卡座

[英]Java shuffle card deck

I am having problem getting my code to shuffle the deck. 我在获取我的代码来洗牌时遇到问题。 I think i have the syntax for collections.shuffle wrong. 我认为我具有collections.shuffle的语法错误。 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方法。

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", "Jack", "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;
    } 
}

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","JACK","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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM