bgb102 bgb102 - 21 days ago 6
Java Question

Matching game program provides no output from console?

Basically, the project description asks the user to create a matching game, consisting of:


  1. A two dimensional playing table (4x4), 16 pairs of matching cards, and a running count of the number of face-down cards. Include a method to retrieve a specific card from the table at an input x,y position.

  2. A
    gameBoard
    , and loop that continues play until all cards remain face up.

    The loop includes an interface with the user to pick two cards (inputting x,y table positions), checking if two cards are equal,
    decrementing the count of
    faceDown
    cards, and setting the
    faceUp boolean
    of the cards.



Essnetially, the program should run until all the cards are face up, and the game is won. I separated my program in to four separate classes below.

1.

public class Card {
private final int cardValue;
private boolean faceUp;

public Card(int value) {
cardValue = value;
faceUp = false;
}

public int getValue() {
return cardValue;
}

public boolean isFaceUp() {
return faceUp;
}

public void setFaceUp(boolean input) {
faceUp = input;
}

public static void printBoard(Card[][] cards) {
System.out.println("\t\t1\t2\t3\t4");
System.out.println("\t____________");

for(int i = 0; i < cards.length; i++) {
System.out.println((i + 1) + "\t|\t");

for(int j = 0; j < cards[0].length; j++)
if(cards[i][j].isFaceUp()) {
System.out.print(cards[i][j].getValue() + "\t"); }
else
System.out.println("*\t");

}
System.out.println();
}

}


2.

public class CreateBoard {

public static Card[][] createBoard() {
Card[][] board = new Card[4][4];

for(int i = 1; i <= 8; i++) {
for(int j = 1; j <= 2; j++) {

boolean boardLocation = false;

while(!boardLocation) {
int row = (int)(Math.random() * 4);
int column = (int)(Math.random() * 4);

if(board[row] == null && board[column] == null) {
boardLocation = true;
board[row][column] = new Card(i);
}
}
}
}
return board;
}
}


3.

public class Game {
public static boolean wonGame(Card[][] board) {
for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(!board[i][j].isFaceUp())
return false;
}
}
return true;
}

}


And finally, the main class:

public class GameDriver {
public static void main(String[] args) {
Card[][] board = CreateBoard.createBoard();
Scanner keyboard = new Scanner(System.in);

System.out.println("Starting Game...");

while(!Game.wonGame(board)) {
Card.printBoard(board);

System.out.println("Enter X-Coordinate #1 (1-4): ");
int column1 = keyboard.nextInt();

System.out.println("Enter Y-Coordinate #1 (1-4): ");
int row1 = keyboard.nextInt();

System.out.println("Enter X-Coordinate #2 (1-4): ");
int column2 = keyboard.nextInt();

System.out.println("Enter Y-Coordinate #2 (1-4): ");
int row2 = keyboard.nextInt();

Card card1 = board[row1][column1];
Card card2 = board[row2][column2];

if(card1.getValue() == card2.getValue() && !(row1 == row2 && column1 == column2))
{
card1.setFaceUp(true);
card2.setFaceUp(true);
}

else if(row1 == row2 && column1 == column2)
{
System.out.println("Points selected are the same, try again");
}

else
{
System.out.println(card1.getValue() + " and " + card2.getValue() + " do not match");

}
}
Card.printBoard(board);
}
}


The code seems to run fine, no errors or anything. However, after multiple test trials, the glaring issue is that it does not output anything to console... Am I missing something? Help would be appreciated!

Answer

Create board should be

public class CreateBoard {

public static Card[][] createBoard() {
    Card[][] board = new Card[4][4];

    for(int i = 1; i <= 8; i++) {
        for(int j = 1; j <= 2; j++) {

            boolean boardLocation = false;

            while(!boardLocation) {
                int row = (int)(Math.random() * 4); 
                int column = (int)(Math.random() * 4);

                if(board[row][column] == null) {
                    boardLocation = true;
                    board[row][column] = new Card(i);
                    }
                }
            }
        }
                return board;
    }
}

And for the Game class, since java array index is starting with 0 then these lines should be

Card card1 = board[row1-1][column1-1];
Card card2 = board[row2-1][column2-1];
Comments