![]() |
random numbers in 2D array
ok so i keep getting an array outofBounds exception when i try to run this. I need to come up with a random number then place the letter B across this 2 demensional array either verticle or horizontal which is also determined by a random number. Here is what i have can someone tell me why i get an arrayOutOfBoundsException?
Here is the class that makes the random number :
public class RandomNumberhere is the class that makes the 2D array :
public class Board:
class Playerhere is the class that runs it all :
public class Play |
i'm assuming that you don't always get the error message.
in your player class, try checking what the size of your battleship is, then when you want to put it into the array, you check if it will fit, then just move it's location a bit to the left or up. if you can post your battleship class, then i can see if i can get it to work and push you in the right direction.Ü |
your right i dont get the exception every time and here is my battleship class the variables are sent down from the ship class...
Ship class :
abstract public class Shiphere is the BattleShip class :
public class BattleShip extends Ship |
sorry, got a bit busy and also have my finger in so many pies here (hmm... pies... *drool* ) i kinda forgot about this.
ok, now don't freak out, but i modified your board class a bit board.java :
public class Boardplayer.java :
class Playerlets see... what did i do.... ah!! when board is initiated and the object created, it creates the array and puts in a bunch of w's. what then happens, is that player class calls getArray() and that gives it an array. it puts in the ship and then sends it back to the board class with setBoard( array ) and that tells it to put it on screen. you can try it, it works(except for the out of bounds part, i'm gonna try fix that now). what happens now, is that you will have B's on the board instead of all w's. sorry about not replying dude. ;) |
this is where you get the ArrayIndexOutOfBoundsException. i changed it from ">" to "==". all that needs to happen now, is that when R or C hits 10, then you need to change the direction you're putting your B's down. put them before the spot where you started putting them down. make sense? ;)
if(C == 10) break; if(R == 10) break; |
| All times are GMT -5. The time now is 11:03 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC