Enter a name to a Highscore list with 4 Letters

I want four letters to appear behind the last added score. With the arrow keys, you can switch between the letters (Left & Right) and you can scroll through the alphabet (Up & Down). The letter that is selected should be displayed in white and the others should remain red. As soon as the enter key is pressed, the name should be saved to the current score and can no longer be changed. This function is to be implemented so that it is added with the current score and is also saved after the enter key is pressed.

The function should look something like this.

int Letter = 65;

void setup(){

  size(1260, 720);

}

void draw(){

  background(0);
  textSize(80);
  fill(255, 0, 0, 255);
  text(char(Letter)+"", width/2, height/2);

}

void keyPressed(){

  if(keyCode == UP){ 
   if(Letter > 64){ 
    Letter--;
     if(Letter == 64){
      Letter = 90;
     }

   }
  }

  if(keyCode == DOWN){ 
   if (Letter < 91){
    Letter++;
     if(Letter == 91){
      Letter = 65;
     }
   }
  }

}

Here I try to implement the solution

int[] scores = new int[5];
int score;

Table table;
String savegame = "savescore.csv";


void setup(){

 size(1260, 720);
 for (int i=0; i<scores.length; i++){
  scores[i] = 0;
 }
 table = null;
 table = loadTable(savegame, "header");
 if(table!=null){ 
  for(TableRow row : table.rows()){ 
   int id = row.getInt("id");
   scores[id] = row.getInt("savedata");
  }
 }else{ 
  table = new Table();  
  table.addColumn("id"); 
  table.addColumn("savedata");
  for(int i = 0; i<scores.length; i++){  
   TableRow newRow = table.addRow();     
   newRow.setInt("id", table.lastRowIndex());
   newRow.setInt("savedata", scores[i]);
  }
 }

}

void draw(){

 background(0);
 textSize(80);
 fill(255, 0, 0, 255);
 text("Highscores ", 300, 160);
 for (int i=0; i<scores.length; i++) {
  textSize(60);  
  text(scores[i], 570, 300+80*i);
 }

}

void keyPressed(){

 if(key == ' '){ // Press space to add score
  score = int(random(100));
  addNewScore(score);
  updateTable();
  saveScores();
 } 
}

void addNewScore(int score){

 for(int i=0; i<scores.length; i++){
  if(score>=scores[i]){
   for(int j=scores.length-1; j>=max(i,1); j--){
    scores[j] = scores[j-1];
   }
    scores[i] = score;

    break;
  }
 }

}

void updateTable(){  

 for(TableRow row : table.rows()){  
  int id = row.getInt("id");
  table.setInt(id, "savedata", scores[id]);
 }

}

void saveScores(){

 saveTable(table, "data/savescore.csv");

}

