使用相等

时间:2015-05-06 23:06:51

标签: java arraylist

如果2个值彼此相等,我正试图从一副牌中取出一张牌。相等是检测2个值是否匹配(在代码中解释)但我无法弄清楚如果匹配,如何删除数组中的倒数第二张卡。

    private void makeMovePreviousPile(){
    int lastDealtCardPos = theFlop.size() - 1; //allows us to see how many cards have been dealt, are you even trying to challenge us Chris?
    int previouslyDealtCardPos = lastDealtCardPos - 1;

    if(lastDealtCardPos != 0){ // check that the deck has been shuffled
        String lastDealtCardValue = theFlop.get(lastDealtCardPos).getValue(); // fetches the value of the last dealt card
        String lastDealtCardSuit = theFlop.get(lastDealtCardPos).getSuit(); // fetches the suit of the last dealt card
        String previouslyDealtCardValue = theFlop.get(previouslyDealtCardPos).getValue(); // fetches the 2nd to last dealt card's value
        String previouslyDealtCardSuit = theFlop.get(previouslyDealtCardPos).getSuit(); // fetches the 2nd to last dealt card's suit

        if(lastDealtCardValue.equals(previouslyDealtCardValue)){
            theFlop.remove(previouslyDealtCardValue);
        }
        else if(lastDealtCardSuit.equals(previouslyDealtCardSuit)) {
            theFlop.remove(previouslyDealtCardSuit);
        }
        else {
            System.out.println("Cannot make a move. Are you sure you know the rules?");
        }
        printCardsFromFlop();
        //System.out.print(lastDealtCardValue + "\n");
    }
    else { // if it hasn't been shuffled we shun the user.
        System.out.println("Are you sure you shuffled the deck before dealing? Stop trying to cheat.");
        System.out.println("Next time we play Monopoly you won't be the banker. \n");
    }
    //System.out.print(totalDealtCards + " "); // should be equal to the amount of cards we've dealt, if not we've got a problem Huston.
    System.out.print("Total cards on the flop: " + lastDealtCardPos + " "); // checking to see that its working as intended
    //System.out.print("Previous card dealt: " + previouslyDealtCardPos);
}

此方法通过.equals检查卡片是否可以匹配的两个特征之一(两者之间的套装是相同的,或者两者之间的数字是相同的)。如果它们匹配,则.Fop.remove(previousDealtCard)应该从阵列中完全移除卡片,并且最近处理的卡片在阵列中占据其位置。

有人可以就如何删除匹配卡提供一些指导吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

显然,集合theFlop类似List<Card>,删除String将始终返回false。 试试

final Card lastDealtCard = theFlop.get(lastDealtCardPos);
...
if (lastDealtCard.getValue().equals(previouslyDealtCard.getValue())) {
    theFlop.remove(previouslyDealtCard);
}