Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By KevinWorkman

Thread: HELP!Another Memory Game issue

  1. #1
    allan2020 is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Default HELP!Another Memory Game issue

    Hello All,

    I have a java assignment thats supposed to create a memory game, with 10 rows and 10 columns.50 images have been supplied.

    The part i'm stuck is the the window with the actual images on the grid.The images are supposed to be generated randomly each time the application is run . The actual game logic will come after I complete this part

    So far I have the following code seperated in two files

    memoryGame.java

    Java Code:
    import java.util.Random;
    import java.awt.*; 
    import javax.swing.*;
    import java.math.*;
    
    public class MemoryGame {
        
        static final int row = 10;
        static final int col = 10;
        int size= 0;
        String[][] position = new String[row][col];
        String[] tile = new String[new java.io.File("icons").list().length*2];
        String [] fileNames = new String[new java.io.File("icons").list().length];
        
        public MemoryGame() {
            
            String[] fileNames = new java.io.File("icons").list();
    
            for(int i=0;i<2;i++){
               for(int j=0;j<=10;j++){
                   tile[size] = fileNames[j];
                   size++;
               }
           }
        }
        public String[][] positionMethod(){
            String[] icons = new String[size]; 
            for (int row=0; row<=10; row++){
                for (int col=0; col<=10; col++) {
                    int num = RandomNumber();
                    position [row][col] = tile[num];
                    icons[size] = tile[num];
                    size--;
                }
            }
            return position;
        }
        
        public int RandomNumber(){
          Random rand = new Random();
          int num = rand.nextInt(50);
          return num;
        }
        
        public void isOver(){
        
        }
    }
    and the actual display class

    MemoryGameDisplay.java

    Java Code:
    import java.awt.*; 
    import javax.swing.*;
    public class MemoryGameDisplay extends JFrame {
        
        JButton buttons;
        MemoryGame memoryGame = new MemoryGame();
        
    
        public MemoryGameDisplay(String title) {
            super(title);
            
            getContentPane().setLayout(new GridLayout(10,10)); 
            
            for (int row=0; row<=10; row++){
                for (int col=0; col<=10; col++) {
                    buttons = new JButton(new ImageIcon("icons" + java.io.File.separator + "blank.jpg"));
                    buttons.setSelectedIcon(new ImageIcon("icons" + java.io.File.separator + memoryGame.positionMethod()[row][col]));
                    buttons.setSelected(true);
                    getContentPane().add(buttons);
                }
            }
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(350, 350); 
        } 
        
        public static void main(String[] args) { 
            new MemoryGameDisplay("Memory Game").setVisible(true); 
        
           
        
        }
    
    }
    the code is not compiling and giving me an error message :

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102
    at MemoryGame.positionMethod(MemoryGame.java:32)
    at MemoryGameDisplay.<init>(MemoryGameDisplay.java:17 )
    at MemoryGameDisplay.main(MemoryGameDisplay.java:27)

    now I know my for loop , most likely in my positionMethod() is causing the ArrayIndexOutOfBoundsException, I'm just not sure how .

    Any help would be greatly appreciated

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: HELP!Another Memory Game issue

    I suggest stepping through this with a debugger, or at the very least adding some print statements, to figure out what your code is actually doing. What array are you trying to access? How many indexes does it have? What index are you trying to access?
    Tolls likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,800
    Rep Power
    19

    Default Re: HELP!Another Memory Game issue

    Quote Originally Posted by allan2020 View Post
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 102
    at MemoryGame.positionMethod(MemoryGame.java:32)
    at MemoryGameDisplay.<init>(MemoryGameDisplay.java:17 )
    at MemoryGameDisplay.main(MemoryGameDisplay.java:27)

    now I know my for loop , most likely in my positionMethod() is causing the ArrayIndexOutOfBoundsException, I'm just not sure how .

    Any help would be greatly appreciated
    You know exactly where it is happening, as the stacktrace tells you it is on line 32.
    Use that to narrow down which array index is causing the problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Java Memory Issue
    By personal in forum Advanced Java
    Replies: 12
    Last Post: 01-07-2012, 02:05 PM
  2. Memory Game Help
    By JordanJava1 in forum New To Java
    Replies: 7
    Last Post: 12-13-2010, 05:48 PM
  3. Memory game
    By ronyyy in forum New To Java
    Replies: 9
    Last Post: 04-15-2010, 11:00 AM
  4. Memory Game
    By torres in forum AWT / Swing
    Replies: 6
    Last Post: 04-21-2009, 11:00 PM
  5. help with memory game!
    By rac in forum New To Java
    Replies: 6
    Last Post: 04-13-2009, 11:39 PM

Posting Permissions

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