Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Respawn moving objects on 2D array

  1. #1
    Junior Member
    Join Date
    Nov 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Respawn moving objects on 2D array

    I have a 2D array grid on which a couple of runnable vehicle objects (an 'x' and an 'o') are randomly placed. The grid repeatedly prints to console, and the x/y coordinates of the vehicle objects updates on their threads which gives the impression of the vehicles animating across the grid-lanes.

    However, I'm stuck on how to make the vehicle objects respawn once they've reached the end of the grid. Any ideas how I can achieve this? Here's a screen shot and the code:

    grid.png

    public class gridMain {
     
        public static void main(String[] args) {
     
     
            Grid grid = new Grid(10, 20);
     
            Vehicle1 v1 = new Vehicle1(grid);
            Vehicle2 v2 = new Vehicle2(grid);
            grid.setVehicles(v1, v2);
     
     
            while(true) {
     
                grid.printGrid();
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
     
     
        }
    }
     
     
    public class Grid {
     
         //Grid width and height
        private int rows;
        private int columns;
        private String[][] gridArray;
        Vehicle1 v1;
        Vehicle2 v2;
     
     
    public Grid(int rows, int columns) {
        this.rows = rows;
        this.columns = columns;
        gridArray = new String[rows][columns];
    }
     
    public int getRows() {
        return rows;
    }
     
    public int getColumns() {
        return columns;
    }
     
    public void setVehicles(Vehicle1 v1, Vehicle2 v2) {
        this.v1 = v1;
        this.v2 = v2;
        startVehicles();
    }
     
    public void startVehicles() {
        Thread t = new Thread(v1);
        t.start();
     
        Thread t2 = new Thread(v2);
        t2.start();
    }
     
     
     
    public void printGrid() {
     
     
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                if (i == v1.getX() && j == v1.getY()) {
                    gridArray[v1.getX()][v1.getY()] = "o|";
     
                } else if (i == v2.getX() && j == v2.getY()) {
                    gridArray[v2.getX()][v2.getY()] = "x|";
     
                } else {
                        gridArray[i][j] = " |";
                    }
                }
            }
     
        System.out.println();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                System.out.print(gridArray[i][j]);
            }
            System.out.println();
       }
    }
     
     
    }
     
    public class Vehicle1 implements Runnable {
     
    private int x; //Horizontal coordinate of vehicle
    private int y; //Vertical coordinate of vehicle
    private Grid g;
    private int speed;
     
     
    public Vehicle1 (Grid g) {
        int max = g.getRows();
        this.x = (int)(Math.random() * (max));
        this.y = 0;
        this.g = g;
        this.speed = (int) (200 + (600 - 200 ) * Math.random());
    }
     
    public int getX() {
    return x;
    }
     
    public int getY() {
        return y;
    }
     
    @Override
    public void run() {
     
     
        for(int i = y; i < g.getColumns(); i++) {
            y++;
            try {
                Thread.sleep(speed);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
     
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Respawn moving objects on 2D array

    Where is the definition for the class: Vehicle2?

    Why are there 2 Vehicle classes? Why not have 2 instances of the same class?

    respawn once they've reached the end of the grid
    What does respawn mean?
    Change direction of movement?
    Start moving from a new location?
    Or what???
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Respawn moving objects on 2D array

    Hi, thanks for the reply. It's very similar to Vehicle1 - I was going to try and understand the respawning problem then probably tidy everything with a Vehicle superclass. I'm kind of at the rough-sketching stage of this puzzle, eventually I'd like lots of lockable vehicle threads letting each other cross the intersecting lanes. By respawn, I mean a new instantiation of a Vehicle object - in the Vehicle class is a random generator which chooses a random lane for the Vehicle objects to start in. I'm trying to figure out a loop which would continuously create new Vehicle objects:

    public class Vehicle2 implements Runnable {
     
        private int x; //Horizontal coordinate of vehicle
        private int y; //Vertical coordinate of vehicle
        private Grid g;
        private int speed;
     
        public Vehicle2 (Grid g) {
            int max = g.getRows();
            this.x = 0;
            this.y = (int)(Math.random() * (max));
            this.g = g;
            this.speed = (int) (200 + (600 - 200 ) * Math.random());
        }
     
        public int getX() {
            return x;
        }
     
        public int getY() {
            return y;
        }
     
        @Override
        public void run() {
            for(int i = x; i < g.getColumns(); i++) {
                x++;
                try {
                    Thread.sleep(speed);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    Last edited by Lylio; February 13th, 2018 at 04:01 PM. Reason: Clarifying my issue

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Respawn moving objects on 2D array

    Is there a max number of vehicles that can be running?

    The current program is hard coded to have 2 vehicles. Change it to use a list that holds the current live vehicles.
    If you want to spawn a new vehicle when a vehicle reaches the edge, create the new vehicle and add it to the list and remove the vehicle that got to the edge.

    Also posted at: https://coderanch.com/t/690676/java/...-array#3242094
    and here: http://www.dreamincode.net/forums/to...s-on-2d-array/
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Objects - Moving Shapes in JAVA HELP PLEASE!
    By imzy in forum Object Oriented Programming
    Replies: 1
    Last Post: October 10th, 2013, 07:08 PM
  2. collision detection moving objects
    By nichodiaz in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 14th, 2013, 02:21 AM
  3. Moving Two Objects at Once
    By BellarmineSeattle in forum Java Theory & Questions
    Replies: 2
    Last Post: January 23rd, 2013, 04:44 PM
  4. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 08:03 AM
  5. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM