纸牌游戏将卡片添加到一组"手中" --java

时间:2017-10-13 14:29:39

标签: java

我正在尝试为纸牌游戏制作一个基本大纲,我创建了一个牌组,这是一种用于处理随机牌的方法,但我很难将牌添加到实际牌中。问题出在游戏类中,其中包含 getCard()方法。我不知道这样做的正确方法,因为我的想法不起作用。

创建套牌的类:

import java.util.Random;

public class Deck<E> {

//create a new linked list for Deck
LinkedPositionalList deck = new LinkedPositionalList();

public Deck(){
    for (int i = 0; i < 4; i++){
        for(int j = 2; j< 14; j++){
            Card card = new Card(i,j);
            deck.addLast(card); //add to linked list
        }
    }
}

public Card card(){
    Random rand = new Random();
    int position = rand.nextInt(52);//create random number 0-52
    int counter = 0;
    Iterator<Card> iter = this.deck.iterator();

    while(counter <= position){
        Card card = iter.next();
        if(counter == position){
            iter.remove();
            return card;
        }
        counter++;
    }
    return null;
}
}

创建卡的类:

public class Card<E> {
public final static int CLUBS  = 0,
                        SPADES = 1,
                        DIAMONDS = 2,
                        HEARTS = 3;
private final static String [] suitNames = {"CLUBS", "SPADES", "DIAMONDS", "HEARTS"};

// Special cards
private int JACK_VALUE = 11;
private int QUEEN_VALUE = 12;
private int KING_VALUE = 13;
private int ACE_VALUE = 14;

// The card
private int suit = 0;           // Suit of the card
private int value = 2;          // Value of the card

public int getSuit() {
    return this.suit;
}

public int getValue() {
    return this.value;
}

public Card(int suit, int value ){

    this.suit = suit;
    this.value = value;
}

public String suitToString() {
    return suitNames[ suit ];
}

public String valueToString() {
    if (value == ACE_VALUE) {
        return "ACE";
    } else if (value == JACK_VALUE) {
        return "JACK";
    } else if (value == QUEEN_VALUE) {
        return "QUEEN";
    } else if (value == KING_VALUE) {
        return "KING";
    } else if ( value > 0 ) {
        return String.valueOf(value);
    }
    return "";
}

public String shortValueToString() {
    if (value == ACE_VALUE) {
        return " A";
    } else if (value == JACK_VALUE) {
        return " J";
    } else if (value == QUEEN_VALUE) {
        return " Q";
    } else if (value == KING_VALUE) {
        return " K";
    } else if ( value > 0 ) {
        return String.format("%2d",value);
    }
    return "";
}

public String toString() {
    return valueToString() + " of " + suitToString();
}

public String toShortString() {
    return shortValueToString() + suitToString().substring(0,1);
}

public boolean equalTo(Card c ) {
    if ( c.getSuit() != this.getSuit()) return false;
    if ( c.getValue() != this.getValue()) return false;
    return true;
}
}

创建手的类:

public class CardHand<E> {

LinkedPositionalList<E> hand = new LinkedPositionalList();
//TODO: create method to order hand
}

初始化游戏的类:

public class Game{
    int players;
    int maxCardsInHand;
    int decks;
    CardHand[] hand;
    Card card;
    Deck deck;

//constructor
public Game(int player, int max, int numDecks){
    players = player;
    maxCardsInHand = max;
    decks = numDecks;
    this.hand = new CardHand[player];
    for(int index = 0; index < hand.length; index++){
        this.hand[index] = new CardHand();
    }
}

public void getCard(){
    System.out.println("You got this far...");
    card = deck.card();
    for(int index = 0; index < hand.length;  index++){ //to add to each players hands
        hand[index] = card; //issues with this part
    }
    //TODO: ordered correctly by suit AND value
}

1 个答案:

答案 0 :(得分:1)

鉴于呼叫iter.remove();有效,每次调用card()方法时,您的套牌都会越来越小。但是你假设你可以通过一个完整的套牌进行迭代:

rand.nextInt(52);

相反,只对你剩下的牌进行迭代:

rand.nextInt(this.deck.size());

如果列表对象没有size()或length()方法,则每次调用card()时减少一个计数器:

rand.nextInt(cardsInDeck++);