/**
   * Compares two high scores
   *
   * @param x the first high score being compared
   * @param y the second high score being compared
   * @return an integer indicating the result of the comparison.
   */
  @Override
  public int compare(HighScore x, HighScore y) {
    int returnVal = 0;

    /* tests if x high score is greater than y*/
    if (x.getScore() < y.getScore()) {
      returnVal = -1;
    }
    /*tests if y high score is greater than x*/
    if (x.getScore() > y.getScore()) {
      returnVal = 1;
    }
    /*Determines if the two scores are equal*/
    if (x.getScore() == y.getScore()) {
      /*Compares x's name alphabetically to y's*/
      if (x.getName().compareTo(y.getName()) > 0) {
        returnVal = 1;
      }
      /*Compares y's name alphabetically to x's*/
      if (x.getName().compareTo(y.getName()) < 0) {
        returnVal = -1;
      }
    }

    return returnVal;
  }
Beispiel #2
0
  public HighScorePanel() {
    super.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(10, 0, 0, 0);

    HighScore[] hsdb = HighScore.readFromFile();
    int easyScore = hsdb[0].getScore();
    int medScore = hsdb[1].getScore();
    int hardScore = hsdb[2].getScore();

    String easy = easyScore + "";
    String med = medScore + "";
    String hard = hardScore + "";
    String easyN = hsdb[0].getUserName();
    String medN = hsdb[1].getUserName();
    String hardN = hsdb[2].getUserName();
    JLabel easyLabel = new JLabel("Easy:");
    Font f = easyLabel.getFont();
    easyLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD));
    JLabel easyNameLabel = new JLabel("Name: " + easyN);
    JLabel easyScoreLabel = new JLabel("Score: " + easy);
    JLabel medLabel = new JLabel("Medium:");
    medLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD));
    JLabel medNameLabel = new JLabel("Name: " + medN);
    JLabel medScoreLabel = new JLabel("Score: " + med);
    JLabel hardLabel = new JLabel("Hard:");
    hardLabel.setFont(f.deriveFont(f.getStyle() ^ Font.BOLD));
    JLabel hardNameLabel = new JLabel("Name: " + hardN);
    JLabel hardScoreLabel = new JLabel("Score: " + hard);
    JLabel highScores = new JLabel("High Scores");
    gbc.gridy = 0;
    super.add(highScores, gbc);
    gbc.gridy++;
    super.add(easyLabel, gbc);
    gbc.insets = new Insets(2, 0, 2, 0);
    gbc.gridy++;
    super.add(easyNameLabel, gbc);
    gbc.gridy++;
    super.add(easyScoreLabel, gbc);
    gbc.insets = new Insets(10, 0, 0, 0);
    gbc.gridy++;
    super.add(medLabel, gbc);
    gbc.insets = new Insets(2, 0, 2, 0);
    gbc.gridy++;
    super.add(medNameLabel, gbc);
    gbc.gridy++;
    super.add(medScoreLabel, gbc);
    gbc.insets = new Insets(10, 0, 0, 0);
    gbc.gridy++;
    super.add(hardLabel, gbc);
    gbc.insets = new Insets(2, 0, 2, 0);
    gbc.gridy++;
    super.add(hardNameLabel, gbc);
    gbc.gridy++;
    super.add(hardScoreLabel, gbc); // initiates all labels and adds them
  }
  /*
   * Creates a new item and adds it to the HighScores domain.
   */
  public void addHighScore(HighScore score) {
    String paddedScore = SimpleDBUtils.encodeZeroPadding(score.getScore(), 10);

    ReplaceableAttribute playerAttribute =
        new ReplaceableAttribute(PLAYER_ATTRIBUTE, score.getPlayer(), Boolean.TRUE);
    ReplaceableAttribute scoreAttribute =
        new ReplaceableAttribute(SCORE_ATTRIBUTE, paddedScore, Boolean.TRUE);

    List<ReplaceableAttribute> attrs = new ArrayList<ReplaceableAttribute>(2);
    attrs.add(playerAttribute);
    attrs.add(scoreAttribute);

    PutAttributesRequest par =
        new PutAttributesRequest(HIGH_SCORES_DOMAIN, score.getPlayer(), attrs);
    try {
      this.sdbClient.putAttributes(par);
    } catch (Exception exception) {
      System.out.println("EXCEPTION = " + exception);
    }
  }
 /*
  * Removes the item from the HighScores domain.
  * The item removes is the item whose 'player' matches the theHighScore submitted.
  */
 public void removeHighScore(HighScore score) {
   DeleteAttributesRequest dar =
       new DeleteAttributesRequest(HIGH_SCORES_DOMAIN, score.getPlayer());
   this.sdbClient.deleteAttributes(dar);
   this.count = -1; // To force count refresh.	
 }