Results 1 to 7 of 7
  1. #1
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default Unique data check in a 2d array using objects

    First and foremost, Hello java forums, I'm new here and I'm sure I'll have many questions regarding a project that I'm working on in the current weeks as I attempt to slim down, beautify and complete to my standards a project that I've been working on for a while.

    I have been asking a lot of questions in regards to objects lately, and I've just gotten a feel for objects but this still feels a little clunky. My issue right now is object placement in an array, and checking to see if that data is unique and within the bounds of the array (which I do not check for yet). This is sort of the culmination of all the problems leading up to this point.

    I have a 2d character array(10x10) that is going to be filled with '0', Each play "piece" is going to occupy a location on this 2d array.

    The shapes of the pieces are 2x1 (2 long, 1 wide), 3x1, 4x1, 5x1 the "orientation" below is whether or not they will be oriented vertically or horizontally on the array. My issue here is checking to see whether or not I can place an entire piece down without causing a collision(with another piece) on my grid or not.

    I've been playing around with different ways of doing this, for now I have this:
    Java Code:
    if (SelectedBoat.orientation==1){ // Is vertical
    for (j=0; j<SelectedBoat.health; j++){
    if (arrayPLAYER[SelectedBoat.location[0]][SelectedBoat.location[1]] !='0'){
    System.out.println("This piece collides at location: "+ arrayPLAYER[SelectedBoat.location[0]][SelectedBoat.location[1]];
    break;
    }
    SelectedBoat.location[1]++
    }
    
    }
    for (k=0; k<SelectedBoat.health; k++){
    arrayPLAYER[SelectedBoat.location[0]][SelectedBoat.location[1]] = SelectedBoat.letter; // Place Boat letter into the array
    SelectedBoat.location[1]++; // iterate the value
    }
    }
    My issue with this is that I'm not entirely sure if the first for loop is going to catch the issues that I need it to catch, or if it will iterate all the way through before the piece is placed....

    Is my logic on this right? Help! Should I nest the second for?!
    Last edited by sarevok9; 04-19-2011 at 08:36 PM. Reason: Changed idea slightly, changed question at the end.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Not quite sure what you're trying to achieve here... your code doesn't make much sense when theres no definition of SelectedBoat nor its members

  3. #3
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    I suppose I'll post my full code, warning though, it's a little on the long / incomplete side:

    Java Code:
    import java.lang.String;
    import java.util.*;
    
    class Boat{
    public int size;
    public int health;
    public int used;
    public int orientation;
    public char letter;
    public int[] location;
    public String name;
    
    public int[] BoatLoc (String location){
    
     int y = Character.toLowerCase(location.charAt(0))-97;
     int z= Character.toLowerCase(location.charAt(1))-49;
     int[] x = {y, z};
    return x;
    
    }//end public boats class//end class boats
    }
    
    public class ArrayTest {
    
    
    public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    //In the game, 0 will be unused, 1 will be 'ship occupied', 2 will be hit, 3 will be miss.
    
    
    //Make me some objects slave!!!
    //Patrol Boat
    Boat PatrolBoat = new Boat();
    PatrolBoat.health=2;
    PatrolBoat.name="Patrol Boat";
    PatrolBoat.letter='p';
    //Submarine
    
    Boat Submarine = new Boat();
    Submarine.health=3;
    Submarine.name="Submarine";
    Submarine.letter='s';
    
    //Destroyer
    Boat Destroyer = new Boat();
    Destroyer.health=4;
    Destroyer.name="Destroyer";
    Destroyer.letter='d';
    //Battleship
    Boat BattleShip = new Boat();
    BattleShip.health=5;
    BattleShip.name="BattleShip";
    BattleShip.letter='b';
    
    //SelectedBoat (empty boat class that gets points to array of boats below)
    Boat SelectedBoat;
    Boat[] boatarray = {PatrolBoat, Submarine, Destroyer, BattleShip}; // Boat array used in select
    
    
    int i=0, j=0, k=0, l, iSelect, iOrientation; //Variables
    //I,j,k,l Are all counter variables.
    Random generator = new Random(); // Create a random to be used later on. 
    
    
    //Generate and 0-fill player grid
    char[][] arrayPLAYER= new char[10][10];
    for (i=0; i<arrayPLAYER.length; i++){  // i is vertical, 
    for (j=0;j<arrayPLAYER[i].length; j++){ // j is Horizontal
    arrayPLAYER[i][j]='0';//
    }
    }
    
    
    
    //Generate and 0-fill CPU grid
    char[][] arrayCPU= new char[10][10];
    for (i=0; i<arrayCPU.length; i++){ 
    for (j=0;j<arrayCPU[i].length; j++){
    arrayCPU[i][j]= '0';//
    }
    }
    
    //Piece placement, sloppy as hell for now.
    for(i=0; i<4; i++){
        j=0; k=0; l=0;
        System.out.println("Select a ship that you would like to use\nPress 1 for a patrol boat(2 spaces) \nPress 2 for a Submarine( 3 spaces) \n"
                + "Press 3 for a destroyer(4 Spaces) \nPress 4 for a battleship (5 Spaces)");
        iSelect= input.nextInt();
        SelectedBoat = boatarray[iSelect-1];
        while(SelectedBoat.used >=1){
              System.out.println("This boat has already been used, select again");
              iSelect = input.nextInt();
              SelectedBoat = boatarray[iSelect-1];
        }
        SelectedBoat.used +=1;
        System.out.println("Would you like this ship to be horizontal or vertical? Press 1 for Horizontal, 2 for Vertical:  ");
        iOrientation=input.nextInt();
        SelectedBoat.orientation=iOrientation;
        System.out.println("Enter the space where you would like your piece to begin(A1 for example):  ");
        String c="";
        c = input.next();
        SelectedBoat.location = SelectedBoat.BoatLoc(c);
       if (SelectedBoat.orientation==1){ // If horizontal
    
    
           //Shitty attempt at collision detection here  This is what I need help with in general!
           for (j=0; j<SelectedBoat.health; j++){
               if (arrayPLAYER[SelectedBoat.location[0]][SelectedBoat.location[1]] !='0'){
               break;
               }
               SelectedBoat.location[1]++;
           }
           for (k=0; k<SelectedBoat.health; k++){
               arrayPLAYER[SelectedBoat.location[0]][SelectedBoat.location[1]] = SelectedBoat.letter; // Place Boat letter into the array
               SelectedBoat.location[1]++;
       }//Placement for
       }//End Boat Orientation If
       
    
    
    
    
    
    
    
          //Horizontal Orientation  -- Haven't done anything with this yes
          if (SelectedBoat.orientation==2){
           for (k=0; j<SelectedBoat.health; k++){
               arrayPLAYER[SelectedBoat.location[0]][SelectedBoat.location[1]] = SelectedBoat.letter;
               SelectedBoat.location[0]+=1;
           }
       }
    }
                
      
    //Player Grid Output.
    System.out.println("____________Player setup:____________\n"); // Top of the 'player positioning' box.
    System.out.println("         A B C D E F G H I J"); 
    System.out.println("");
    for (i=0;i<arrayPLAYER.length; i++){ // Outer loop of the 2d array output
    if (i< 9){
    System.out.print("|  "+ (i+1) +"     "); // Left side of the box
    }
    else{
        System.out.print("|  "+ (i+1) +"    ");
    }
    for (j=0; j<10; j++){ // Inner loop of array
    System.out.print(arrayPLAYER[i][j]+ " "); // Array output
    }// End inner for.
    System.out.println("       |"); // Right side of playermap box
    }//End outer for
    System.out.println("______________________________________"); // Formatting 
    //End player graph output.

    Hopefully that helps
    Last edited by sarevok9; 04-20-2011 at 02:43 AM. Reason: Added a couple comments to code.

  4. #4
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Long story short about the code: I'm recreating the game 'battleship' in text.

    I want to allow the player to be able to place ships and guess variables using the correct battleship notation (A1 = 0,0, e1 = 4,0 on the grid) Hence the boatloc method.

  5. #5
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Oh, so I understand that your problem is allowing each square of a boat to be placed first before you check for a collision?

  6. #6
    sarevok9 is offline Member
    Join Date
    Apr 2011
    Posts
    37
    Rep Power
    0

    Default

    Quote Originally Posted by ozzyman View Post
    Oh, so I understand that your problem is allowing each square of a boat to be placed first before you check for a collision?
    My issue is actually checking to see if there is something there before placing it and then placing it in the right spot using the code that I have in there, the code that I've been trying to revise with little to no luck is line 103-111 if you drop that into an IDE.

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Okay, so you need general help with your collision detection...

    you have a 2D board of squares which you place boats onto.

    when placing a boat you need a method to get all of the squares occupied by that boat.

    here's an example code:

    Java Code:
    public class CoOrds {
        public int x;
        public int y;
        CoOrds(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    
    public class Boat {
        private int posX;
        private int posY;
        private int width;
        ...
        public List<CoOrds> getOccupiedSquares() {
            /* the squares occupied are calculated by the X,Y start position until the X width */
            List<CoOrds> coOrds = new ArrayList<>();
            for (int i=posX; i<width; i++) coOrds.add(new CoOrds(i,posY));
            return coOrds;
        }
    }

    Once you have all of the squares (by x,y co-ordinates of the board) that the boat occupies, it should be easier to check if boats are colliding (if any two boats have the same coordinate - that is a collision).

Similar Threads

  1. find the unique values of a double array
    By tyang in forum New To Java
    Replies: 3
    Last Post: 09-11-2011, 03:47 PM
  2. Array of Arrays all unique elements
    By dusker in forum New To Java
    Replies: 9
    Last Post: 04-02-2011, 08:21 PM
  3. Arbitrary number of objects, each with a unique name.
    By providence in forum New To Java
    Replies: 8
    Last Post: 02-23-2011, 02:12 AM
  4. Check for unique key violation
    By andy16 in forum JDBC
    Replies: 6
    Last Post: 05-24-2010, 07:23 PM
  5. Unique element in an array
    By revathi17 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 09:44 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
  •