aRandomStudent
aRandomStudent

Reputation: 101

Use package-private class in the public class

I want to create a simple card game called Pairs. Basically a player flips 2 cards, 1 at a time, and if 2 cards matches, they remain faced up; if there is a mismatch, they are reversed back to the faced-down position. I created a package private class Card to store the actual value (char) of the card and whether or not it's flipped (faced-up). But I got the error

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

when I tried to run the following code...

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;
    }
}

What's the correct syntax of doing it? Or is there any simpler way of writing this game? (keeping the methods...)

Upvotes: 0

Views: 115

Answers (4)

user4142018
user4142018

Reputation: 550

You are accessing value from the Card class but you never declared that variable.Change your Card class like this, It will work

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

Upvotes: 2

panagdu
panagdu

Reputation: 2133

Your error comes from the fact that you've missed the value field in Card class.

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

Upvotes: 1

Raj Rusia
Raj Rusia

Reputation: 736

Card class doesn't have any data member of value name.

Here is your Card Class Code

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

So You need to create value as data member inside Card Class

Sample Code

    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
            }
        }

Hope it will help you..

Upvotes: 1

Pablo Lozano
Pablo Lozano

Reputation: 10342

Your Card class is not correct:

class Card{
    boolean flipped; //This is an attribute
    Card(char value){
        value = value; // This line does nothing!!!
    }
}

I guess your class should be something like :

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
    }

}

Upvotes: 2

Related Questions