Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have made a game Minesweeper in Java applet and i want to store the winning person's name in database but i am not able to do it and also i want to make a button score in which on clicking it the list of winner got open in a new window
please help
Posted
Comments
Sergey Alexandrovich Kryukov 13-Apr-12 15:05pm    
Don't you think such a heavy thing as database will be a huge overkill for a small game? Better use per-user or "All Users" "Application Data" directory; a simple XML file will do the trick.
--SA
TorstenH. 14-Apr-12 6:45am    
I already answered that same question 2 days ago: need help for project

where is the problem? If you need more information you'll have to communicate with us.

1 solution

Simplest solution is to have a serializable class to hold the high scores.
Here's one I prepared earlier:

Java
class HighScore
        implements Serializable {
    private Score[] scores;
    
    HighScore() {
        this.scores = new Score[10];
    }
    
    addScore(final Score score) {
        Score swap = score;
        
        for (int s = 0;
                s < 10;
                s++) {
            if (swap.getScore() > this.scores[s].getScore()) {
                Score temp = this.scores[s];
                this.scores[s] = swap;
                swap = temp;
            }
        }
    }
    
    Score getScore(int score) {
        if (score < 0 || score > 9) {
            return null;
        }
        return this.scores[score];
    }
}


Java
class Score
        implements Serializable {
    private final String user;
    private final int score;
    
    Score (final String user, final int score) {
        this.user = user;
        this.score = score;
    }
    
    String getUser() {
        return this.user;
    }
    int getScore() {
        return this.score;
    }
}


Then in the game class you can save and load it thus:
Java
class Game {
    private HighScore highScore;

    void saveHighScore() {
        try { 
            FileOutputStream fos = new FileOutputStream("highScore.dat"); 
            ObjectOutputStream oos = new ObjectOutputStream(fos); 
            oos.writeObject(this.highScore); 
            oos.flush(); 
        } catch(IOException iex) {
            iex.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch(IOException iex) {
                    iex.printStackTrace();
                }
            }
        }
    }
    
    void loadHighScore () {
        try {
            FileInputStream fis = new FileInputStream("serial"); 
            ObjectInputStream ois = new ObjectInputStream(fis); 
            this.highScore = (HighScore)ois.readObject(); 
        } catch (IOException iex) {
            iex.printStackTrace();
            this.highScore = new HighScore();
        } catch (ClassNotFoundException cex) {
            cex.printStackTrace();
            this.highScore = new HighScore();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch(IOException iex) {
                    iex.printStackTrace();
                }
            }
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900