在Java中切换纸牌游戏的方法?

时间:2016-11-15 23:01:50

标签: java arrays object arraylist methods

我的计算机科学课程有一个涉及制作纸牌游戏的项目。这是卡片的基本外观。

public Card(int value, int suit) {
    if (value < 1 || value > 9) {
        throw new RuntimeException("Illegal card value attempted.  The " +
                "acceptible range is 1 to 9.  You tried " + value);
    }
    if (suit < 0 || suit > 4) {
        throw new RuntimeException("Illegal suit attempted.  The  " + 
                "acceptible range is 0 to 4.  You tried " + suit);
    }
    this.suit = suit;
    this.value = value;
}

/** 
 * "Getter" for value of Card.
 * @return value of card 
 */
public int getValue() {
    return value;
}

/**==
 * "Getter" for suit of Card.

public int getSuit() {
    return suit;
}

我遇到的问题是切割方法。使用cut方法,用户基本上在ArrayList中选择他们希望列表开始的位置。那个位置前面的任何东西都移到了arraylist的后面。因此,如果ArrayList的值为[1 2 3 4 5]并且用户选择位置3(将为4),则该方法将返回[4 5 1 2 3]的新ArrayList。这就是我的代码中的内容。

private ArrayList<Card> cards;

public void cut(int position) {
    Deck tophalf = new Deck();
    tophalf.cards = new ArrayList<Card>();
    Deck bottomhalf = new Deck();
    bottomhalf.cards = new ArrayList<Card>();
    Deck cutted = new Deck();
    cutted.cards = new ArrayList<Card>();
    for (int i=0; i<position; i++){
        bottomhalf.cards.add(cards.get(i));
    }
    for (int i=position; i<this.getNumCards(); i++){
        tophalf.cards.add(cards.get(i));
    }
    for (int i=0; i<this.getNumCards()-(position+1); i++){
        cutted.cards.add(tophalf.getCardAt(i));
    }
    int pos=0;
    for (int i=this.getNumCards()-(position+1); i<this.getNumCards(); i++){

        cutted.cards.add(bottomhalf.getCardAt(pos));
        pos++;
    }
    this.cards=cutted.cards;
}

每次运行代码时都会出错,而且它与pos变量有关。我不确定具体的错误是什么。

0 个答案:

没有答案