# Memory card game using JOption... Need help flipping cards...more info inside, thanks

• 12-02-2009, 11:19 AM
moe123
Memory card game using JOption... Need help flipping cards...more info inside, thanks
Here is what I have at the moment, I was able to create two arrays 4x4 , ones a boolean and the other is a normal one called "board". I got to the level where I was able to shuffle the numbers randomly and I've tested it. There is 2 lines of code that shows where I tested two different coordinates. The project I'm working on asks to create a 4 x 4 to match numbers with each other, if correct the two numbers should remain revealed as you continue to play and if the two numbers are not the same to flip once more. I got some commented out code, not sure if thats any good. Thanks and let me know if further details needed.

Code:

``` import javax.swing.JOptionPane; import java.util.Random; public class MemoryGame {     public static void main(String[] args) {         int[][] board = {             {1, 1, 2, 2},             {3, 3, 4, 4},             {5, 5, 6, 6},             {7, 7, 8, 8}         };         boolean[][] revealed = new boolean[4][4];         String input;         int row;         int col;         showBoard(board, revealed);         shuffleBoard(board);         //this is just to test the my board for right now         revealed[2][2] = true;         revealed[3][1] = true; //        input = JOptionPane.showInputDialog("Enter the row"); //        row = Integer.parseInt(input); //        input = JOptionPane.showInputDialog("Enter the col"); //        col = Integer.parseInt(input); //        row -= 1; //        col -= 1; // //        board[row][col] = 'B'; // //        for (int i = 0; i < board.length; i++) { //            for (int j = 0; j < board[i].length; j++) { //                if (row != i) { //                    if (row - i == j - col || row - i == col - j) { //                        board[i][j] = 'x'; //                    } //                } //            } //        }         showBoard(board, revealed);         System.exit(0);     }     public static void showBoard(int[][] b, boolean[][] revealed) {         String theBoard = "";         for (int i = 0; i < b.length; i++) {             for (int j = 0; j < b[i].length; j++) {                 if (revealed[i][j] == true) {                     theBoard = theBoard + " " + b[i][j] + " ";                 } else {                     theBoard = theBoard + " X ";                 }             }             theBoard = theBoard + "\n";         }         JOptionPane.showMessageDialog(null, theBoard);     }     public static void shuffleBoard(int[][] b) {         Random randomNumbers = new Random();         for (int i = 0; i < b.length; i++) {             for (int j = 0; j < b[i].length; j++) {                 // Shuffling code goes here                 int x = randomNumbers.nextInt(4);                 int y = randomNumbers.nextInt(4);                 int old = b[x][y];                 b[x][y] = b[i][j];                 b[i][j] = old;             }         }     } }```
Thanks!
• 12-02-2009, 04:39 PM
Xeroeh
What a coincidence I too am writing this program. What we did was use the Boolean array to see if the cards are either true(up) or false(down) then if it's true show the card or number and if false so an *.
Maybe you are in my class. Uncg?