aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/com/camilstaps/rushhour/HighScore.java
blob: 1ddeb0fc38cc55224d3e3a7237cfa43d4d0b745c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
 *     Rush Hour Android app
 * Copyright (C) 2015 Randy Wanga, Jos Craaijo, Camil Staps
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package com.camilstaps.rushhour;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Highscore exists of name and score (amount of moves; so lower is better)
 * Created by camilstaps on 23-4-15.
 * Edited by Halzyn on 23-4-15.
 */
public class HighScore implements Comparable<HighScore> {

    private final int score;
    private final String name;

    public HighScore(int score, String name) {
        this.score = score;
        this.name = name;
    }

    /**
     * HighScore from json
     * @see #toString()
     * @param jsonString
     */
    public HighScore(String jsonString) {
        int temp_score = -1;
        String temp_name = null;
        try {
            JSONObject json = new JSONObject(jsonString);
            temp_score = json.getInt("score");
            temp_name = json.getString("name");
        } catch (JSONException e) {
        }
        score = temp_score;
        name = temp_name;
    }

    public int getScore() {
        return score;
    }

    public String getName() {
        return name;
    }

    @Override
    public int compareTo(HighScore other_score) {
        if (other_score.score < score)
        {
            return 1;
        }
        else if (other_score.score == score)
        {
            return 0;
        }
        else
        {
            return -1;
        }
    }

    /**
     * JSON representation
     * @see #HighScore(String)
     * @return
     */
    public String toString() {
        JSONObject json = new JSONObject();
        try {
            json.put("score", score);
            json.put("name", name);
        } catch (JSONException ex) {
        }
        return json.toString();
    }

}