循环遍历数组以检查类属性

时间:2017-01-31 11:30:44

标签: java

我想制作一款名为Pairs的简单纸牌游戏。基本上一个玩家翻转2张牌,一次1张,如果2张牌匹配,他们仍然面朝上;如果存在不匹配,则将它们反转回面朝下的位置。对于gameOver()方法,如果所有卡面朝上并匹配,则应返回true,否则应返回false。如何遍历卡阵列以检查卡阵列中的每个对象flipped == true

public class MatchCardGame {

    public char[] gameCards;
    String[] showBoard;
    Card[] cardArray;
    int flipCount = 0;
    int gcCount, showCount, temp1_pos, temp2_pos;
    char firstCard = 'a';
    Card tempCard1, tempCard2;

    public MatchCardGame(int n){ // n is the size of the game set by the player in the main, it could only be a multiply of four

        // Check if input n is valid
        if ((n % 4) != 0 || n < 4 || n > 104) System.exit(0);

        // Create an array of cards used in the game
        // here we're using a-z as cards thus min = 4 and max = 26*4
        gcCount = 0;
        gameCards = new char[n];
        for (int i = 0;i < (n / 4); i++){
            for (int j = 0; j < 4; j++){
                gameCards[gcCount] = firstCard;
                gcCount++;
            }
            firstCard++;
        }

        // Display the back of the cards array
        showCount = 1;
        showBoard = new String[n];
        for (int i = 0; i < n; i++){
            showBoard[showCount-1] = "X(" + showCount + ") ";
            showCount++;
        }

        // Create an array of object card, assign each card with a corresponding value in the gameCards array
        cardArray = new Card[n];
        for (int i = 0; i < cardArray.length; i++){
            cardArray[i] = new Card(gameCards[i]);
        }

    }

    // String representation of cards array
    public String boardToString(){
        StringBuilder builder = new StringBuilder();
        for(String board : showBoard){
            builder.append(board);
        }
        return builder.toString();
    }

    // flip the card - if already faced up or picked an invalid card, don't flip
    public boolean flip (int i){
        if (cardArray[i].flipped == true){
            System.out.println("This card is already matched.");
        }
        if (i > gameCards.length || i <= 0){
            System.out.println("Picked an invalid card.");
        }
        if (flipCount % 2 == 0){
            tempCard2 = new Card(gameCards[i-1]);
            temp2_pos = i - 1;
            showBoard[i-1] = tempCard2.value + "(" + i + ") ";
        }else{
            tempCard1 = new Card(gameCards[i-1]);
            temp1_pos = i - 1;
            showBoard[i-1] = tempCard1.value + "(" + i + ") ";
        }
        flipCount++;
        return true;
    }

    // returns true if card1 and card2 are matched - only executes when an even # of flips are made
    public boolean wasMatch(){
        boolean result = false;
        if (flipCount % 2 == 0){
            if (tempCard1.value == tempCard2.value){
                result = true;
                cardArray[temp1_pos].flipped = true;
                cardArray[temp2_pos].flipped = true;
            }
        }
        return result;
    }

    // if card1 and card2 create a mismatch, reverse them back to faced down position
    public void flipMismatch(){
        if (tempCard1.value != tempCard2.value){
            showBoard[temp1_pos] = "X(" + temp1_pos + ") "; // temp1_pos prints weird indexes
            showBoard[temp2_pos] = "X(" + temp2_pos + ") "; // temp2_pos prints weird indexes
        }
    }

    // if all cards are flipped and matched, game is over
    public boolean gameOver(){
        return false;
    }

    // count the # of flips made during the game
    public int getFlips(){
        return flipCount;
    }

}

class Card{
    boolean flipped; // check if card is flipped
    char value;
    Card(char value){
        this.value = value;
        this.flipped = false;
    }
}

另外,temp1_pos和temp2_pos是错误的,但我不知道如何修改它......

1 个答案:

答案 0 :(得分:1)

for(Card card : cardArray) {
    if (!card.flipped) return false;
}
return true;