⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 score.java~32~

📁 j2me源代码
💻 JAVA~32~
字号:
package snake;

import java.io.*;
import javax.microedition.rms.*;

public class Score {
  private final static int SIZE = 5;
  private static short[] scoreRecords = new short[SIZE];
  private static String[] userName = new String[SIZE];
  private static RecordStore rs;

  public Score() {
  }

  private static void Init(){
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    DataOutputStream dOut = new DataOutputStream(bOut);
    byte[] bt;

    try {
      try {
        dOut.writeShort(0);
        dOut.writeUTF("");
        bt = bOut.toByteArray();
        dOut.close();
        bOut.close();
      }
      catch (IOException ex) {
        throw new RecordStoreException();
      }

      for (int i = 0; i < SIZE; i++) {
        rs.addRecord(bt, 0, bt.length);
      }
    } catch(RecordStoreException rse){
      try{
        rs.closeRecordStore();
      }catch (RecordStoreException rse2){
      }
    }
  }

  public static void loadScore(){
    try {
      rs = RecordStore.openRecordStore("socreRecord", true);

      if(rs.getNumRecords() == 0){
        Init();
      }else{
        ByteArrayInputStream bIn;
        DataInputStream dIn;
        byte data[];

        for(int i=0; i<SIZE; i++){
          data = rs.getRecord(i+1);
          if(data != null){
            bIn = new ByteArrayInputStream(data);
            dIn = new DataInputStream(bIn);
            try {
              scoreRecords[i] = dIn.readShort();
              userName[i] = dIn.readUTF();

              bIn.close();
              dIn.close();
            }
            catch (IOException ex1) {
            }
          }
        }
      }
    }
    catch (RecordStoreException ex) {
    }
  }

  public  static void  setScore(int score, String name){
    //debug
    System.out.println("Before set score:");
    for (int i = 0; i < SIZE; i++) {
      System.out.println(scoreRecords[i]);
      System.out.println(userName[i]);
    }

    sortScore();

    //debug
    System.out.println("After sort:");
    for (int i = 0; i < SIZE; i++) {
     System.out.println(scoreRecords[i]);
     System.out.println(userName[i]);
   }

    if(score < scoreRecords[SIZE-1])
      return;
    else{
      int i = 0;
      for(; i<SIZE; i++){
        if(score > scoreRecords[i])
          break;
      }

      for(int j = SIZE -1; j>i; j--){
        scoreRecords[j] = scoreRecords[j-1];
        userName[j] = userName[j-1];
      }

      if(i >= SIZE)
        i--;

      scoreRecords[i] = (short)score;
      userName[i] = name;
    }
  }

  /**
   * 将新的得分记录写进数据库
   */
  public static void closeScore() {
    ByteArrayOutputStream bOut;
    DataOutputStream dOut;
    byte[] data;
    RecordEnumeration em;

    try {
      em = rs.enumerateRecords(null, null, false);
    }
    catch (RecordStoreNotOpenException ex) {
      return;
    }

    while(em.hasNextElement()){
      try {
        try {
          bOut = new ByteArrayOutputStream();
          dOut = new DataOutputStream(bOut);

          for (int i = 0; i < SIZE; i++) {
            dOut.writeShort(scoreRecords[i]);
            dOut.writeUTF(userName[i]);
            data = bOut.toByteArray();

            rs.setRecord(em.nextRecordId(), data, 0, data.length);
          }

          bOut.close();
          dOut.close();
        }
        catch (IOException ioe) {
          throw new RecordStoreException();
        }
      }
      catch (RecordStoreException rse) {
      }
    }
  }

  /**
   * 对分数进行排序
   */
  private static void sortScore() {
    int temp = -1;
    String str;
    for(int i=0; i<SIZE; i++)
      for(int j=i; j<SIZE; j++){
        if(scoreRecords[i] < scoreRecords[j]){
          temp = scoreRecords[i];
          scoreRecords[i] = scoreRecords[j];
          scoreRecords[j] = (short)temp;

          str = userName[i];
          userName[i] = userName[j];
          userName[j] = str;
        }
      }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -