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 . So far I have the following code seperated in two files

memoryGame.java

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

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

Recommended Answers

All 2 Replies

Try changing

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

to

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

Get rid of the <= and put in a <. In java, an array with 10 spaces goes from 0...9, and not 0...10. In you method, you are still executing the for loops when the array space is 10, which doesn't actually exist, giving you an out of bounds exception.

How are you keeping the random number used on line 32 within the bounds of the array?
In the RandomNumbr method you have a hardcoded: 50. Your code should use the size of the array, not a fixed number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.