3 Replies - 663 Views - Last Post: 13 February 2018 - 02:00 PM Rate Topic: -----

#1 Lylio   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-February 18

Respawn moving objects on 2D array

Posted 13 February 2018 - 01:31 PM

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:

Posted Image

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();
        }
    }

}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Respawn moving objects on 2D array

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15884
  • View blog
  • Posts: 63,571
  • Joined: 12-June 08

Re: Respawn moving objects on 2D array

Posted 13 February 2018 - 01:40 PM

I would figure your loop would have a part where it takes the 'end of the grid' (in how ever you determine that given your code), and compares the vehicle's position. If they are the same then reinstantiate/start or reset the vehicle object.
Was This Post Helpful? 0
  • +
  • -

#3 Lylio   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-February 18

Re: Respawn moving objects on 2D array

Posted 13 February 2018 - 01:57 PM

modi123, thanks for the reply. Yeah, that makes sense, and conceptually I understand that the solution requires a new car object to be instantiated. In the Grid class, there is the method:

    public void startVehicles() {
        Thread t = new Thread(v1);
        t.start();

        Thread t2 = new Thread(v2);
        t2.start();
    }


and this starts the first two vehicles moving - do you think this would be the method to focus on, perhaps try and create a loop to continuously call it? 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.

Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15884
  • View blog
  • Posts: 63,571
  • Joined: 12-June 08

Re: Respawn moving objects on 2D array

Posted 13 February 2018 - 02:00 PM

If that is causing a logical problem then it is a decent sign that may be the wrong spot for it, or at worst, the wrong setup.

Why not have a method called 'start' where it takes in a vehicle object, adds it to a thread, and starts it? That is reasonably more flexible, right?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1