Answers

  • I'm not able to do that. I know it is hard. If anyone can do this, I'll be very happy. If not, I'm ok with it. :)

  • edited July 2017

    Your implementation doesn't look anything like your description or your pseudo-code. Pressing up and down doesn't do anything.

    First, I would suggest separating editing a score string from loading / saving scores or displaying a score table. Start by making an editing sketch.

    So create a sketch based on your top sketch -- with no table load or save code that just lets you manipulate a four-letter string on the screen. When you press enter (or whatever), that would save it, but that it separate.

    You might also want to consider saving a String -- or a char[] instead of an int[] -- just seems like it would make life easier....

  • edited July 2017

    Here is one simple example based on your code. Yours does not need to work this way. I still might recommend switching from int[] to char[] or String.

    int[] score; // a string of letters
    int pos;     // currently edited letter
    
    void setup() {
      score = newScore(score);
      size(600, 400);
      textSize(80);
    }
    
    // set a score to AAAA
    int[] newScore(int[] thisScore) {
      thisScore = new int[4];
      for (int i=0; i<thisScore.length; i++) {
        thisScore[i] = 65;
      }
      return thisScore;
    }
    
    // display  score with current letter highlighted
    void draw() {
      background(0);
      for (int i=0; i<score.length; i++) {
        if (i==pos) {
          fill(255, 0, 0, 255);
        } else {
          fill(255, 255);
        }
        text(char(score[i])+"", width/4 + i*80, height/2);
      }
    }
    
    // change edit position or letter value
    void keyPressed() {
      // change edit position
      if (keyCode == LEFT) {
        pos = max(pos-1, 0);
      }
      if (keyCode == RIGHT) {
        pos = min(pos+1, 3);
      }
    
      // change letter value
      if (keyCode == UP) { 
        score[pos]--;
        if (score[pos] < 65) score[pos] = 90;
      }
      if (keyCode == DOWN) { 
        score[pos]++;
        if (score[pos] > 90) score[pos] = 65;
      }
    
      // display current results on console
      if (keyCode == ENTER) {
        for(int i:score){
          print(char(i));
        }
        println();
      }
    }
    
  • @jeremydouglass you are great. thanks for your help. My first code works only up and down and my second code was the code that I want to implement this function. Because of your help, I'm one step closer now.

  • edited July 2017 Answer ✓

    here is the entire thing:

    • hit space to enter a new score

    • enter name (when your score is high enough) and finish with Enter

    • we only use the table now, no additional score[] array anymore

    Chrisir

    // states of a program - necessary for the Input  
    final int stateNormal     = 0;
    final int stateInput      = 1;
    final int stateAfterInput = 2;
    int state=stateNormal; 
    
    // simulate new score entry
    String score;
    
    // for input 
    char[] letters = new char[4]; // 4 letters
    int index=0;                  // which letter is active  
    String result=""; 
    
    Table table;
    String savegame = "savescore.csv";
    final int N = 5; // entries in high score  
    
    //---------------------------------------------------------
    
    void setup() {
    
      size(1260, 720);
    
      // letters for Input 
      for (int i=0; i<letters.length; i++) {
        letters[i]='A';
      }
    
      //try to load table
      table = null;
      table = loadTable(savegame, "header");
      // success?
      if (table!=null) {
        //success
        for (TableRow row : table.rows()) { 
          int id = row.getInt("id");
          //      scores[id] = row.getInt("savedata");
        }
      } else { 
        // fail: 
        // first run, make table 
        table = new Table();  
        table.addColumn("id"); 
        table.addColumn("savedata");
        table.addColumn("name");
        for (int i = 0; i<N; i++) {  
          TableRow newRow = table.addRow();     
          newRow.setInt("id", table.lastRowIndex());
          newRow.setInt("savedata", 0);
          newRow.setString("name", "");
        }
      }
    }
    
    void draw() {
    
      // state tells how the program works: 
      if (state==stateNormal) {
        // normal: show high score 
        background(0);
        textSize(80);
        fill(255, 0, 0, 255);
        text("Highscores ", 300, 160);
        textSize(18);
        text("Simulate Highscore, hit space to add new entry", 17, 27);
        textSize(60);  
        for (int i=0; i<N; i++) {
          TableRow newRow = table.getRow(i);    
          text(newRow.getInt("savedata")
            +" " +newRow.getString("name"), 570, 300+80*i);
        }
      }
      // -------------------
      else  if (state==stateInput) {
        // state for the Input 
        background(0);
    
        textSize(80);
        fill(255, 0, 0); // red = default
        text("Please enter your name", 33, 77);
        textSize(18);
        text("Use cursor left right and up and down, return to finish input", 33, 127);
        textSize(80);
    
        int i=0;
        for (char c : letters) { 
          fill(255, 0, 0); // red = default
          if (i==index) 
            fill(255); // selected 
          text(char(letters[i])+"", width/2+i*65, height/2);
          i++;
        }//for
      }// else if
      // ------------------
      else if (state==stateAfterInput) {
        // state after the Input 
    
        result=""+letters[0]+letters[1]+letters[2]+letters[3];
        println(result); 
    
        addNewScore(score, result);
        saveScores();
    
        state=stateNormal;
      }
    }
    
    // -----------------------------------------------------
    
    void keyPressed() {
    
      // state tells how the program works: 
      if (state==stateNormal) {
    
        if (key == ' ') { // Press space to add score
          letters = new char[4];
          for (int i=0; i<letters.length; i++) {
            letters[i]='A';
          }
          // add one element to high scores
          score = str(int(random(100)));
          println(score); 
          if (highEnoughForHighScore(score)) {
            state=stateInput;
          }
        }
      }// state 
      // -------------------
      else  if (state==stateInput) {
        // state for the Input  
    
        if (keyCode == UP) { 
          if (letters[index] > 64) { 
            letters[index]--;
            if (letters[index] == 64) {
              letters[index] = 90;
            }
          }
        } else if (keyCode == DOWN) { 
          if (letters[index] < 91) {
            letters[index]++;
            if (letters[index] == 91) {
              letters[index] = 65;
            }
          }
        } else if (keyCode == LEFT) {
          index--;
          if (index<0) 
            index=0;
        } else if (keyCode == RIGHT) {
          index++;
          if (index>3) 
            index=3;
        } else if (key == RETURN||key==ENTER) {
          state=stateAfterInput;
        }
      }//state
    }//func 
    
    // ---------------------------------------------------------------------------
    
    void addNewScore(String score, String name) {
      // 
      // we add a new row, table is too long now 
      TableRow newRow = table.addRow();     
      newRow.setInt("id", table.lastRowIndex());
      newRow.setInt("savedata", int(score));
      newRow.setString("name", name);
    
      // we sort 
      table.trim();  // trim 
      table.sortReverse("savedata"); // sort backwards by score
    
      // test
      println ("---");
      for (int i = 0; i<table.getRowCount(); i++) {  
        newRow = table.getRow(i);     
        println (newRow.getInt("savedata"), newRow.getString("name"));
      }
      println ("---");
    
      // we delete items 
      if (table.getRowCount()>5)
        for (int i=table.getRowCount()-1; i>=5; i--) {
          TableRow row=table.getRow(i);
          table.removeRow(i);
        }
    
      // test
      println ("---");
      for (int i = 0; i<table.getRowCount(); i++) {  
        newRow = table.getRow(i);     
        println (newRow.getInt("savedata"), newRow.getString("name"));
      }
      println ("---");
    }//func 
    
    void saveScores() {
      saveTable(table, "data/savescore.csv");
    }
    
    boolean highEnoughForHighScore(String score) {
    
      // test whether new score is high enough to get into the highscore
    
      for (TableRow newRow : table.rows()) {
        if (score.compareTo(str(newRow.getInt("savedata")))>0) {
          return true; // high enough
        }//if
      }//for
      return false; // NOT high enough
    } //func
    //
    
  • edited August 2017

    @Chrisir You are the dumbledore of processing. Thank you so much. Thanks for the explaination entries in the code. I'll try to learn what you've done and implement this to my game. Thank you guys, you are great. :D

  • To get a single value from the table use

    newRow = table.getRow(i);     
    println (newRow.getInt("savedata"), newRow.getString("name"));
    

    With i being eg 0

  • @Chrisir

    text("Highscore:" + scores[0] , 380, 45);

    I want to Display the scores[0] value as the Highscore in the header of my game but scores[0] doesn't exist anymore. :) Is it possible to show a single row from the table in my text?

  • again:

    TableRow newRow = table.getRow(0);  
    
    text(newRow.getInt("savedata"), 380,45); 
    
  • edited August 2017

    @Chrisir thanks it works fine. Here is my last problem... If you use

    score = str(int(random(20)));

    The Table sort the values in a wrong position. Do you've got an idea what the problem is?

  • line 122: you just changed 100 to 20 or did you change more?

  • I've only changed line 122: 100 to 20.

  • Weired.

    The sorting is done by line 173

    I am not at home but I can look tonight

    Or you look into it: it's called debugging. Insert some println (score) to check where the code goes astray....

  • edited August 2017

    I think, the table only sorts from 0 to 9. he sorted like this:

    1. 9
    2. 6
    3. 5
    4. 20
    5. 13

    The table don't mind, that it is a to figured value

  • No. With numbers from 0 to 100 it sorted correctly.

    Did you change the column he is sorting by? Must be savedata

  • edited August 2017

    from 0 to 100, is the chance to get a 1 figured value much smaller. I think it would be like this

    9

    75

    5

    43

    11

    I don't change the column or something. I'll try to figure it out and use your tip with the debugging. I think that the problem can not be very big. If I can figure it out, I'll post the solution here.

  • Maybe it's because we convert from string to int and back - not really necessary

  • Processing has inbuilt means to debug, check the menus

  • The problem is probably bigger than I thougt. I tried to use only Strings or only Integers but nothing solve the Problem. The debugging tool, doesn't find any failures or something. I think, I have to sort it with arrays and for loops just like before and use the table, only to display save and load scores.

  • edited August 2017

    When posting feedback like that (like you just did) it is always practically if you post your current version (entire code) so that we are on the same ground

    I'll take a look right now at my old version

  • edited August 2017 Answer ✓

    I solved it.

    It is necessary to delete your old high score from hard drive or start with a new folder and high score in it.

    Start with a new high score.

    Reason was that the high score had a String type (for the score savedata) in the table (even on the hard drive).

    Seen as Strings, 19 is smaller than 8 or 9 (they are seen as "8_" and "9_" with a space after them, so higher than 19, so 19 didn't get into the high score list after sorting and cutting the table in addNewScore), hence the error. It's like Max is before Maximilian in the dictionary (When it's a String. When it's int, it's 19>9).

    When making the table the very first time we need

        table.addColumn("savedata", Table.INT ); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!
    

    so we have this column as int (the default is String and that's what we had).

    Hard to spot.That error was hard to find.

    By the way I also thought of a way that lets the player select A-Z_!?-_* as signs and not only A-Z.

    Best, Chrisir ;-)

    // states of a program - necessary for the Input  
    final int stateNormal     = 0;
    final int stateInput      = 1;
    final int stateAfterInput = 2;
    int state=stateNormal; 
    
    // simulate new score entry
    int score;
    
    // for input 
    char[] letters = new char[4]; // 4 letters
    int index=0;                  // which letter is active  
    String result=""; 
    
    Table table;
    String savegame = "savescore.csv";
    final int N = 5; // entries in high score  
    
    //---------------------------------------------------------
    
    void setup() {
    
      size(1260, 720);
    
      // letters for Input 
      for (int i=0; i<letters.length; i++) {
        letters[i]='A';
      }
    
      //try to load table
      table = null;
      table = loadTable(savegame, "header");
      // success?
      if (table!=null) {
        //success
        for (TableRow row : table.rows()) { 
          int id = row.getInt("id");
          //      scores[id] = row.getInt("savedata");
        }
      } else { 
        // fail: 
        // first run, make table 
        table = new Table();  
        table.addColumn("id"); 
        table.addColumn("savedata", Table.INT ); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!
        table.addColumn("name");
        for (int i = 0; i<N; i++) {  
          TableRow newRow = table.addRow();     
          newRow.setInt("id", table.lastRowIndex());
          newRow.setInt("savedata", 0);
          newRow.setString("name", "");
        }
      }
    }
    
    void draw() {
    
      // state tells how the program works: 
      if (state==stateNormal) {
        // normal: show high score 
        background(0);
        textSize(80);
        fill(255, 0, 0, 255);
        text("Highscores ", 300, 160);
        textSize(18);
        text("Simulate Highscore, hit space to add new entry", 17, 27);
        textSize(60);  
        for (int i=0; i<N; i++) {
          TableRow newRow = table.getRow(i);    
          text(newRow.getInt("savedata")
            +" " +newRow.getString("name"), 570, 300+80*i);
        }
      }
      // -------------------
      else  if (state==stateInput) {
        // state for the Input 
        background(0);
    
        textSize(80);
        fill(255, 0, 0); // red = default
        text("Please enter your name", 33, 77);
        textSize(18);
        text("Use cursor left right and up and down, return to finish input", 33, 127);
        textSize(80);
    
        int i=0;
        for (char c : letters) { 
          fill(255, 0, 0); // red = default
          if (i==index) 
            fill(255); // selected 
          text(char(letters[i])+"", width/2+i*65, height/2);
          i++;
        }//for
      }// else if
      // ------------------
      else if (state==stateAfterInput) {
        // state after the Input 
    
        result=""+letters[0]+letters[1]+letters[2]+letters[3];
        println(result); 
    
        addNewScore(score, result);
        saveScores();
    
        state=stateNormal;
      }
    }
    
    // -----------------------------------------------------
    
    void keyPressed() {
    
      // state tells how the program works: 
      if (state==stateNormal) {
    
        if (key == ' ') { // Press space to add score
          letters = new char[4];
          for (int i=0; i<letters.length; i++) {
            letters[i]='A';
          }
          // add one element to high scores
          score = int(random(20));
          println(score); 
          if (highEnoughForHighScore(score)) {
            state=stateInput;
          }
        }
      }// state 
      // -------------------
      else  if (state==stateInput) {
        // state for the Input  
    
        if (keyCode == UP) { 
          if (letters[index] > 64) { 
            letters[index]--;
            if (letters[index] == 64) {
              letters[index] = 90;
            }
          }
        } else if (keyCode == DOWN) { 
          if (letters[index] < 91) {
            letters[index]++;
            if (letters[index] == 91) {
              letters[index] = 65;
            }
          }
        } else if (keyCode == LEFT) {
          index--;
          if (index<0) 
            index=0;
        } else if (keyCode == RIGHT) {
          index++;
          if (index>3) 
            index=3;
        } else if (key == RETURN||key==ENTER) {
          state=stateAfterInput;
        }
      }//state
      else {
        // don't do anything
      }//
    }//func 
    
    // ---------------------------------------------------------------------------
    
    void addNewScore(int score, String name) {
      // 
      // we add a new row, table is too long now 
      TableRow newRow = table.addRow();     
      newRow.setInt("id", table.lastRowIndex());
      newRow.setInt("savedata", int(score));
      newRow.setString("name", name);
    
      // we sort 
      table.trim();  // trim 
      table.sortReverse("savedata"); // sort backwards by score
    
      // test
      println ("---");
      for (int i = 0; i<table.getRowCount(); i++) {  
        newRow = table.getRow(i);     
        println (newRow.getInt("savedata"), newRow.getString("name"));
      }
      println ("---");
    
      // we delete items 
      if (table.getRowCount()>5)
        for (int i=table.getRowCount()-1; i>=5; i--) {
          TableRow row=table.getRow(i);
          table.removeRow(i);
        }
    
      // test
      println ("---");
      for (int i = 0; i<table.getRowCount(); i++) {  
        newRow = table.getRow(i);     
        println (newRow.getInt("savedata"), newRow.getString("name"));
      }
      println ("---");
    }//func 
    
    void saveScores() {
      saveTable(table, "data/savescore.csv");
    }
    
    boolean highEnoughForHighScore(int score) {
    
      // test whether new score is high enough to get into the highscore
    
      for (TableRow newRow : table.rows()) {
        //if (score.compareTo(str(newRow.getInt("savedata")))>0) {
        if (score>newRow.getInt("savedata")) {
          return true; // high enough
        }//if
      }//for
      // for didn't find anything, we are still in this function 
      return false; // NOT high enough
    } //func
    //
    
  • edited August 2017

    @Chrisir Thank you so much for spending so much time. I appreciate that. I've learned many things from you. Thanks to all other volunteers too. If you want to see the final result.

    https://www.dropbox.com/sh/xnzhx4v9hd0s7tp/AAALJYZ06S01rVvhVDR5Cvb3a?dl=0

    That's my very first game and I'll always remember, that you guys spend so much time to help me. Now I'll try a new game and maybe I can help here someone in the near future. :)

    If anyone find a bug or a failure in my game, please give me a notice.

  • edited August 2017

    @MigiModo -- Congratulations, and thanks for sharing your work!

    Three things:

    • Non-Windows users wanting to run this as a sketch in Processing should do the following:
      1. download "source" folder and rename "Project_Pong_7"
      2. download "data" folder and put inside Project_Pong_7 folder
    • The high scores table works very well! It could be improved by sorting it by score, high to low. Tie scores that come later have a lower rank than the first person to make that score.
    • In Ping Pong, when I tried to use both sides of the keyboard to control both paddles, the keyboard quickly froze, and no further keyboard input was possible. This is on Mac in PDE 3.3.5.
  • @jeremydouglass thanks for your feedback. I think that the Problems only appears on Mac. I'll share the complete sketch and post the new link. Thanks for your time :)

  • I thought the high score list is sorted by score?

  • The high score list is not sorted by score -- at least not correctly. That is one of my suggestions for improvement. Here is what happens when I play once and get a "12" (JHDZ).

    MigiModo Pong High Score--unsorted

  • Sorry, this is a misunderstanding... I believe you and I believed you when I saw your post.

    I meant to say to the OP why it doesn't work - since I wrote the code for him and it worked....

  • Hmm.. Delete the savescore.txt file in all data folders. After that, it should work :)

  • Ohh. I see what's happen.. When you close the game and start it again, the Table.INT doesn't work anymore. :\"> I'll try to fix it. Thanks @jeremydouglass :)

  • Initially after using Table.INT you need to delete old table on hard drive - also when uploading

  • edited August 2017

    It works only one time. When you close and restart the game, the scores are sorted bad again. :)

  • Then debug it....

    Is it somewhere loaded as String e.g.?

  • Answer ✓
    // states of a program - necessary for the Input  
    final int stateNormal     = 0;
    final int stateInput      = 1;
    final int stateAfterInput = 2;
    int state=stateNormal; 
    
    // simulate new score entry
    int score;
    
    // for input 
    char[] letters = new char[4]; // 4 letters
    int index=0;                  // which letter is active  
    String result=""; 
    
    Table table;
    String savegame = "savescore.csv";
    final int N = 5; // entries in high score  
    
    //---------------------------------------------------------
    
    void setup() {
    
      size(1260, 720);
    
      // letters for Input 
      for (int i=0; i<letters.length; i++) {
        letters[i]='A';
      }
    
      //try to load table
      table = null;
      table = loadTable(savegame, "header");
      // success?
      if (table!=null) {
        //success
        table.trim();  // trim 
        table.sortReverse("savedata"); // sort backwards by score
      } else { 
        // fail: 
        // first run, make table 
        table = new Table();  
        table.addColumn("id"); 
        table.addColumn("savedata"); 
        table.addColumn("name");
        for (int i = 0; i<N; i++) {  
          TableRow newRow = table.addRow();     
          newRow.setInt("id", table.lastRowIndex());
          newRow.setString("savedata", "");
          newRow.setString("name", "");
        }
      }
    }
    
    void draw() {
    
      // state tells how the program works: 
      if (state==stateNormal) {
        // normal: show high score 
        background(0);
        textSize(80);
        fill(255, 0, 0, 255);
        text("Highscores ", 300, 160);
        textSize(18);
        text("Simulate Highscore, hit space to add new entry", 17, 27);
        textSize(60);  
        for (int i=0; i<N; i++) {
          TableRow newRow = table.getRow(i);
          String scoreAsString=newRow.getString("savedata");
          // when it has a leading zero 
          if (scoreAsString.length()>=2)
            if (scoreAsString.charAt(0)=='0')
              scoreAsString=scoreAsString.charAt(1)+"";
          text(scoreAsString
            +" " 
            +newRow.getString("name"), 570, 300+80*i);
        }
      }
      // -------------------
      else  if (state==stateInput) {
        // state for the Input 
        background(0);
    
        textSize(80);
        fill(255, 0, 0); // red = default
        text("Please enter your name", 33, 77);
        textSize(18);
        text("Use cursor left right and up and down, return to finish input", 33, 127);
        textSize(80);
    
        int i=0;
        for (char c : letters) { 
          fill(255, 0, 0); // red = default
          if (i==index) 
            fill(255); // selected 
          text(char(letters[i])+"", width/2+i*65, height/2);
          i++;
        }//for
      }// else if
      // ------------------
      else if (state==stateAfterInput) {
        // state after the Input 
    
        result=""+letters[0]+letters[1]+letters[2]+letters[3];
        println(result); 
    
        addNewScore(score, result);
        saveScores();
    
        state=stateNormal;
      }
    }
    
    // -----------------------------------------------------
    
    void keyPressed() {
    
      // state tells how the program works: 
      if (state==stateNormal) {
    
        if (key == ' ') { // Press space to add score
          letters = new char[4];
          for (int i=0; i<letters.length; i++) {
            letters[i]='A';
          }
          // add one element to high scores
          score = int(random(20));
          println(score); 
          if (highEnoughForHighScore(score)) {
            state=stateInput;
          }
        }
      }// state 
      // -------------------
      else  if (state==stateInput) {
        // state for the Input  
    
        if (keyCode == UP) { 
          if (letters[index] > 64) { 
            letters[index]--;
            if (letters[index] == 64) {
              letters[index] = 90;
            }
          }
        } else if (keyCode == DOWN) { 
          if (letters[index] < 91) {
            letters[index]++;
            if (letters[index] == 91) {
              letters[index] = 65;
            }
          }
        } else if (keyCode == LEFT) {
          index--;
          if (index<0) 
            index=0;
        } else if (keyCode == RIGHT) {
          index++;
          if (index>3) 
            index=3;
        } else if (key == RETURN||key==ENTER) {
          state=stateAfterInput;
        }
      }//state
      else {
        // don't do anything
      }//
    }//func 
    
    // ---------------------------------------------------------------------------
    
    void addNewScore(int score, String name) {
      // 
      // we add a new row, table is too long now 
      TableRow newRow = table.addRow();     
      newRow.setInt("id", table.lastRowIndex());
      newRow.setString("savedata", nf(score, 2));
      newRow.setString("name", name);
    
      // we sort 
      table.trim();  // trim 
      table.sortReverse("savedata"); // sort backwards by score
    
      // test
      println ("---");
      for (int i = 0; i<table.getRowCount(); i++) {  
        newRow = table.getRow(i);     
        println (newRow.getInt("savedata"), newRow.getString("name"));
      }
      println ("---");
    
      // we delete items 
      if (table.getRowCount()>5)
        for (int i=table.getRowCount()-1; i>=5; i--) {
          TableRow row=table.getRow(i);
          table.removeRow(i);
        }
    
      // test
      println ("---");
      for (int i = 0; i<table.getRowCount(); i++) {  
        newRow = table.getRow(i);     
        println (newRow.getInt("savedata"), newRow.getString("name"));
      }
      println ("---");
    }//func 
    
    void saveScores() {
      saveTable(table, "data/savescore.csv");
    }
    
    boolean highEnoughForHighScore(int score) {
    
      // test whether new score is high enough to get into the highscore
    
      for (TableRow newRow : table.rows()) {
        //if (score.compareTo(str(newRow.getInt("savedata")))>0) {
        if (score>newRow.getInt("savedata")) {
          return true; // high enough
        }//if
      }//for
      // for didn't find anything, we are still in this function 
      return false; // NOT high enough
    } //func
    //
    
  • This works for scores from 0 to 99

    For higher scores you need to work on it

    It works with a leading zero that gets hidden when we display the score

  • @Chrisir That was a very smart idea. It works fine. Thanks again. I appreciate your explanations in the code. :-bd

  • I changed all link posts to this link. This is the last Version and I hope this is bug free. Thanks for help :)

    https://www.dropbox.com/sh/xnzhx4v9hd0s7tp/AAALJYZ06S01rVvhVDR5Cvb3a?dl=0

Sign In or Register to comment.