我想制作一款名为Pairs的简单纸牌游戏。基本上一个玩家翻转2张牌,一次1张,如果2张牌匹配,他们仍然面朝上;如果存在不匹配,则将它们反转回面朝下的位置。我创建了一个包私有类卡来存储卡的实际值(char)以及它是否被翻转(面朝上)。但我得到了错误
MatchCardGame.java:57: error: cannot find symbol
showBoard[i-1] = tempCard2.value + "(" + i + ") ";
^
symbol: variable value
location: variable tempCard2 of type Card
MatchCardGame.java:61: error: cannot find symbol
showBoard[i-1] = tempCard1.value + "(" + i + ")";
^
symbol: variable value
location: variable tempCard1 of type Card
2 errors
当我尝试运行以下代码时...
public class MatchCardGame {
public char[] gameCards;
int gcCount, showCount;
String[] showBoard;
char firstCard = 'a';
char cardValue;
Card tempCard1, tempCard2;
int flipCount = 0;
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
Card[] 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 (flipCount % 2 == 0){
tempCard2 = new Card(gameCards[i-1]);
showBoard[i-1] = tempCard2.value + "(" + i + ") ";
tempCard2.flipped = true;
}else{
tempCard1 = new Card(gameCards[i-1]);
showBoard[i-1] = tempCard1.value + "(" + i + ")";
tempCard1.flipped = true;
}
flipCount++;
return true;
}
// returns true if card1 and card2 are matched - only executes when an even # of flips are made
// public boolean wasMatch(){}
// if card1 and card2 create a mismatch, reverse them back to faced down position
// public void flipMismatch(){}
// if all cards are flipped and matched, game is over
// public boolean gameOver(){}
// count the # of flips made during the game
// public int getFlips(){}
public static void main(String[] args) {
//set up reader to take inputs
java.util.Scanner reader = new java.util.Scanner (System.in);
int n = 16; //game size
MatchCardGame g1 = new MatchCardGame(n);
System.out.println(g1.boardToString());
g1.flip(5);
System.out.println(g1.boardToString());
}
}
class Card{
boolean flipped; // check if card is flipped
Card(char value){
value = value;
}
}
这样做的正确语法是什么?或者有没有更简单的方式来编写这个游戏? (保持方法......)
答案 0 :(得分:2)
您的卡类不正确:
class Card{
boolean flipped; //This is an attribute
Card(char value){
value = value; // This line does nothing!!!
}
}
我猜你的课应该是这样的:
class Card{
boolean flipped; // check if card is flipped
char value;
Card(char val){
this.value = val; // "this" is not necessary, but makes it more readable
}
}
答案 1 :(得分:2)
您正在从Card类访问值,但您从未声明过该变量。请更改您的Card类,它会起作用
class Card{
boolean flipped; // check if card is flipped
char value;
Card(char value){
value = value;
}
}
答案 2 :(得分:1)
您的错误来自于您错过了Card类中的value
字段。
class Card{
boolean flipped; // check if card is flipped
char value;
Card(char value){
this.value = value;
}
}
答案 3 :(得分:1)
卡片类没有值名称的任何数据成员。
这是您的卡类代码
class Card{
boolean flipped; // check if card is flipped
Card(char value){
value = value;
}
}
所以你需要在Card Class
中创建值作为数据成员示例代码
class Card{
boolean flipped; // check if card is flipped
char value; //here you need to create value data member
//datatype you can change according to your requirement
Card(char value){
this.value = value; //here need to write this.value because you want assign value to class member
}
}
希望它会对你有帮助..