Results 1 to 2 of 2
  1. #1
    moe123 is offline Member
    Join Date
    Sep 2009
    Posts
    4
    Rep Power
    0

    Question 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.



    Java 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!
    Last edited by moe123; 12-02-2009 at 11:22 AM.

  2. #2
    Xeroeh is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Default

    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?

Similar Threads

  1. Replies: 44
    Last Post: 10-19-2009, 09:07 PM
  2. flipping memory cards if they are not matched...
    By yanipao in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 03:25 AM
  3. Creating a Card Game in Java
    By Natrix in forum New To Java
    Replies: 1
    Last Post: 05-05-2009, 06:55 PM
  4. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 04:53 PM
  5. A Online Card Game
    By GonzaloP in forum Networking
    Replies: 0
    Last Post: 12-28-2008, 07:37 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •