Я хочу создать простую карточную игру под названием «Пары». В основном игрок переворачивает 2 карты, по одному за раз, и если 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;
}
}
Что такое правильный синтаксис это делать? Или есть ли более простой способ написания этой игры? (Сохраняя методы ...)
Пожалуйста, напишите полный стек tt – AMB