Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Just a little Java Help
New Posts  All Forums:Forum Nav:

Just a little Java Help

post #1 of 4
Thread Starter 
Hey everyone,

I just started an algorithms class, and as a review our teacher assigned us to code minesweeper as a programming review. For some reason with the code I have, I keep receiving an ArrayIndexOutOfBoundsException. Can anyone help me out?

I fixed the problem, but will now leave this as an exercise to the reader. Anyone who can figure out the problem gets +rep.

The error I keep receiving happens in the MinesweeperPanel class on this line:
Code:
for(int q = 0; q < xLocations.size(); q++)
{
//First row = X co-ordinates of the bombs, Second row = Y co-ordinates. 
bombLocations[q][0] = xLocations.get(q); // <--------- HERE
bombLocations[q][1] = yLocations.get(q);
}
Here is what I have:

Minsweeper.class:
Code:
// MinesweeperXX.javaAuthor: Brendan Lesniak
// Requires: MinesweeperPanelXX.java
//
// Takes in two command line arguments, the number of squares across (or down)
// that the minesweeper board should be and the probability (as an integer
// percentage between 1 and 100) that any given square should be a bomb, and
// then allows the user to play against a board with those specs.

import javax.swing.JFrame;

public class MinesweeperXX 
{
   public static void main (String[] args) 
   {
      JFrame frame = new JFrame ("Minesweeper");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  
      MinesweeperPanelXX panel = new MinesweeperPanelXX(6,10);//Integer.parseInt(args[0]),
//Integer.parseInt(args[1]));

      frame.getContentPane().add(panel);
      frame.pack();
      frame.setVisible(true);
   }
}
and

MinsweeperPanel:
Code:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel;

