Results 1 to 8 of 8
  1. #1
    HbJgd's Avatar
    HbJgd is offline Member
    Join Date
    Mar 2011
    Location
    Doesn't matter
    Posts
    10
    Rep Power
    0

    Default Help With my Game for CS class

    Hello,

    I need some help editing my computer science project that is due next monday.
    I have 2 problems that i hope someone can help me with.
    1. My Restart Button does not work correctly I get an "ArrayIndexOutOfBounds" Error.
    2. When I click a square it is supposed to turn yellow as if it were a light, but it just stays gray.

    Here is my code. It is a little lengthy for this simple of a game.
    But please help if you can


    import java.applet.*;
    import java.awt.*;
    import java.lang.*;
    import java.net.*;

    public class EveryLight extends Applet
    {
    private final int ON = 1;
    private final int OFF = -1;
    private final int WIDTH = 200;
    public int light[][];
    public int countOn = 0;
    public int countOff = 0;
    public int end = 0;


    //Starts the game by setting the background to white.
    public void startGame()
    {
    initializeLights();
    setBackground(Color.white);
    }

    public void initializeLights()
    {
    light = new int[5][5];
    for (int l = 0; l < 5; l++)
    {
    for(int i = 0; i < 5; i++)
    {
    light[l][i] = OFF;
    }
    }
    countOnAndOff();
    }


    public void paint(Graphics g)
    {
    initializeLights();
    g.setColor(Color.blue);
    g.fill3DRect(210, 165, 60, 20, true);
    g.setColor(Color.red);
    g.drawString("RESTART", 214, 180);
    makeBoard(g);
    for (int i = 0; i < 5; i++)
    {
    for (int j = 0; j < 5; j++)
    {
    if (light[i][j] != 0)
    {
    drawLight(i, j, g);
    }
    }
    }
    drawOnAndOffCounters(g);
    }


    public void drawLight(int column, int row, Graphics g)
    {
    if (light[column][row] == ON)
    {
    g.setColor(Color.yellow);
    }
    else if (light[column][row] == OFF)
    {
    g.setColor(Color.gray);
    }
    g.fillRect(column * WIDTH / 5 + 2, row * WIDTH / 5 + 2, WIDTH / 5 - 3, WIDTH / 5 - 3);
    }

    //Creates the board.
    public void makeBoard (Graphics graph)
    {
    graph.setColor(Color.blue);
    graph.drawLine(0, 0, 0, WIDTH);
    graph.drawLine(WIDTH, 0, WIDTH, WIDTH);
    graph.drawLine(0, 0, WIDTH, 0);
    graph.drawLine(0, WIDTH, WIDTH, WIDTH);
    for (int i = 1; i < 5; i++)
    {
    graph.drawLine(WIDTH * i / 5, 0, WIDTH * i / 5, WIDTH);
    graph.drawLine(0, WIDTH * i / 5, WIDTH, WIDTH * i / 5);
    }
    }

    //Counts and total the on and off lights.
    public void countOnAndOff ()
    {
    countOn = 0;
    countOff = 0;
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++) {
    if(light[i][j] == ON) countOn++;
    if(light[i][j] == OFF) countOff++;
    }
    }
    if (countOn == 25) {
    winGame(getGraphics());
    }
    }

    //Wins the game and displays Congratulations!
    public void winGame (Graphics g)
    {
    update(getGraphics());
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++)
    {
    light[i][j] = OFF;
    update(getGraphics());
    }
    }
    for (int i = 0; i < 5; i++) {
    for (int j = 0; j < 5; j++)
    {
    light[i][j] = ON;
    update(getGraphics());
    }
    }
    g.setColor(Color.yellow);
    g.fillRect(65, 98, 80, 10);
    g.setColor(Color.black);
    g.drawString("Congratulations!!", 66, 106);
    g.setColor(Color.red);
    g.drawString("Congratulations!!", 65, 105);
    end = 1;
    }

    //Displays the number of on and off lights.
    public void drawOnAndOffCounters (Graphics graph)
    {
    graph.setColor(Color.white);
    graph.fill3DRect(WIDTH+15, 50, 30,20, false);
    graph.fill3DRect(WIDTH+15, 110, 30,20, false);
    graph.setColor(Color.gray);
    graph.fill3DRect(WIDTH+5, 85, 20,20, true);
    graph.setColor(Color.yellow);
    graph.fill3DRect(WIDTH+5, 20, 20,20, true);
    graph.setColor(Color.black);
    graph.drawString("LightOn", WIDTH+30, 35);
    graph.drawString("LightOff", WIDTH+30, 100);
    graph.drawString(Integer.toString(countOn), WIDTH+20, 65);
    graph.drawString(Integer.toString(countOff), WIDTH+20, 125);
    }


    public boolean mouseUp(Event event, int x, int y) {
    if (end == 1)
    {
    init();
    update(getGraphics());
    }
    else
    {
    if (x > 210 && x < 270 && y > 165 && y < 185)
    {
    init();
    update(getGraphics());
    }
    int column = (int)(x / (WIDTH / 5));
    int row = (int)(y / (WIDTH / 5));
    light[column][row] = -light[column][row];
    try
    {
    light[column+1][row] = -light[column+1][row];
    }
    catch(Exception e)
    {
    }
    try
    {
    light[column-1][row] = -light[column-1][row];
    }
    catch(Exception e)
    {
    }
    try
    {
    light[column][row+1] = -light[column][row+1];
    }
    catch(Exception e)
    {
    }
    try {
    light[column][row-1] = -light[column][row-1];
    } catch(Exception e)
    {
    }
    countOnAndOff();
    if (end == 0)
    {
    update(getGraphics());
    }
    }
    return true;
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Wrap your code in tags. Place [ code ] before and [ /code ] (without spaces) after your code.
    If you get an error copy and paste the EXACT and FULL error message(s) and indicate on which line it occurs.

  3. #3
    HbJgd's Avatar
    HbJgd is offline Member
    Join Date
    Mar 2011
    Location
    Doesn't matter
    Posts
    10
    Rep Power
    0

    Default

    Java Code:
    /*
     	@author: Jack Davis
     	Objective of the Game:
     		To systematically change all of the lights to on.
     		When you select a square it will turn all of the lights around that light
     		not diagonal just beside it to either On or Off, according to it's previous state.
     		There is a reset Button on the bottom right corner of the game that re-Initializes 
     		the game and allows you to start over with all of the lights in the Off state of being.
     		The two counters on the right hand side of the screen display the number of On and Off
     		lights that are currently on the board.
     		When you have turned all 25 of the lights to the On orientation, there will be a message
     		that pops up and says "Congratulations!".
     */
    
    import java.applet.*;
    import java.awt.*;
    import java.lang.*;
    import java.net.*;
    
    public class EveryLight extends Applet
    {
    	private final int ON = 1;
    	private final int OFF = -1;
    	private final int WIDTH = 200;
    	public int light[][];
    	public int countOn = 0;
    	public int countOff = 0;
    	public int end = 0;
    	
    	
    	//Starts the game by setting the background to white and initializing all
    	//of the lights in the grid.
    	public void startGame()
    	{	
    		initializeLights();
    		setBackground(Color.white);
    	}
    	
    	//Set all of the lights in the grid to OFF for the beginning of the game,
    	//and then counts the lights that are ON or OFF.
    	public void initializeLights()
    	{
    		light = new int[5][5];
    		for (int l = 0; l < 5; l++)
    		{
    			for(int i = 0; i < 5; i++)
    			{
    				light[l][i] = OFF;
    			}
    		}
    		countOnAndOff();
    	}
    	
    	//Makes the board, Initializes the lights once again, and then draws
    	//the lights in order.
    	public void paint(Graphics g) 
    	{
    		initializeLights();
            g.setColor(Color.blue);
            g.fill3DRect(210, 165, 60, 20, true);
            g.setColor(Color.red);
            g.drawString("RESTART", 214, 180);
            makeBoard(g);
            for (int i = 0; i < 5; i++)
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      if (light[i][j]  != 0) 
                      {
                           drawLight(i, j, g);
                      }
                 }
            }
            drawOnAndOffCounters(g);
       }
    	
    	//Sets up the lights to be approximately 200 wide, and assigns each light to
    	//be either gray or yellow according to what it currently is set as.
    	public void drawLight(int column, int row, Graphics g) 
    	{
            if (light[column][row] == ON) 
            {
                 g.setColor(Color.yellow);
            } 
            else if (light[column][row] == OFF) 
            {
                 g.setColor(Color.gray);
            }
            g.fillRect(column * WIDTH / 5 + 2, row * WIDTH / 5 + 2, WIDTH / 5 - 3, WIDTH / 5 - 3);
       }
    	
    	//Creates the board by drawing all of the necessary lines.
    	public void makeBoard (Graphics graph)
    	{
    		graph.setColor(Color.blue);
    		graph.drawLine(0, 0, 0, WIDTH);
    		graph.drawLine(WIDTH, 0, WIDTH, WIDTH);
    		graph.drawLine(0, 0, WIDTH, 0);
    		graph.drawLine(0, WIDTH, WIDTH, WIDTH);
    		for (int i = 1; i < 5; i++)
    		{
    			graph.drawLine(WIDTH * i / 5, 0, WIDTH * i / 5, WIDTH);
    			graph.drawLine(0, WIDTH * i / 5, WIDTH, WIDTH * i / 5);
    		}
    	}
    	
    	//Counts and total the on and off lights.
    	public void countOnAndOff ()
    	{
    		countOn = 0;
            countOff = 0;
            for (int i = 0; i < 5; i++) 
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      if(light[i][j] == ON) countOn++;
                      if(light[i][j] == OFF) countOff++;
                 }
            }
            if (countOn == 25) 
            {
                 winGame(getGraphics());
            }
    	}
    	
    	//Wins the game and displays Congratulations!
    	public void winGame (Graphics g)
    	{
    		update(getGraphics());
            for (int i = 0; i < 5; i++) 
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      light[i][j] = OFF;
                      update(getGraphics());
                 }
            }
            for (int i = 0; i < 5; i++) 
            {
                 for (int j = 0; j < 5; j++) 
                 {
                      light[i][j] = ON;
                      update(getGraphics());
                 }
            }
            g.setColor(Color.yellow);
            g.fillRect(65, 98, 80, 10);
            g.setColor(Color.black);
            g.drawString("Congratulations!!", 66, 106);
            g.setColor(Color.red);
            g.drawString("Congratulations!!", 65, 105);
            end = 1;
    	}
    	
    	//Displays the number of on and off lights and sets up the LightOn, and 
    	//LightOff displays.
    	public void drawOnAndOffCounters (Graphics graph)
    	{
    		graph.setColor(Color.white);
            graph.fill3DRect(WIDTH+15, 50, 30,20, false);
            graph.fill3DRect(WIDTH+15, 110, 30,20, false);
            graph.setColor(Color.gray);
            graph.fill3DRect(WIDTH+5, 85, 20,20, true);
            graph.setColor(Color.yellow);
            graph.fill3DRect(WIDTH+5, 20, 20,20, true);
            graph.setColor(Color.black);
            graph.drawString("LightOn", WIDTH+30, 35);
            graph.drawString("LightOff", WIDTH+30, 100);
            graph.drawString(Integer.toString(countOn), WIDTH+20, 65);
            graph.drawString(Integer.toString(countOff), WIDTH+20, 125);
    	}
    	
    	//Overrides the Graphic Class mouseUp method.
        public boolean mouseUp(Event event, int x, int y) 
        {
             if (end == 1) 
             {
                  init();
                  update(getGraphics());
             } 
             else 
             {
                  if (x > 210 && x < 270 && y > 165 && y < 185) 
                  {
                	  init();
                      update(getGraphics());
                  }
                  int column = (int)(x / (WIDTH / 5));
                  int row    = (int)(y / (WIDTH / 5));
             	  light[column][row] = -light[column][row];
             	  try 
             	  {
                   	   light[column+1][row] = -light[column+1][row];
             	  } 
             	  catch(Exception e) 
             	  {
             	  }
             	  try 
             	  {
                       light[column-1][row] = -light[column-1][row];
                  } 
                  catch(Exception e) 
                  {
                  }
                  try 
                  {
                       light[column][row+1] = -light[column][row+1];
                  } 
                  catch(Exception e) 
                  {
                  }
                  try 
                  {
                       light[column][row-1] = -light[column][row-1];
                  } 
                  catch(Exception e) 
                  {
                  }
                  countOnAndOff();
                  if (end == 0) 
                  {
                       update(getGraphics());
                  }
             }
             return true;
        }
    }
    I get no errors when compiling this in eclipse. but when i run it and try to use my restart button, I get this error(This is word for word what comes up):

    Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 6
    at EveryLight.mouseUp(EveryLight.java:164)
    at java.awt.Component.handleEvent(Unknown Source)
    at java.awt.Component.postEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.awt.EventQueue$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.awt.EventQueue$2.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  4. #4
    HbJgd's Avatar
    HbJgd is offline Member
    Join Date
    Mar 2011
    Location
    Doesn't matter
    Posts
    10
    Rep Power
    0

    Default

    And just so you know.
    Line 164 is in the ---- drawOnAndOffCounters ---- method.. it looks like this

    graph.setColor(Color.yellow);

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    The error message says that line 164 in is the mouseUp method. The line you have highlight cannot possible throw an ArrayIndexOutOfBoundsException.

    If your 2D array is a 5 by 5 grid then the valid indicies are 0 to 4. Thus 6 is definately out of bounds. You need to do some debugging to find out when some value reaches 6 and causes the error.

    Also why are you accessing the array as [column][row]? The most common way is to use [row][column].

  6. #6
    HbJgd's Avatar
    HbJgd is offline Member
    Join Date
    Mar 2011
    Location
    Doesn't matter
    Posts
    10
    Rep Power
    0

    Default

    I know. I wrote this code in literally about 5-6 hours total. It's something I've thrown together and am now working to perfect it.


    And when you say debug, you mean basically walk through it and write it all out by hand? or use the debug tool in eclipse?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You can use the debug tool in Eclipse which can be overwhelming. Or you can add a bunch of print statements in your code at various points to print out the value of variables. This way you can see what values those variables actually have versus what you think they should have.

  8. #8
    HbJgd's Avatar
    HbJgd is offline Member
    Join Date
    Mar 2011
    Location
    Doesn't matter
    Posts
    10
    Rep Power
    0

    Default

    Wow. I honestly wouldn't have thought of that. thank you Junky!

Similar Threads

  1. Replies: 1
    Last Post: 06-02-2011, 10:29 AM
  2. Class in simple game problem
    By thebiggestfish26 in forum Java Applets
    Replies: 1
    Last Post: 04-19-2011, 03:30 AM
  3. how to log into a game with my player.java class
    By basketball8533 in forum New To Java
    Replies: 11
    Last Post: 10-13-2010, 01:40 AM
  4. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  5. Pig Game class plz HELP!!!
    By linux1man in forum New To Java
    Replies: 5
    Last Post: 11-26-2008, 06:28 AM

Posting Permissions

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