![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 347
Rep Power: 4
![]() |
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 RandomNumber
{
int num;
int sides;
RandomNumber(int number, int num_sides)
{
num = 1;
sides = num_sides;
if(number > 1)
{
num = number;
}
}
int roll()
{
int sum = 0;
int number;
for(int i = 0; i < num; i++)
{
number = ((int)(Math.random() * 10000000))%sides + 1;
sum += number;
}
return sum;
}
}here is the class that makes the 2D array public class Board
{
char[][] ar = new char[10][10];
/*Creates a new instance of Board*/
public Board()
{
this.ar = ar;
this.setBoard();
}
void setBoard()
{
for (int r=0; r<ar.length; r++)
{
for (int c=0; c<ar[r].length; c++)
{
ar[r][c] = 'w';
System.out.print(" " + ar[r][c]);
}
System.out.println("");
}
}
}class Player
{
Board board = new Board();
/** Creates a new instance of Player */
public Player()
{
this.setBattleship();
}
void setBattleship()
{
int R = 0;
int C = 0;
int place = 0;
int pos = 0;
RandomNumber r = new RandomNumber(1, 10);
RandomNumber vh = new RandomNumber(1, 2);
BattleShip b = new BattleShip("BattleShip", 4, 0);
place = vh.roll()-1;
R = r.roll()-1;
C = r.roll()-1;
for(int i = 0; i < b.size; i++)
{
if(C > 10)
break;
if(R > 10)
break;
board.ar[R][C] = 'B';
if(place == 0)
{
C++;
}
else if(place == 1)
{
R++;
}
}
}
}here is the class that runs it all public class Play
{
public static void main(String[] args)
{
Player p = new Player();
p.setBattleship();
}
} |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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.Ü
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 347
Rep Power: 4
![]() |
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 Ship
{
String name;
int size;
int hits;
/** Creates a new instance of Ship */
public Ship(String name, int size, int hits)
{
this.name = name;
this.size = size;
this.hits = hits;
}
}here is the BattleShip class public class BattleShip extends Ship
{
/** Creates a new instance of BattleShip */
public BattleShip(String name, int size, int hits)
{
super(name, size, hits);
}
} |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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 Board
{
char[][] ar = new char[10][10];
/*Creates a new instance of Board*/
public Board()
{
this.ar = ar;
for (int r=0; r<ar.length; r++)
{
for (int c=0; c<ar[r].length; c++)
{
ar[r][c] = 'w';
}
}
// this.setBoard();
}
public void setBoard( char[][] arr )
{
for (int r=0; r<arr.length; r++)
{
for (int c=0; c<arr[r].length; c++)
{
System.out.print(" " + ar[r][c]);
}
System.out.println("");
}
}
//important bit
public char[][] getArray()
{
return ar;
}
}player.java class Player
{
Board board = new Board();
char[][] array;
/** Creates a new instance of Player */
public Player()
{
this.setBattleship();
}
void setBattleship()
{
array = board.getArray(); //important bit
int R = 0;
int C = 0;
int place = 0;
int pos = 0;
RandomNumber r = new RandomNumber(1, 10);
RandomNumber vh = new RandomNumber(1, 2);
BattleShip b = new BattleShip("BattleShip", 4, 0);
place = vh.roll()-1;
R = r.roll()-1;
C = r.roll()-1;
for(int i = 0; i < b.size; i++)
{
if(C > 10)
break;
if(R > 10)
break;
array[R][C] = 'B';
if(place == 0)
{
C++;
}
else if(place == 1)
{
R++;
}
}
board.setBoard( array ); //important bit
}
}lets 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. ![]()
__________________
There's got to be more to life than being really, really ridiculously good looking Last edited by Easter Bunny; Apr 28th, 2005 at 10:28 AM. |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 4
![]() |
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;
__________________
There's got to be more to life than being really, really ridiculously good looking |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|