Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 14th, 2005, 6:06 PM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 391
Rep Power: 13 cwl157 is on a distinguished road
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("");
      }
    }
}
here is the class that is supposed to randomly set a row or column to 'B'
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();
        
    }
    
}
I hope someone can decifer all that ive put here. I barely know what i am doing with this so any help is really apreciated.
cwl157 is offline   Reply With Quote
Old Apr 15th, 2005, 3:59 AM   #2
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 13 Easter Bunny is on a distinguished road
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
Easter Bunny is offline   Reply With Quote
Old Apr 15th, 2005, 10:26 AM   #3
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 391
Rep Power: 13 cwl157 is on a distinguished road
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);
    }
    
}
thanks
cwl157 is offline   Reply With Quote
Old Apr 28th, 2005, 10:10 AM   #4
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 13 Easter Bunny is on a distinguished road
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.
Easter Bunny is offline   Reply With Quote
Old Apr 29th, 2005, 7:08 AM   #5
Easter Bunny
Programmer
 
Easter Bunny's Avatar
 
Join Date: Mar 2005
Location: different places. constantly on the run.
Posts: 57
Rep Power: 13 Easter Bunny is on a distinguished road
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
Easter Bunny is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:53 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC