开发者

android: final score and high score

On completing my Android game I want the user to compare his/her score with the high score. To do this I stored the current high score in a SQLite database. But I think my method (which seems to work) is clumsy and ugly:

//in the final score activity, which has been sent the users score from the game.
if (extras != null){
            //get users score
        SCORE = extras.getLong("Score");
            //create DB if necessary, otherwise open
        startDatabase();

    /tv is a text view (defined outside this code snippet)
    tv.setText("Your score: "+SCORE+"  \n"+"Top Score: "+ getHighScore());

    }
}

//opens or creates a DB. If it is created insert current score.
public void startDatabase(){
     SQLiteDatabase myDB = null;


     try{    
         myDB = this.openOrCreateDatabase(DB_NAME, 0,null);

         myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                 + SCORE_TABLE
                 + " (key VARCHAR," 
                 + " score NUM)");


     }catch(SQLException e){
         myDB.close();
        }


     Cursor c1 = myDB.rawQuery("SELECT * FROM "+ SCORE_TABLE ,null);

     c1.moveToNext();


    Long HIGHSCORE=0l;

            //try to get current high score.
    try{
    HIGHSCORE = c1.getLong(c1.getColumnIndex("score"));
    }

            //DB score is empty. Fill it with current score. This is the initial high                    `               //score.
    catch(IndexOutOfBoundsException e){
         myDB.execSQL("INSERT INTO "+ SCORE_TABLE+"(score)                        
                                            VALUES('"+SCORE+"')" );
         myDB.close();

    }

    c1.close();
    myDB.close();

}

The next method retrieves the current high score, and if necessary inputs a new highscore.

//returns the high score. also inputs new score as high score if it is high enough.
public long getHighScore(){
    SQLiteDatabase myDB = null;

    myD开发者_如何学CB = this.openOrCreateDatabase(DB_NAME, 0,null);

    Cursor c1 = myDB.rawQuery("SELECT * FROM "+ SCORE_TABLE ,null);

    c1.moveToNext();

    Long HIGHSCORE=0l;
    try{
    HIGHSCORE = c1.getLong(c1.getColumnIndex("score"));
    }
    catch(IndexOutOfBoundsException e){



    }

    //if user score is higher than high score...
     if(HIGHSCORE<=SCORE){
         myDB.execSQL("UPDATE "+ SCORE_TABLE+"  SET score= '"+SCORE+"'" );
         HIGHSCORE=SCORE;
         myDB.close();
     }

    c1.close();
    myDB.close();
    return HIGHSCORE;
}

I dont particularly like the code. Initially I thought doing the ifnal score/high score comparison would be a piece of cake, even with my basic knowledge of SQL. I think I made a mountian out of a molehill with this. Is there a better way to do this?


Well it appears you are only saving the highest score - and not all the scores, or even the top n scores. If this is the case, it is probably easier to just save the high score in a sharedpreference

public long getHighScore(){
SharedPreferences pp = PreferenceManager
            .getDefaultSharedPreferences(context);


if(score>pp.getLong("highscore",0l)){
    Editor pe=(Editor) pp.edit();
    pe.putLong("highscore",score);
    pe.commit();
    return score;
 } else {return pp.getLong("highscore",0l);}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