gpt4 book ai didi

java - 循环遍历数组来检查类属性

转载 作者:行者123 更新时间:2023-12-01 08:57:25 24 4
gpt4 key购买 nike

我想创建一个名为 Pairs 的简单纸牌游戏。基本上,玩家翻 2 张牌,一次一张,如果 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是错误的,但我不知道如何修改...

最佳答案

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

关于java - 循环遍历数组来检查类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41956450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com