/**
 * Not a real game, but just a context for showing how to print an array
 * of HighScore objects and how to add a new object to an array in sorted
 * order (without increasing the size of the array).
 *
 * @author Zach Tomaszewski
 */
class Game {

  public static void main(String[] args) {

    HighScore[] record = new HighScore[5];

    /*
     * Imagine that the record was then filled by users playing the game normally
     * and that the array is kept sorted by adding each item in score order.
     * Here, we'll do it manually just so we have data to work with...
     */
    record[0] = new HighScore();
    record[0].name = "AAA";
    record[0].score = 1400;
    record[1] = new HighScore();
    record[1].name = "QUJ";
    record[1].score = 1280;
    record[2] = new HighScore();
    record[2].name = "ZMT";
    record[2].score = 1200;
    record[3] = new HighScore();
    record[3].name = "JJJ";
    record[3].score = 1150;
    record[4] = new HighScore();
    record[4].name = "TPS";
    record[4].score = 1000;

    /* First, printing the array */
    for (int i = 0; i < record.length; i++) {
      System.out.println(record[i].name + "\t" + record[i].score);
    }

    /*
     * Then, adding a new HighScore in sorted order.  Assumes we only ever
     * want to record the top 5 high scores.  Also, this code assumes
     * the array is always full of objects, which is unlikely to always
     * be the case.  In practice, we would probably need to use a count
     * variable to track how many objects are actually in a partially-
     * filled array.  In that case, we'd use count instead of record.length
     * in the code below.
     */
    //Create a new HighScore object;
    // pretend these values came from the user somehow.
    HighScore winner = new HighScore();
    winner.name = "NUE";
    winner.score = 1250;

    //Find where to insert this item into the array
    int placeToAdd = -1;  //if still -1 after loop, score too low to add
    for (int i = 0; i < record.length; i++) {
      if (record[i].score < winner.score) {
        //found the first high score that is less than the new winner
        //so should add the new winner here
        placeToAdd = i;
        break;
      }
    }
    if (placeToAdd >= 0) {
      //shift everything in array down to make room
      for (int i = record.length - 1; i > placeToAdd; i--) {
        record[i] = record[i - 1];
      }
      //insert winner
      record[placeToAdd] = winner;
    }

    /* printing all highscores again */
    System.out.println();
    for (int i = 0; i < record.length; i++) {
      System.out.println(record[i].name + "\t" + record[i].score);
    }

  }
}