Help (some cars in a row)

edited October 2013 in How To...

hello everyone, I have a problem. I have to make a program that moves some cars in a row, the fermane face in front of a petrol station and after a number of seconds re-started. A machine at a time. I do not know how to do!

Answers

  • I suggest you first try with making sketch which moves circle across the screen.

    Show us your code and we'll help you progress further.

  • edited October 2013

    this is the code

    // Partenza da 0
    float x = 0;
    int actualSecs;
    int actualMins;
    int startSec = 0; 
    int startMin = 0; 
    int scrnSecs; 
    int scrnMins=0;
    int restartSecs=0;
    int restartMins=0;
    
    
    void setup() {
    size(200,200);
    }
    
    void draw() {
    background(255);
    drawTimer();
    timer();
    // Visualizza macchinina
    fill(0);
    rect(x,100,20,20);
    // Incrementa x
    x = x + 1;
    if (x >= width/2) {
    x = x - 1;
    }
    if (scrnSecs == 8){
     fill(0);
    rect(x,100,20,20);
    // Incrementa x
    x = x + 1;
    } 
    
    }
    void timer() {
      actualSecs = millis()/1000; //convert milliseconds to seconds, store values.
      actualMins = millis() /1000 / 60; //convert milliseconds to minutes, store values.
      scrnSecs = actualSecs - restartSecs; //seconds to be shown on screen
      scrnMins = actualMins - restartMins; //minutes to be shown on screen
    }
    
    void drawTimer() {
      text(nf(scrnMins, 2) + " : " + nf(scrnSecs, 2), 20, 20);
    }
    
  • Answer ✓

    Now it should go one be one.

    // Partenza da 0
    float x = 0;
    int actualSecs;
    int actualMins;
    int startSec = 0; 
    int startMin = 0; 
    int scrnSecs; 
    int scrnMins=0;
    int restartSecs=0;
    int restartMins=0;
    
    
    void setup() {
      size(200, 200);
    }
    
    void draw() {
      background(255);
      drawTimer();
      timer();
      // Visualizza macchinina
      fill(0);
      rect(x, 100, 20, 20);
      // Incrementa x
      x = x + 1;
      if (x >= width/2) {
        x = x - 1;
      }
      if (scrnSecs == 8) {
        fill(0);
        rect(x, 100, 20, 20);
        // Incrementa x
        x = x + 1;
      }
    
      if ( scrnSecs > 10 ){
         timer();
          restartSecs = actualSecs;
          restartMins = actualMins;
          x = 0;
      }
    }
    void timer() {
      actualSecs = millis()/1000; //convert milliseconds to seconds, store values.
      actualMins = millis() /1000 / 60; //convert milliseconds to minutes, store values.
      scrnSecs = actualSecs - restartSecs; //seconds to be shown on screen
      scrnMins = actualMins - restartMins; //minutes to be shown on screen
    }
    
    void drawTimer() {
      text(nf(scrnMins, 2) + " : " + nf(scrnSecs, 2), 20, 20);
    }
    
  • edited October 2013

    is not correct. My program should simulate a queue of cars which stops 170 px and again after a few seconds. machines must form a queue, and the number of machines is chosen by the user

  • Answer ✓

    (i've renamed this question. "Help" is too vague)

  • nobody? :(

  • edited October 2013

    my new code. my problem is that where there is the triangle must stop all, while I overlap why?

    final int NEW_CARS = 7 ;
    Car[] carList = new Car[NEW_CARS];
    
    void setup () {
      size(700, 800);
      for (int i = 0; i < NEW_CARS; ++i) {
        carList[i] = new Car(color(0,150,200), /* x*/i*40, /* y */60, /*velocità*/1);
      }
    }
    
    void draw () {
      background (255);
      for (int i = 0; i < NEW_CARS; ++i) {
        carList[i].move(i);
        carList[i].display();
      } 
    
    }
    
    class Car {
      color c;
      float x;
      float y;
      float speed;
      int name;
      Car (color superc, float superx, float supery, float superspeed) {
    
        c = superc;
        x = superx;
        y = supery;
        speed = superspeed;
      }
    
      void display () {
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(x, y, 30, 20);
        triangle(400,50,450,50,425,25);
      }
    
    
      void move(int i) {
       x = x + speed;
        for(int j=0;j<NEW_CARS;j++){
          if(x==width/2  ){  // pixel dove si deve fermare+ 250 + larghezza della macchina
            speed=0;
    
          }
        }
      }
    }
    
  • the cars are individual objects - they don't know about the other cars. so they all continue until they reach the triangle. this isn't easy to fix. each car will need to know the position of the car in front.

    line 46 controls where they stop.

    but there is nothing that makes them start again.

  • how can I do? help me

  • edited October 2013

    Here's some imrovements for your code:

    You have to select proper abstraction level for your cars. In the example below: the first car is moving towards the petrol station. And other cars are just following it.

    final int NEW_CARS = 7 ;
    Car[] carList = new Car[NEW_CARS];
    
    void setup () {
      size(700, 800);
      // we fill array backwards. the leftmost car will be the last in array, and the rightmost car will be the first.
      int j = 0;
      for ( int i = NEW_CARS-1; i >= 0 ; i--) {
        carList[j++] = new Car(color(0,150,200), i*40, 60, 1);
      }
    }
    
    void draw () {
      background (255);
    
      // first car leads and moves
      carList[0].move(-1);
    
      // the other cars just follow each other (and the first car)
      for (int i = 1; i < NEW_CARS; ++i) {
        carList[i].follow(carList[i-1]);
      }
    
    
      for (int i = 0; i < NEW_CARS; ++i) {
        carList[i].display();
      }  
    
    }
    
    class Car {
      color c;
      float x;
      float y;
      float speed;
      int name;
      float distanceBetweenCarsAlongXAxis = 35; // px
    
      Car (color superc, float superx, float supery, float superspeed) {
    
        c = superc;
        x = superx;
        y = supery;
        speed = superspeed;
      }
    
      void display () {
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(x, y, 30, 20);
        triangle(400,50,450,50,425,25);
      }
    
    
      void move(int i) {
       x = x + speed;
        for(int j=0;j<NEW_CARS;j++){
          if(x==width/2  ){  // pixel dove si deve fermare+ 250 + larghezza della macchina
            speed=0;
    
          }
        }
      }
    
      /**
      * Follows next car keeping the given distance in pixels.
      */
      void follow(Car car){
          if ( car.x - x > distanceBetweenCarsAlongXAxis ){
              move(-1);
          }
      }
    }
    
  • this was the result I obtained also in the code I posted above. Now the question is: how do I restart only the first car and make the second stop, then start the second, and then stop the third and so on?

  • well, think about it.

    the car needs to move forwards unless it is stopped at the station OR the car in front has stopped. so you just need a condition that checks both those things. and another that starts it moving again after it's been at the station long enough.

  • machines must always stop for a while at the station, one at a time, then leave. The first stops, then starts again (after a certain time) and stops the second, then starts again (after a certain time) and stops the third, etc. ..

  • edited October 2013

    I tried to insert a timer and, after 4 seconds, do rispartire the first machine but nothing works!!

     void move(int i) {
       x = x + speed;
        for(int j=0;j<NEW_CARS;j++){
          if(x==width/2  ){ 
            speed=0;
            if (scrnSecs==4)
              x=x+1;
          }
        }
      }
    
  •   void move(int i) {
       x = x + speed;
        for(int j=0;j<NEW_CARS;j++){
          if(x==width/2  ){  // pixel dove si deve fermare+ 250 + larghezza della macchina
            speed=0;
    
          }
        }
      }
    

    I don't understand the point of the loop in this method. neither j nor NEW_CARS are used.

    About scrnSecs: we don't know what is this variable, how it is managed / changed, so it is hard to tell why it doesn't work...

  • edited October 2013

    this is the code with the timer activated by me. The problem is that all the cars stop, but after that you are the first stops should go away and then stop the second (for 3 seconds), then the second must start and stop the third (for 3 seconds), and so on.

        // timer
    int actualSecs; //actual seconds elapsed since start
    int actualMins; //actual minutes elapsed since start
    int startSec = 0; //used to reset seconds shown on screen to 0
    int startMin = 0; //used to reset minutes shown on screen to 0
    int scrnSecs; //seconds displayed on screen (will be 0-60)
    int scrnMins=0; //minutes displayed on screen (will be infinite)
    int restartSecs=0; //number of seconds elapsed at last click or 60 sec interval
    int restartMins=0; //number of seconds ellapsed at most recent minute or click
    //
    
    final int NEW_CARS = 7 ;
        Car[] carList = new Car[NEW_CARS];
    
        void setup () {
          size(700, 800);
          // we fill array backwards. the leftmost car will be the last in array, and the rightmost car will be the first.
          int j = 0;
          for ( int i = NEW_CARS-1; i >= 0 ; i--) {
            carList[j++] = new Car(color(0,150,200), i*40, 60, 1);
          }
        }
    
        void draw () {
          background (255);
    
          // first car leads and moves
          carList[0].move(-1);
    
          // the other cars just follow each other (and the first car)
          for (int i = 1; i < NEW_CARS; ++i) {
            carList[i].follow(carList[i-1]);
          }
    
    
          for (int i = 0; i < NEW_CARS; ++i) {
            carList[i].display();
          } 
    
        }
    
        class Car {
          color c;
          float x;
          float y;
          float speed;
          int name;
          float distanceBetweenCarsAlongXAxis = 35; // px
    
          Car (color superc, float superx, float supery, float superspeed) {
    
            c = superc;
            x = superx;
            y = supery;
            speed = superspeed;
          }
    
          void display () {
            stroke(0);
            fill(c);
            rectMode(CENTER);
            rect(x, y, 30, 20);
            triangle(400,50,450,50,425,25);
          }
    
    
          void move(int i) {
           x = x + speed;
            for(int j=0;j<NEW_CARS;j++){
              if(x==width/2  ){ 
                speed=0;
    
              }
            }
          }
    
          /**
          * Follows next car keeping the given distance in pixels.
          */
          void follow(Car car){
              if ( car.x - x > distanceBetweenCarsAlongXAxis ){
                  move(-1);
              }
          }
        }
    
    void drawTimer(){
        text(nf(scrnMins, 2) + " : " + nf(scrnSecs, 2), 20, 20);
    }
    void timer() {
      actualSecs = millis()/1000; //convert milliseconds to seconds, store values.
      actualMins = millis() /1000 / 60; //convert milliseconds to minutes, store values.
      scrnSecs = actualSecs - restartSecs; //seconds to be shown on screen
      scrnMins = actualMins - restartMins; //minutes to be shown on screen
    }
    
  • Better have the timer built into the car, since each car stops for a given time.

    // timer
    int actualSecs; //actual seconds elapsed since start
    int actualMins; //actual minutes elapsed since start
    int startSec = 0; //used to reset seconds shown on screen to 0
    int startMin = 0; //used to reset minutes shown on screen to 0
    int scrnSecs; //seconds displayed on screen (will be 0-60)
    int scrnMins=0; //minutes displayed on screen (will be infinite)
    int restartSecs=0; //number of seconds elapsed at last click or 60 sec interval
    int restartMins=0; //number of seconds ellapsed at most recent minute or click
    //
    
    final int CAR_NB = 7 ;
    Car[] carList = new Car[CAR_NB];
    
    void setup () {
      size(700, 800);
      // we fill array backwards. the leftmost car will be the last in array, and the rightmost car will be the first.
      int j = 0;
      for (int i = CAR_NB-1; i >= 0 ; i--) {
        carList[j++] = new Car(color(0, 150, 200), i*40, 60, 1);
      }
    }
    
    void draw () {
      background (255);
    
      // first car leads and moves
      carList[0].move();
    
      // the other cars just follow each other (and the first car)
      for (int i = 1; i < CAR_NB; ++i) {
        carList[i].follow(carList[i-1]);
      }
    
    
      for (int i = 0; i < CAR_NB; ++i) {
        carList[i].display();
      }
    }
    
    class Car {
      color c;
      float x;
      float y;
      float speed;
      int name;
      float distanceBetweenCarsAlongXAxis = 35; // px
      int stopTime;
      boolean filled; // Gas tank is filled, continue
      final int WAIT_TIME = 4000; // 4 seconds
    
      Car (color superc, float superx, float supery, float superspeed) {
        c = superc;
        x = superx;
        y = supery;
        speed = superspeed;
      }
    
      void display () {
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(x, y, 30, 20);
        triangle(400, 50, 450, 50, 425, 25);
      }
    
    
      void move() {
        if (stopTime > 0) {
          if (millis() - stopTime > WAIT_TIME) {
            stopTime = 0;
            filled = true;
          } else {
            return; // No move
          }
        }
        x += speed;
        if (!filled && x >= width/2) {
          stopTime = millis();
        }
      }
    
      /**
       * Follows next car keeping the given distance in pixels.
       */
      void follow(Car car) {
        if ( car.x - x > distanceBetweenCarsAlongXAxis ) {
          move();
        }
      }
    }
    
  • there is a problem though .. the timer must be visible and the machines must stay still for a certain time each different for each machine. The time is entered by the user into a variable.

  • Then it is perfect! Just change WAIT_TIME (constant) to waitTime (variable, remove the final part of the declaration, and you have your wait time personalized by car.

    Your visible / global timer can loop on the cars and ask them their stopTime value. If not zero, just display this stopTime less millis() as the global timer value.

  • Example:

    // timer
    int actualSecs; //actual seconds elapsed since start
    int actualMins; //actual minutes elapsed since start
    int startSec = 0; //used to reset seconds shown on screen to 0
    int startMin = 0; //used to reset minutes shown on screen to 0
    int scrnSecs; //seconds displayed on screen (will be 0-60)
    int scrnMins=0; //minutes displayed on screen (will be infinite)
    int restartSecs=0; //number of seconds elapsed at last click or 60 sec interval
    int restartMins=0; //number of seconds ellapsed at most recent minute or click
    //
    
    final int CAR_NB = 7 ;
    Car[] carList = new Car[CAR_NB];
    
    void setup () {
      size(700, 800);
      // we fill array backwards. the leftmost car will be the last in array, and the rightmost car will be the first.
      int j = 0;
      for (int i = CAR_NB-1; i >= 0 ; i--) {
        carList[j++] = new Car(color(0, 150, 200), i*40, 60, 1, int(random(1000, 5000)));
      }
    }
    
    void draw () {
      background (255);
    
      // first car leads and moves
      carList[0].move();
    
      // the other cars just follow each other (and the first car)
      for (int i = 1; i < CAR_NB; ++i) {
        carList[i].follow(carList[i-1]);
      }
    
    
      for (int i = 0; i < CAR_NB; ++i) {
        carList[i].display();
      }
    
      drawTimer();
    }
    
    class Car {
      color c;
      float x;
      float y;
      float speed;
      int name;
      float distanceBetweenCarsAlongXAxis = 35; // px
      int stopTime;
      boolean filled; // Gas tank is filled, continue
      int waitTime = 4000; // 4 seconds
    
      Car(color superc, float superx, float supery, float superspeed, int wait) {
        c = superc;
        x = superx;
        y = supery;
        speed = superspeed;
        waitTime = wait;
      }
    
      void display () {
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(x, y, 30, 20);
        triangle(400, 50, 450, 50, 425, 25);
      }
    
    
      void move() {
        if (stopTime > 0) {
          if (millis() - stopTime > waitTime) {
            stopTime = 0;
            filled = true;
          } else {
            return; // No move
          }
        }
        x += speed;
        if (!filled && x >= width/2) {
          stopTime = millis();
        }
      }
    
      /**
       * Follows next car keeping the given distance in pixels.
       */
      void follow(Car car) {
        if ( car.x - x > distanceBetweenCarsAlongXAxis ) {
          move();
        }
      }
    }
    
    void drawTimer() {
      float timerValue = 0;
      for (int i = 0; i < CAR_NB; ++i) {
        if (carList[i].stopTime > 0) {
          timerValue = (millis() - carList[i].stopTime) / 1000.0;
          break; // No need to ask other cars
        }
      }
      if (timerValue > 0) {
        text(nf(timerValue, 1, 2), 20, 20);
      }// else text(nf(timerValue, 1, 2), 20, 50);
    }
    

    Not perfect, but see this as a starting point.

  • to save every single timer value for each car I have to create an array?

  • Look at my code: each car has its own timer already!

    Here is a slightly better version.

    One puzzling thing: why, if I add the last else in the code, it works better? It shouldn't have any incidence!

    // timer
    int actualSecs; //actual seconds elapsed since start
    int actualMins; //actual minutes elapsed since start
    int startSec = 0; //used to reset seconds shown on screen to 0
    int startMin = 0; //used to reset minutes shown on screen to 0
    int scrnSecs; //seconds displayed on screen (will be 0-60)
    int scrnMins=0; //minutes displayed on screen (will be infinite)
    int restartSecs=0; //number of seconds elapsed at last click or 60 sec interval
    int restartMins=0; //number of seconds ellapsed at most recent minute or click
    //
    
    final int CAR_NB = 7 ;
    Car[] carList = new Car[CAR_NB];
    
    void setup () {
      size(700, 800);
      // we fill array backwards. the leftmost car will be the last in array, and the rightmost car will be the first.
      int j = 0;
      for (int i = CAR_NB-1; i >= 0 ; i--) {
        carList[j++] = new Car(color(0, 150, 200), 
            i * 40 + int(random(20, 50)), 60, 1, 
            int(random(1000, 5000)));
      }
    }
    
    void draw () {
      background (255);
    
      // first car leads and moves
      carList[0].move();
    
      // the other cars just follow each other (and the first car)
      for (int i = 1; i < CAR_NB; ++i) {
        carList[i].follow(carList[i-1]);
      }
    
    
      for (int i = 0; i < CAR_NB; ++i) {
        carList[i].display();
    //    println("Car " + i + " - " + carList[i].x + " " + (carList[i].stopTime == 0 ? "" : millis() - carList[i].stopTime));
      }
    
      drawTimer();
    
      // Not associated to cars!
      int x = width / 2;
      triangle(x - 25, 50, x + 25, 50, x, 25);
    }
    
    class Car {
      color c;
      float x;
      float y;
      float speed;
      int name;
      float distanceBetweenCarsAlongXAxis = 35; // px
      int stopTime;
      boolean filled; // Gas tank is filled, continue
      int waitTime;
    
      Car(color superc, float superx, float supery, float superspeed, int wait) {
        c = superc;
        x = superx;
        y = supery;
        speed = superspeed;
        waitTime = wait;
      }
    
      void display () {
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(x, y, 30, 20);
      }
    
      void move() {
        if (stopTime > 0) {
          if (millis() - stopTime > waitTime) {
            stopTime = 0;
            filled = true;
          } else {
            return; // No move
          }
        }
        x += speed;
        if (!filled && x >= width / 2) {
          stopTime = millis();
        }
      }
    
      /**
       * Follows next car keeping the given distance in pixels.
       */
      void follow(Car car) {
        if (car.x - x > distanceBetweenCarsAlongXAxis) {
          move();
        }
      }
    }
    
    void drawTimer() {
      float timerValue = 0;
      float max = 0;
      for (int i = 0; i < CAR_NB; ++i) {
        if (carList[i].stopTime > 0) {
          timerValue = (millis() - carList[i].stopTime) / 1000.0;
          max = carList[i].waitTime / 1000.0;
          break; // No need to ask other cars
        }
      }
      if (timerValue > 0) {
        text(nf(timerValue, 1, 1) + " / " + nf(max, 1, 1), 20, 20);
      } else text(nf(timerValue, 1, 1) + " / " + nf(max, 1, 1), 20, 40);
    }
    
  • yes, I saw that every car has its own time, but I need a something that I contains all times, because then I have to order cars from one that is less firm than that which is more firm

  • edited October 2013

    I suggest to study the code I show, because it probably does what you want, or easily allow it.

    I don't know why you need to sort cars, nor what you mean by "less firm" or "firmer".

    Latest improvements:

    // timer
    int actualSecs; //actual seconds elapsed since start
    int actualMins; //actual minutes elapsed since start
    int startSec = 0; //used to reset seconds shown on screen to 0
    int startMin = 0; //used to reset minutes shown on screen to 0
    int scrnSecs; //seconds displayed on screen (will be 0-60)
    int scrnMins=0; //minutes displayed on screen (will be infinite)
    int restartSecs=0; //number of seconds elapsed at last click or 60 sec interval
    int restartMins=0; //number of seconds ellapsed at most recent minute or click
    //
    
    final int CAR_NB = 7 ;
    Car[] carList = new Car[CAR_NB];
    
    void setup () {
      size(1000, 200);
      textSize(10);
    
      // we fill array backwards. the leftmost car will be the last in array, and the rightmost car will be the first.
      int pos = 0;
      for (int i = CAR_NB-1; i >= 0 ; i--) {
        carList[i] = new Car("C" + i,
            color(0, 150, 200), 
            pos, 60, 1, 
            int(random(1000, 5000)));
        pos += 30 + int(random(20, 50));
      }
    }
    
    void draw () {
      background (255);
    
      // first car leads and moves
      carList[0].move();
    
      // the other cars just follow each other (and the first car)
      for (int i = 1; i < CAR_NB; ++i) {
        carList[i].follow(carList[i-1]);
      }
    
    
      for (int i = 0; i < CAR_NB; ++i) {
        carList[i].display();
    //    println("Car " + i + " - " + carList[i].x + " " + (carList[i].stopTime == 0 ? "" : millis() - carList[i].stopTime));
      }
    
      drawTimer();
    
      // Not associated to cars!
      int x = width / 2;
      fill(#779900);
      triangle(x - 25, 50, x + 25, 50, x, 25);
    }
    
    class Car {
      color c;
      float x;
      float y;
      float speed;
      String name;
      float distanceBetweenCarsAlongXAxis = 35; // px
      int stopTime;
      boolean filled; // Gas tank is filled, continue
      int waitTime;
    
      Car(String supername, color superc, float superx, float supery, float superspeed, int wait) {
        name = supername;
        c = superc;
        x = superx;
        y = supery;
        speed = superspeed;
        waitTime = wait;
      }
    
      void display () {
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(x, y, 30, 20);
        String state = "M"; // Moving
        if (filled) state = "F";
        else if (stopTime > 0) state = "W"; // Wait
        fill(255);
        text(name + " " + state, x - 10, y);
      }
    
      void move() {
        if (stopTime > 0) {
          if (millis() - stopTime > waitTime) {
            stopTime = 0;
            filled = true;
          } else {
            return; // No move
          }
        }
        x += speed;
        if (!filled && x >= width / 2) {
          stopTime = millis();
        }
      }
    
      /**
       * Follows next car keeping the given distance in pixels.
       */
      void follow(Car car) {
        if (car.x - x > distanceBetweenCarsAlongXAxis) {
          move();
        }
      }
    
      public String toString() { return name; }
    }
    
    void drawTimer() {
      String name = "";
      float timerValue = 0;
      float max = 0;
      for (int i = 0; i < CAR_NB; ++i) {
        if (carList[i].stopTime > 0) {
          timerValue = (millis() - carList[i].stopTime) / 1000.0;
          max = carList[i].waitTime / 1000.0;
          name = carList[i].name;
          break; // No need to ask other cars
        }
      }
      if (timerValue > 0) {
        fill(#FF0000);
        text(name + " - " + nf(timerValue, 1, 1) + " / " + nf(max, 1, 1), 20, 20);
      }
    }
    
  • I intend to order the car to take those with less time and make them go head to tail and those with more time in the queue. ;)

  • Then you need to look at the Comparable interface. There are some examples of implementation in the old forums.

  • thank you very much

  • one last question, to start all machines from "behind" the screen as you do? Or rather, the car must come from the left appearing on the screen, but not starting directly on the screen appearing.

  • The simplest way to do this is to add a large negative number to their starting x position...

  • in this way, however, do not return the values ​​used for the x of the machines

  • I don't understand your last remark.

  • edited October 2013
    • If I move the departure of the car behind the screen does not work any more downtime.
    • I can not find where they are saved the time every time
  • edited October 2013
    • I took my last code and replaced the line int pos = 0; with int pos = -1000; and after some seconds, I saw the cars coming in. Not sure what you tried to do.
    • You can also adjust the line pos += 30 + int(random(20, 50)); to increase their interval. For example, pos += 50 + int(random(10, 100)); makes the cars to appear earlier and to have less regular intervals.
    • See the fields (variables) waitTime (time they will wait) and stopTime (time stamp set when they start to wait).
  • thank you !!

  • edited October 2013

    I'm sorry but I have a new problem. I have a file which is occupied by the waiting times of the machines. I modified the code so

    void setup(){
      size(850,637);
      stato = statoMenu;
      sfondoMenu=loadImage("sfondomenu.jpg");
      macchina=loadImage("macchina.png");
      menuFont=loadFont("myfont.vlw");
      font = loadFont("font.vlw");
      file=loadStrings("simulazione.txt"); //apertura del file
      p = file.length; //lunghezza del file
      //Divisione del file
      for (int i = 0; i < p; i++){
        valori = int(split(file[i],","));
        attesa = valori[1];
        println(" nome: "+valori[0]+" ts: " +valori[1]);
      }
      primaMacchina = new Macchina[p];
      for(int i = p - 1; i >= 0; i--){
        float y=100;
        primaMacchina[i] = new Macchina(pos, y, 1, "M" + (i+1), attesa);
        pos += 30 + int(random(20,50));
      }
    }
    

    the file is this 1,1 2,2 3,3 4,1 5,4 6,2 7,5

    the first number is the name of the car, the second waiting time

    but the machines go on without stopping, how can I do

  • edited October 2013

    I guess you should place each pair in its own line inside that file: :-/

    1,1
    2,2
    3,3
    4,1
    5,4
    6,2
    7,5

  • are already entered in pairs, but still does not work

  • edited October 2013

    Something strange I've spotted in your algorithm:
    Variables can only hold up 1 value at a time!
    So, attesa will have the last value attributed to it! Any past values are lost! :-B

  • how can I do?

  • resolved !!!!!!!

    // Stati
    final int statoMenu = 0;
    final int FCFS = 1;
    final int SJF = 2;
    final int ritorno = 3;
    int stato;
    
    PImage macchina,sfondoMenu;
    PFont menuFont, font;
    String file[];
    int p;
    Macchina [] primaMacchina;
    float pos=-250;
    int [] valori;
    int [] appo = new int [1000];
    
    void setup(){
    
      size(850,637);
      stato = statoMenu;
      sfondoMenu=loadImage("sfondomenu.jpg");
      macchina=loadImage("macchina.png");
      menuFont=loadFont("myfont.vlw");
      font = loadFont("font.vlw");
      file=loadStrings("simulazione.txt"); //apertura del file
      p = file.length; //lunghezza del file
      //Divisione del file
      for (int i = 0; i < p; i++){
        //valori[i] = int(split(file[i],","));
        valori = int(split(file[i],","));
        valori[1] = valori[1]*1000;
        appo[i] = valori[1];
        println(appo[i]);
        //println(" nome: "+valori[0]+" ts: " +valori[1]);
      }
    
  • No need for a separate array of "appo". You can put the wait time in the car constructor, so you can stock them there instead. So each car can use its own wait time.

Sign In or Register to comment.