Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   random numbers in 2D array (http://www.programmingforums.org/showthread.php?t=3360)

cwl157 Apr 14th, 2005 5:06 PM

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.

Easter Bunny Apr 15th, 2005 2:59 AM

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.Ü

cwl157 Apr 15th, 2005 9:26 AM

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

Easter Bunny Apr 28th, 2005 9:10 AM

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. ;)

Easter Bunny Apr 29th, 2005 6:08 AM

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