Results 1 to 5 of 5
  1. #1
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default Not another Battleships post..

    OK i've read through the other Battleships assignments, and have not found my answer there (yet) so unfortunately have to post another thread :o

    For reference, the assignment is very similar to this one (CIT591 Saddle Points).

    As usual, all the classes and most of the methods are provided already; what's been provided must be used.

    I've made ok progress, but am stuck on the placing of the ships on the grid. Here's how the process has to work:

    1) There are three classes; Ocean (for a new game) Ship (generic ship class) and a number of ship sub-classes (Destroyer etc). BattleshipGame is the main method and calls the Ocean class to begin a new game.

    2) The Ocean class has a placeAllShipsRandomly method which passes randomly generated parameters to the generic Ship class placeShipAt method (actally passing to the Destroyer Sub-class which inherits the Ship placeShipAt method)

    3) The placeShipAt method needs to call the Ocean class getShipArray method and populate the array with 'D' or text representing Destroyer, but i'm stuck on this part.

    This Ocean Class

    Java Code:
    public class Ocean {
    
        Ship[][] ships = new Ship[10][10];
        int shotsFired;
        int hitCount;
        int shipsSunk;
    
        public Ocean() {
            EmptySea newEmptySea = new EmptySea();
            shotsFired = 0;
            hitCount = 0;
            shipsSunk = 0;
    
            for (int i = 0; i < ships.length; i++) {
                for (int j = 0; j < ships.length; j++) {
                    ships[i][j] = newEmptySea;
                }
            }
        }
    
        void placeAllShipsRandomly() {
            Random rand = new Random();
            
            /*
            int x = rand.nextInt(9);
            int y = rand.nextInt(9);
            boolean h = rand.nextBoolean();
            */
         
            Destroyer newDestroyer = new Destroyer();
            newDestroyer.placeShipAt(rand.nextInt(9), rand.nextInt(9), rand.nextBoolean(), this);
    
            /*
            ships[x][y] = newDestroyer;
            for (int i = 0; i < newDestroyer.length; i++) {
                if (h == false) {
                    ships[x+i][y] = newDestroyer;
                }
                if (h == true) {
                    ships[x][y+i] = newDestroyer;
                }
            }
            */
        }
    
        Ship[][] getShipArray() {
            return ships;
        }
    The Ship Class:

    Java Code:
    public class Ship {
    
        int bowRow;
        int bowColumn;
        int length;
        boolean horizontal;
        boolean[] hit = new boolean[4];
    
        int getLength() {
            return length;
        }
    
        int getBowRow() {
            return bowRow;
        }
    
        int getBowColumn() {
            return bowColumn;
        }
    
        boolean isHorizontal() {
            return horizontal;
        }
    
        void setBowRow(int row) {
            bowRow = row;
        }
    
        void setBowColumn(int column) {
            bowColumn = column;
        }
    
        void setHorizontal(boolean horizontal) {
            this.horizontal = horizontal;
        }
    
        String getShipType() {
            return "undefined";
        }
    
        void placeShipAt(int row, int column, boolean horizontal, Ocean ocean) {
            bowRow = row;
            bowColumn = column;
            this.horizontal = horizontal;
            ocean.getShipArray();
            //Destroyer newDestroyer = new Destroyer();
            //ocean.ships[row][column] = newDestroyer;
        }
    }
    So logically, the Ocean class method needs to generate random parameters, pass them to the Destroyer sub-class method placeShipAt which updates the Destroyer ship instance variables, then calls the Ocean Ships Array to update that with the Destroyer parameter.

  2. #2
    noobForever is offline Member
    Join Date
    Nov 2010
    Posts
    12
    Rep Power
    0

    Default

    If anyones interested; it turns out that on this one, the 'Ships' array needed to be Private, not public as above. This is what i couldn't work out; why the need to call the array in another method when i could access it directly via the ocean class. Having changed the array to private, the getShipArray method makes sense! So now my class looks like this:

    void placeShipAt(int row, int column, boolean horizontal, Ocean ocean) {
    //setting ship parameters
    setBowRow(row);
    setBowColumn(column);
    setHorizontal(horizontal);

    //get ships array and update locations
    ocean.getShipArray()[row][column] = this;
    for (int i = 0; i < this.length; i++) {
    if (horizontal == false) {
    ocean.getShipArray()[bowRow + i][bowColumn] = this;
    }
    if (horizontal == true) {
    ocean.getShipArray()[bowRow][bowColumn + i] = this;
    }
    }
    }


    ..which seems to work when called from the ocean class:

    void placeAllShipsRandomly() {

    //create new random object
    Random rand = new Random();

    //adding destroyer ship
    Destroyer newDestroyer = new Destroyer();
    System.out.println("Adding Ship: " + newDestroyer.getShipType());
    newDestroyer.placeShipAt(rand.nextInt(9), rand.nextInt(9), rand.nextBoolean(), this);

    }

    Ship[][] getShipArray() {
    return ships;
    }

    Onwards then..

  3. #3
    lostInJava is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Hiya, well done for figuring it out. How do you write the shootingAt method:

    boolean shootAt(int row, int column)?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    LostInJava -- you're far better off creating a new thread for your question rather than hijacking the original poster's thread.

  5. #5
    lostInJava is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    Sorry about that. you are right, just thought my question maybe relevant to above post. I will try with the new one.

    Thanks and sorry again!

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2009, 03:15 AM
  2. combo box help... plus hi this is my first post
    By okabeer in forum AWT / Swing
    Replies: 3
    Last Post: 04-06-2009, 06:05 PM
  3. my first post
    By kaviyasivashanmugham in forum New To Java
    Replies: 2
    Last Post: 03-13-2009, 09:50 AM
  4. First post out of the way..
    By sirwiggles in forum Introductions
    Replies: 0
    Last Post: 02-06-2009, 10:44 PM
  5. First post ever
    By pbpersson in forum Introductions
    Replies: 4
    Last Post: 08-16-2008, 05:30 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
  •