Results 1 to 3 of 3
Thread: HELP!Another Memory Game issue
- 03-06-2012, 05:14 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 1
- Rep Power
- 0
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
and the actual display classJava 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(){ } }
MemoryGameDisplay.java
the code is not compiling and giving me an error message :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); } }
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
- 03-06-2012, 05:26 PM #2
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?
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 03-06-2012, 05:41 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Java Memory Issue
By personal in forum Advanced JavaReplies: 12Last Post: 01-07-2012, 02:05 PM -
Memory Game Help
By JordanJava1 in forum New To JavaReplies: 7Last Post: 12-13-2010, 05:48 PM -
Memory game
By ronyyy in forum New To JavaReplies: 9Last Post: 04-15-2010, 11:00 AM -
Memory Game
By torres in forum AWT / SwingReplies: 6Last Post: 04-21-2009, 11:00 PM -
help with memory game!
By rac in forum New To JavaReplies: 6Last Post: 04-13-2009, 11:39 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks