How can I display the 1st, 2nd, and 3rd places in my race program?

I already got the first place displayed, now I need help with finding out which car gets 2nd, and third place.

My Code:

PImage [] Lambos = new PImage [5];
float[] horsePos = new float [5];

float first;
float second;
float third;

int state = 0;
int value = 0;
int value2 = 0;
int x = 100;
int x2 = 100;

//Image location on X coordinate
int car1X = 0;
int car2X = 0;
int car3X = 0;
int car4X = 0;
int car5X = 0;

//Image location on Y coordinate
int car1Y = 40;
int car2Y = 110;
int car3Y = 180;
int car4Y = 250;
int car5Y = 320;

void setup() {
  size(1200, 400);

  Lambos[0] = loadImage("red.png");
  Lambos[1] = loadImage("orange.png");
  Lambos[2] = loadImage("blue.png");
  Lambos[3] = loadImage("green.png");
  Lambos[4] = loadImage("yellow.png");

  Lambos[0].resize(110, 40);
  Lambos[1].resize(110, 40);
  Lambos[2].resize(110, 40);
  Lambos[3].resize(110, 40);
  Lambos[4].resize(110, 40);
}

void draw() {
  background(0);

  if (state == 0) {

    stroke(255);
    strokeWeight(10);
    fill(255);
    line (100, 155, 1100, 155);
    rect(x, 140, 10, 30);
    line (100, 290, 1100, 290);
    rect(x2, 275, 10, 30);

    if (dist(mouseX, mouseY, x, 155) < 60 && mousePressed) {
      x = mouseX;
    }

    if (dist(mouseX, mouseY, x2, 275) < 60 && mousePressed) {
      x2 = mouseX;
    }

    if (x < 100) {
      x = 100;
    } else if (x > 1100) {
      x = 1100;
    }
    if (x2 < 100) {
      x2 = 100;
    } else if (x2 > 1100) {
      x2 = 1100;
    }


    if (x > 100) {
      value = -100 + x;
    }

    if (x2 > 0) {
      value2 = -100 + x2;
    }

    textSize(17);
    text(value, 320, 210);
    textSize(17);
    text(value2, 885, 210);
    textSize(30);
    text("Race Game Assignment", 460, 50);
    textSize(15);
    text("By: Nathaniel K.", 560, 75);
    textSize(20);
    text("Player 1", 290, 130);
    text("Player 2", 850, 130);

    noFill();
    rect(50, 100, 1100, 275);

    if (keyPressed) {
      state = 1;
    }
  } 

  if (state == 1) {

    for (int i = 0; i < 6; i ++) {
      image(Lambos[0], horsePos[0], car1Y);
      image(Lambos[1], horsePos[1], car2Y);
      image(Lambos[2], horsePos[2], car3Y);
      image(Lambos[3], horsePos[3], car4Y);
      image(Lambos[4], horsePos[4], car5Y);
      horsePos[0] += random(1, 3);
      horsePos[1] += random(1, 3);
      horsePos[2] += random(1, 3);
      horsePos[3] += random(1, 3);
      horsePos[4] += random(1, 3);
    }

    finishline();
    winner();
  }
}

void winner() {

  for (int i = 0; i < 5; i ++) {
    if (horsePos[i] >= 988) {
      first = i;
    }

    if (horsePos[i] == 987) {
      second = horsePos[i];
    }
    if (horsePos[i]!=988 && horsePos[i] == 987) {
      second = horsePos[i];
    }
    if (horsePos[i]!=987 && horsePos[i] == 986) {
      second = horsePos[i];
    }
    if (horsePos[i]==13) {
      third = horsePos[i];
    }
  }

  System.out.println(first);
  System.out.println(second);
  System.out.println(third);

  if (horsePos[0] > 988) {
    textSize(32);
    fill(255);
    text("Car 1 is the Winner", 370, 40);
    //state = 3;
    noLoop();
  }
  if (horsePos[1] > 988) {
    textSize(32);
    fill(255);
    text("Car 2 is the Winner", 370, 40);
    //state = 3;
    noLoop();
  }
  if (horsePos[2] > 988) {
    textSize(32);
    fill(255);
    text("Car 3 is the Winner", 370, 40);
    //state = 3;
    noLoop();
  }
  if (horsePos[3] > 988) {
    textSize(32);
    fill(255);
    text("Car 4 is the Winner", 370, 40);
    //state = 3;
    noLoop();
  }
  if (horsePos[4] > 988) {
    textSize(32);
    fill(255);
    text("Car 5 is the Winner", 370, 40);
    //state = 3;
    noLoop();
  }

  if (state == 3) {

    textSize(20);
    text("Player 1", 230, 130);
    text("Player 2", 680, 130);

    stroke(255);
    strokeWeight(10);
    line(500, 100, 500, 275);
    noFill();
    rect(50, 100, 900, 175);
  }
}


void finishline() {
  strokeWeight(0);
  fill(0, 0, 0);
  rect(1100, 0, 100, 400);
  fill(555, 255, 255);
  rect(1100, 0, 25, 25);
  rect(1150, 0, 25, 25);
  rect(1100, 50, 25, 25);
  rect(1150, 50, 25, 25);
  rect(1100, 100, 25, 25);
  rect(1150, 100, 25, 25);
  rect(1100, 150, 25, 25);
  rect(1150, 150, 25, 25);
  rect(1100, 200, 25, 25);
  rect(1150, 200, 25, 25);
  rect(1100, 250, 25, 25);
  rect(1150, 250, 25, 25);
  rect(1100, 300, 25, 25);
  rect(1150, 300, 25, 25);
  rect(1125, 25, 25, 25);
  rect(1175, 25, 25, 25);
  rect(1125, 75, 25, 25);
  rect(1175, 75, 25, 25);
  rect(1125, 125, 25, 25);
  rect(1175, 125, 25, 25);
  rect(1125, 175, 25, 25);
  rect(1175, 175, 25, 25);
  rect(1125, 225, 25, 25);
  rect(1175, 225, 25, 25);
  rect(1125, 275, 25, 25);
  rect(1175, 275, 25, 25);
  rect(1175, 325, 25, 25);
  rect(1125, 325, 25, 25);
  rect(1100, 350, 25, 25);
  rect(1150, 350, 25, 25);
  rect(1175, 375, 25, 25);
  rect(1125, 375, 25, 25);
}

Thanks,

            Sandy
Tagged:

Answers

  • you need to sort the horsePos and then just display the resulting array slot 0, slot 1, slot 2, slot 3:

        int[] numbers = new int[20];
        int[] numbers2 = new int[20];
    
    
        void setup() {
          size(1000, 1000);
    
          int num =0;
          for (int x =0; x< 20; x++) {
            num =int(random(10, 50));
            numbers [x] = num ;
          }
    
          println(numbers);
          numbers2 = sort(numbers);
    
          println("\nsorted");
          println(numbers2);
        }
    
        void draw() {
    
          background(255);
        }
        //
    

    anyway

    you are using arrays in your sketch but you are not using the full potential of arrays. I rewrote your setup to show you what I mean:

        String[] fileNames = {
          "red.png", 
          "orange.png", 
          "blue.png", 
          "green.png", 
          "yellow.png"
        };
    
        //----------------------------------------------------
    
        void setup() {
          size(1200, 400);
    
          for (int i=0; i<Lambos.length; i++) {
            Lambos[i] = loadImage(fileNames[i]);
            Lambos[i].resize(110, 40);
          }//for
        }// func 
    

    Here you can see that we handle the arrays using an for-loop which is the way to do it.

    you need to bring car1Y,...,.... into an array to.

    in line 107 you use a for loop but you don't use it variable i, not sure why.

  • Sorry, but I don't understand what you mean when you say " sort the horsePos 's" could you please explain that more

  • edited April 2017

    @Sandyjakes -- horsePos is the name of your positions array in your original code, which needs to be sorted (1st, 2nd, 3rd) and @Chrisir demonstrated how to sort below in his code example -- the sorting happens at the part that calls "sort" and prints "sorted" to the screen.

    numbers2 = sort(numbers);
    println("\nsorted");
    println(numbers2);
    

    Before asking for additional details please first read the full answer -- and try running the code that was written for you! If you don't understand a function or method, try looking it up in the Processing Reference:

  • edited April 2017

    Performance-wise, Arrays::sort() is better than PApplet::sort().
    B/c it doesn't instantiate another cloned array. It sorts the array in loco, mutating it in place: \m/

    http://docs.Oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-byte:A-

    println(numbers);
    java.util.Arrays.sort(numbers);
    println(numbers);
    
  • So this:

      println(horsePos);
      horsePos2 = sort(horsePos);
    
      println("\nsorted");
      println(horsePos2);
    
  • @jeremydouglass @GoToLoop @Chisir

    So, I sorted it but it only displays the positions of each car/horse at the beginning of the race. I need to find out who is 1st, 2nd, and 3rd when the race is finished.

  • I keep trying but I just can't figure out how to do it, I put it in void draw but then it constantly displayed the positions. I only need the first 3 cars/horses that were closest to the finish line which was at 988

  • You want to do the sort when the race is over, so maybe in function winner?

    Once it is sorted tje winner is at horsepos[0], the 2nd at horsePos[1] etc.

    You can use text() to show them on screen

  • I think it would be much easier with classes, but if you don't have knowledge of them (and inheritance too), don't bother.

  • I understand that but instead of it displaying the car/horse# It is displaying the x coordinates

  • show your code, maybe somebody else can help

  • edited May 2017 Answer ✓

    @Sandyjakes -- now that I understand what your horsePos array is I see your dilemma. As far as I am aware there is no concise / elegant way to sort your list and preserve the index in Processing( Java). There are many solutions -- involving HashMaps, or Comparators, or objects, etc. -- and there are long discussions of several ways of doing this here:

    However I wouldn't recommend any of them for you at this stage. The easiest way is not to sort a positions list at all, and instead use a different process. One way involves instead using Processing Tables:

    1. when a car crosses the finish line, write down its number AND score in two columns of a score Table. https://processing.org/reference/Table.html

        for (int i = 0; i < 5; i ++) {
          if (horsePos[i] >= 988) {
            // add car number i, time mills(), and position horsePos[i] to score Table here
          }
       }
      
    2. at this point you can either record all the other cars based on their current positions OR finish the race.

    3. if you choose to finish the race, each time you add an entry to the Table, check to see if there are horsePos.length() entries (5). Only finish the race once you have all 5 entries -- every car will cross the finish line and their positions will be based on time, just like in a real car race.

    4. stop cars and declare a winner by sorting your table by the score column with Table.sort() https://processing.org/reference/Table_sort_.html
    5. finally, display the table results on the screen!
  • That leads to a question - what if two cars reach the finish line in the same frame?

  • well before sorting you can also add the ypos as a string plus "#" plus number as a string, sort it and split it with #

  • what if two cars reach the finish line in the same frame

    Then they are tied!

    Or, if you are recording car number, time mills(), and position horsePos[i] as suggested above then distance may differentiate them -- so they both crossed at the same time, but one was going faster/further.

    Of course, ties are less likely but still possible -- and tied cars (whether by frame number, time in millis() of crossing, or total distance at time of crossing) will be listed in arbitrary order, probably ranked by their array order (lane index).

    If you want to solve which crossed first (according to an interpolation of the previous and new positions, and when each crossed the line), then the math is more complicated -- but I don't think that is what this adding-random-distances method of moving cars is simulating. That makes ties even less likely, but still possible.

    If ties aren't allowed at all, just arbitrarily give out ranks to tied cars (by any ranking) on a first-come basis as they are added to the rankings table.

  • Exactly what I was talking about, @jeremydouglass, though the question was meant for the OP. We can only speculate.

  • This makes me remember a game I had built over a year back. I'll try to find it - you might be able to learn something from it.

  • Alright, so it has a lot of problems with it - I was meant to have continued something with it, but I don't remember what.
    For now, you can see a demo here - http://ge.tt/3ZtdMuj2 (download and extract the zip files, run Movers.exe, and do not click the "See Images" button int the application)

Sign In or Register to comment.