public class MinesweeperPanelXX extends JPanel 
{

private static final long serialVersionUID = 1L;
private final int sqWidth;// width/height of a single cell
private final int WIN_WIDTH;// width/height of minesweeper window
private final int numSqs;

public int[][] gameBoard;

// Constructor - takes as arguments the number of squares across/down
// the playing field should be and the probability as an integer between
// 1 and 100 that a given cell should hold a bomb
public MinesweeperPanelXX(int numSquares, int probBomb) 
{
sqWidth = 30;
WIN_WIDTH = numSquares*sqWidth;
numSqs = numSquares; 
gameBoard = new int[numSquares][numSquares];

//This method is for debug purposes only. Outputs gameBoard[][] to the console.
//displayGameBoard();

Random random = new Random();
int rand;

for(int i = 0; i < numSquares; i++)
{
for(int n = 0; n < numSquares; n++)
{
//Randomly assign the board assignments (0 = empty, 1 = bomb)
rand = random.nextInt(100);

if(rand >= probBomb)
gameBoard[i][n] = 0;
else
gameBoard[i][n] = 1;
}
}

displayGameBoard();
System.out.println(returnBombLocations());

setPreferredSize (new Dimension(WIN_WIDTH, WIN_WIDTH));
setBackground (Color.white);
addMouseListener(new MouseHandler()); 
}

public void displayGameBoard()
{
for(int i = 0; i < numSqs; i++)
{
for(int n = 0; n < numSqs; n++)
{
System.out.println("gameBoard square at " + i + "," + n + " = " + gameBoard[i][n]);
}
}
}

public int[][] returnBombLocations()
{
ArrayList<Integer> xLocations = new ArrayList<Integer>();
ArrayList<Integer> yLocations = new ArrayList<Integer>();

int[][] bombLocations = new int[1][2];

for(int i = 0; i < numSqs; i++)
{
for(int n = 0; n < numSqs; n++)
{
if(gameBoard[i][n] == 1)
{
xLocations.add(i);
yLocations.add(n);
}
}
}

for(int q = 0; q < xLocations.size(); q++)
{
//First row = X co-ordinates of the bombs, Second row = Y co-ordinates. 
bombLocations[q][0] = xLocations.get(q);
bombLocations[q][1] = yLocations.get(q);
}

return bombLocations;
}

public void paintComponent(Graphics page) 
{
super.paintComponent(page);

page.setColor(Color.black);
// draw grid lines
for (int i = 0; i < numSqs; i++) 
{
page.drawLine(0, (i*sqWidth), WIN_WIDTH, (i*sqWidth));
page.drawLine((i*sqWidth), 0, (i*sqWidth), WIN_WIDTH);

for(int n = 0; n < numSqs; n++)
{
switch(gameBoard[i][n])
{
case 0:
//Insert drawing code here
break;
case 1:
//Insert drawing code here
break;
case 2:
//insert drawing code here
break;
}


}
}
}

private class MouseHandler implements MouseListener, MouseMotionListener 
{
// the method that is called when the mouse is clicked - note that Java
// is very picky that a click does not include any movement of the mouse;
// if you move the mouse while you are clicking that is a "drag" and
// this method is not called
public void mousePressed(MouseEvent event)
{
// get the X and Y coordinates of where the  mouse was clicked 
int xPos = event.getX();
int yPos = event.getY();


int numWidth = (int) (xPos / sqWidth);
int numHeight = (int) (yPos / sqWidth);

if (event.getButton() == MouseEvent.BUTTON1) 
{
System.out.println(numHeight + ", " + numWidth);
}

// third button click
if (event.getButton() == MouseEvent.BUTTON3) 
{
System.out.println("Third button was clicked.");
}

repaint();
}

public void mouseExited(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseMoved(MouseEvent event) {}
public void mouseDragged(MouseEvent event) {}
}

}

Edited by CovertCover - 2/2/11 at 3:12pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
post #2 of 4
I'm going to go with that the array starts at 0, and the .size parameter does not account for that, so the loop went one too far.
My PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon X4 635 Biostar A770E3 2 8400 GS's (not SLI) 2x2 gigs ddr3 2000 
Hard DriveOSMonitorKeyboard
OCZ Vertex! Win7 Pro x64 3 of them! Small 
PowerMouse
500W Wireless 
  hide details  
Reply
My PC
(13 items)
 
  
CPUMotherboardGraphicsRAM
Athlon X4 635 Biostar A770E3 2 8400 GS's (not SLI) 2x2 gigs ddr3 2000 
Hard DriveOSMonitorKeyboard
OCZ Vertex! Win7 Pro x64 3 of them! Small 
PowerMouse
500W Wireless 
  hide details  
Reply
post #3 of 4
It is actually because you are instantiating the array with [1][2] but you have 3 locations for the bombs. You have a couple of choices if you know how many bombs you plan on using you could instantiate it with [NumberOfBombs][2] or you could do
[xLocations.size()][2] that way you would know how many positions you need. Right now you are basically saying the array will have 1 column and 2 rows per column when in reality you need n number of columns depending on how many bombs with 2 rows per column to give it enough room to store all your variables. if you go the route of using the xLocations.size you will need to move the bombLocations array instantiation to after the for loops where you fill xLocations and yLocations

Hope that helps
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Q6600 DG33TL XFX 6950 2GB 4gigs Corsair XMS2 DDR2 
Hard DriveOptical DriveOSMonitor
1.5TB seagate 2X640GB Samsung DVDRW Windows Vista & Debian Linux Samsung 19" + Acer 24" Wide 
KeyboardPowerCaseMouse
Microsoft SideWinder X6 Antec 750 TruePower Raidmax Smilodon COOLER MASTER Sentinel 
Mouse Pad
RocketFish 
  hide details  
Reply
post #4 of 4
Thread Starter 
Quote:
Originally Posted by Midpipps View Post
It is actually because you are instantiating the array with [1][2] but you have 3 locations for the bombs. You have a couple of choices if you know how many bombs you plan on using you could instantiate it with [NumberOfBombs][2] or you could do
[xLocations.size()][2] that way you would know how many positions you need. Right now you are basically saying the array will have 1 column and 2 rows per column when in reality you need n number of columns depending on how many bombs with 2 rows per column to give it enough room to store all your variables. if you go the route of using the xLocations.size you will need to move the bombLocations array instantiation to after the for loops where you fill xLocations and yLocations

Hope that helps
DING DING DING winner here , figured it out. +rep.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i7-930 D0 3.8Ghz ASUS P6T SE PowerColor HD5870 1GB 6GB Corsair Dominator XMS3 8-8-8-20 1200Mhz 
Hard DriveOSMonitorPower
1TB Wester Digital Windows 7 Home 64bit Acer 23" XFX 850w Black Edtion 
Case
HAF 932 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Just a little Java Help