My High Score Board rewrites all

edited December 2017 in Questions about Code

Hey, I'm new here. I use java inside Processing. So I'm trying to create a high score board with just the numbers not the name of the people. So far I've tried moving all them down to where the score should be, but all the scores under the one I want to change turn into high, the high score. I was thinking of making a second array and just switching, but I'm hoping there an easier way. I been trying to use it with spacebar instead of going through the whole game. Basically I'm wondering how to fix it, so it drops the other scores to the lower if its not the lowest then writes mine where I want it to be.

here my code:

void update(){ if (high > score[4].total){ check = 1;} if (high > score[3].total){ check = 2;} if (high > score[2].total){ check = 3;} if (high > score[1].total){ check = 4;} if (high > score[0].total){ check = 5;} if (check == 1){ score[4].total = high; } if (check == 2){ score[4].total = score[3].total; score[3].total = high; } if (check == 3){ score[4].total = score[3].total; score[3].total = score[2].total; score[2].total = high; } if (check == 4){ score[4].total = score[3].total; score[3].total = score[2].total; score[2].total = score[1].total; score[1].total = high; } if (check == 5){ score[4].total = score[3].total; score[3].total = score[2].total; score[2].total = score[1].total; score[1].total = score[0].total; score[0].total = high; } }

this is before before

this is after score board

what I wanted was:

score: 50 score: 50 score: 75 score: 100 score 123 /// this the new score

Tagged:

Answers

  • What type is high? Score []?

  • high is a float i used to store how many points the players has gotten.

  • And what type is score[]?

  • an array to me it's easier to manipulate to what I want.

  • Yes, it's an array, I can see that. But an array of what? It appears to have a member called total. What type is that? (This looks a lot like you are using references and assigning all the references to one object so that when you change one they all change)

    But you seem reluctant to supply the necessary details.

  • edited December 2017

    its an array of scores each score item has its own total, float. The way I thought it would turn out is way different then the way it turned out. I tried to change each Score,class, total to the one its needs to be. I don't get why it loops.

  • Answer ✓

    a runnable example would be good, not just a snippet.

    i've done this. and it works fine:

    static final int SCORES = 5;
    Score[] score = new Score[SCORES];
    float high;
    
    class Score {
      float total;
    }
    
    void setup() {
      // initialise scores, score[0] is the highest
      for (int i = 0 ; i < SCORES ; i++) {
        score[i] = new Score();
        score[i].total = 100 - 25 * i;
      }
      println("Before:");
      printScore();
    
      // add new score
      high = 76;
      update();
    
      println("After:");
      printScore();
    }
    
    // prints the scores, 0 to 4
    void printScore() {
      for (int i = 0 ; i < SCORES ; i++) {
        println(i, score[i].total);
      }
    }
    
    // your update code, with one addition
    void update(){
      int check = 0; // added this, everything else is fine
        if (high > score[4].total){ check = 1;}
        if (high > score[3].total){ check = 2;}
        if (high > score[2].total){ check = 3;}
        if (high > score[1].total){ check = 4;}
        if (high > score[0].total){ check = 5;}
        if (check == 1){
          score[4].total = high;
        }
        if (check == 2){
          score[4].total = score[3].total;
          score[3].total = high;
        }
        if (check == 3){
          score[4].total = score[3].total;
          score[3].total = score[2].total;
          score[2].total = high;
        }
        if (check == 4){
          score[4].total = score[3].total;
          score[3].total = score[2].total;
          score[2].total = score[1].total;
          score[1].total = high;
        }
        if (check == 5){
          score[4].total = score[3].total;
          score[3].total = score[2].total;
          score[2].total = score[1].total;
          score[1].total = score[0].total;
          score[0].total = high;
        }
      }
    
  • edited December 2017

    ty, it took a little reorganizing to get it where I want to be but now it works thanks you hundreds.

Sign In or Register to comment.