无法从卡片对象的“卡座”中检索随机对象

时间:2016-01-17 02:00:49

标签: oop random

我的问题是这样的:当我从牌组中抽取一张随机牌时我已经实例化了回报有时是正确的(即KING_CLUBS),但有时我会得到一个奇怪的(即SEVEN_)。这很奇怪,因为当甲板被实例化时,我在Deck构造函数中添加了一个打印行语句,以查看是否正确添加了ID。每次都没有失败,卡ID是正确的。

Blackjack的hitUser()方法中的println用于检查正在绘制的卡的ID。有时它是正确的,有时则不是。谁能告诉我发生了什么以及为什么?

下面的可运行相关代码

我的卡类:

import java.util.*;
import javax.swing.*;
import java.awt.*;

public class Card {
    //cardValues[] represents the tangible values attached to the card faces(ACE, ONE, TWO, THREE, ..., KING)
    private final static int cardValues[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
    private final static String cardIDs[] = {"ACE_", "TWO_", "THREE_", "FOUR_", "FIVE_", "SIX_", "SEVEN_", "EIGHT_", "NINE_", "TEN_", "JACK_", "QUEEN_", "KING_"};
    private int value;

    //Name of the card, i.e. "ACE";
    private String cardID;

    /**
     * Constructor
     * @param v - card value(I.e. 1 for Ace or 13 for King)
     */
    public Card(int v){
        setValue(v);
        setCardID(v);
    }

    /**
     * Constructor
     * @param v - card value(I.e. 1 for Ace or 13 for King)
     * @param id - a manually set ID for the card(used for 'illegal' card instantiation)
     */
    public Card(int v, String id){
        setValue(v);
        setCardID(id);
    }

    /**
     * Returns the card ID
     * @return - the card ID of the respective card
     */
    public String getCardID(){
        return cardID;
    }

    /**
     * Returns the card value
     * @return - the number value of the respective card
     */
    public int getValue(){
        return value;
    }

    /**
     * Setter method for card value
     * @param v - value
     */
    public void setValue(int v){
        //Checks to see if v is a valid cardValue
        if(v >= 1 && v <= 13){
            value = v;
        }
    }

    /**
     * 'legal' setter method for card ID
     * @param v - number value of the card
     */
    public void setCardID(int v){
        //Checks to see if v is a valid cardValue
        if(v >= 1 && v <= 13){
            cardID = cardIDs[v - 1];
        }
    }

    /**
     * 'illegal' setter method for card ID
     * @param id - String value of the card ID
     */
    public void setCardID(String id){
        cardID = id;
    }

    public String getIcon(){
        return "blackjack/" + this.getCardID() + ".png";
    }

我的甲板课程:

import java.util.*;
import javax.swing.*;
import java.awt.*;

public class Deck {
    private int numCards;
    private String cardSuits[] = {"SPADES", "HEARTS", "DIAMONDS", "CLUBS"};
    private static ArrayList<Card> cards = new ArrayList<Card>();
    public static JLabel[] cardIcons = new JLabel[52];
    public static int dealerHand, playerHand;



     /**
     * Constructor
     */
    public Deck(){

        //Will keep track of the index of the 52 cards created
        int counter = 0;

        for(int y = 0; y < 4; y++){
            String suit = cardSuits[y];
                for(int z = 1; z < 14; z++){
                    //Adds a new card and initializes it with its number value
                    cards.add(new Card(z));

                    //Replaces the card ID with a new ID containing the card suit (i.e. SPADES)
                    if(cards.get(counter).getCardID().indexOf("_") == cards.get(counter).getCardID().length() - 1){
                        String newID = cards.get(counter).getCardID() + suit;
                        cards.get(counter).setCardID(newID);
                        System.out.println(newID);
                        counter++;
                    } else {

                    }
                    }
            }
    }

    /**
     * Removes a card from the deck - gets rid of the object in the array of cards once it has been drawn
     * @param id - the card to be removed
     */
    public void removeCard(String id){
        for(int i = 0; i < 52; i++){

            if(cards.get(i).getCardID().equalsIgnoreCase(id)){
                cards.remove(i);
            }
        }
    }

    /**
     * Returns the object of the card within a deck
     * @param c - the index of the card within the deck
     * @return the card object
     */
    public Card getCard(int c){
        return cards.get(c);
    }

    /**
     * Returns a random card from the array of cards in the deck
     * @return - a random card object
     */
    public Card getRandomCard(){
        Random r = new Random();
        int index = r.nextInt(cards.size());

        return cards.get(index);
    }

    /**
     * Resets the deck to its original, 'perfect' order
     */
    public void reset(){
        for(int x = 0; x<cards.size(); x++){
            cards.remove(x);
        }

        //Will keep track of the index of the 52 cards created
        int counter = 0;

        for(int y = 0; y < 4; y++){
            String suit = cardSuits[y];

            for(int z = 0; z < 13; z++){
                //Adds a new card and initializes it with its number value
                cards.add(new Card(z));

                //Replaces the card ID with a new ID containing the card suit (i.e. SPADES)
                String newID = cards.get(counter).getCardID() + suit;
                cards.get(counter).setCardID(newID);

                counter++;
            }
        }
    }

我的二十一点班:

import javax.swing.*;

import net.miginfocom.swing.MigLayout;

/**
 * This class provides all of the functionality of the blackjack game
 * @author Mohamed Amadou
 *
 */
public class Blackjack extends JFrame implements ActionListener{

    public MigLayout mig = new MigLayout("insets 0");
    public Deck bjDeckHouse = new Deck(), bjDeckUser = new Deck();
    public int dealerOffset = 65, userOffset = 65, houseHand = 0, 
    public final int MAX_BET = 100000;
    public JPanel bj = new JPanel(new MigLayout("insets 0")), blackjack = new JPanel(new MigLayout("insets 0"));
    public JButton hit;

    public Blackjack(){

        buildUI();
        mig.layoutContainer(bj);

        setSize(780, 700);
        setResizable(false);
        setLayout(mig);
        setTitle("Blackjack");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);

    }

    public void buildUI(){
        getContentPane().add(bj);
        bj.setBackgroundColor(Color.BLACK);
        hit = new JButton("Hit");
        hit.addActionListener(this);
        bj.add(hit, "pos 570px 285px");


    }

    public void hitUser(int hand){
        JPanel test = new JPanel(new MigLayout("insets 0"));
        JFrame testFrame = new JFrame();
        testFrame.add(test);

        Card hitCard = bjDeckUser.getRandomCard();
        hand += hitCard.getValue();

        String position = "pos "+ userOffset + "px" + " 580px";



        System.out.println(hitCard.getCardID());
        bjDeckUser.removeCard(hitCard.getCardID());
        turns++;
        checkBust(hand);

        testFrame.setSize(400, 400);
        testFrame.setResizable(false);
        testFrame.setLayout(mig);
        testFrame.setTitle("Blackjack Rules");
        testFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        testFrame.setVisible(true);
    }
public void actionPerformed(ActionEvent e) {

        if(e.getSource() == hit){
            hitUser(userHand);
        }

    }

1 个答案:

答案 0 :(得分:0)

您的问题在于reset()

    for(int x = 0; x<cards.size(); x++){
        cards.remove(x);
    }

这实际上并不会删除所有项目(只是播放它),您应该使用cards.clear()。由于牌组不是空的,而你又添加了52张牌,牌组将不正确。此外,您将获得奇怪的名称,可能还有SEVEN_CLUBS_SPADES和类似名称。

此外,您的代码有点令人困惑。套房应该是卡的属性,而不是卡的属性。卡应该是不可变的(根据使用情况,可能是枚举)。你有2个初始化代码片段(你应该有1)。您的&#34;商业模式中的GUI代码&#34;。只需要清理一些想法:)