Project

General

Profile

Download (6.62 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / scores / RubikScores.java @ 714292f1

1 f0e87514 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.scores;
21
22
import android.content.SharedPreferences;
23
import org.distorted.object.RubikObjectList;
24
25
import static org.distorted.object.RubikObjectList.MAX_SIZE;
26
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
27
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
28
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
// hold my own scores
31
32
public class RubikScores
33
  {
34
  private static final long NO_RECORD = Integer.MAX_VALUE;
35 714292f1 Leszek Koltunski
  private static RubikScores mThis;
36
37 f0e87514 Leszek Koltunski
  private long[][][] mRecords;
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 714292f1 Leszek Koltunski
  private RubikScores()
42 f0e87514 Leszek Koltunski
    {
43
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
44
45
    for(int i=0; i<NUM_OBJECTS; i++)
46
      for(int j=0; j<MAX_SIZE; j++)
47
        for(int k=0; k<MAX_SCRAMBLE; k++)
48
          {
49
          mRecords[i][j][k] = NO_RECORD;
50
          }
51
    }
52
53 714292f1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
  public static RubikScores getInstance()
56
    {
57
    if( mThis==null )
58
      {
59
      mThis = new RubikScores();
60
      }
61
62
    return mThis;
63
    }
64
65 f0e87514 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
  public void savePreferences(SharedPreferences.Editor editor)
68
    {
69
    StringBuilder builder = new StringBuilder();
70
    RubikObjectList list;
71
    int[] sizes;
72
    int length;
73
74
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
75
      {
76
      builder.setLength(0);
77
78
      for(int object=0; object<NUM_OBJECTS; object++)
79
        {
80
        list = RubikObjectList.getObject(object);
81
        sizes = list.getSizes();
82
        length = sizes.length;
83
84
        for(int size=0; size<length; size++)
85
          {
86
          builder.append(list.name());
87
          builder.append("_");
88
          builder.append(sizes[size]);
89
          builder.append("=");
90
          builder.append(mRecords[object][size][scramble]);
91
          builder.append(" ");
92
          }
93
        }
94
95
      editor.putString("record"+scramble, builder.toString());
96
      }
97
    }
98
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
101
  public void restorePreferences(SharedPreferences preferences)
102
    {
103
    String recordStr, subStr, nameStr, sizeStr, timeStr;
104
    int start, end, equals, underscore;
105
    int object, size, time;
106
107
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
108
      {
109
      start = end = 0;
110
      recordStr = preferences.getString("record"+scramble, "");
111
112
      //android.util.Log.e("solving", scramble+" record string: "+recordStr);
113
114
      while( end!=-1 )
115
        {
116
        end = recordStr.indexOf(" ", start);
117
118
        if( end==-1 ) subStr = recordStr.substring(start);
119
        else          subStr = recordStr.substring(start,end);
120
121
        start = end+1;
122
123
        underscore = subStr.indexOf("_");
124
        equals = subStr.indexOf("=");
125
126
        if( underscore>=0 && equals>=0 )
127
          {
128
          nameStr = subStr.substring(0,underscore);
129
          sizeStr = subStr.substring(underscore+1, equals);
130
          timeStr = subStr.substring(equals+1);
131
132
          object = RubikObjectList.getOrdinal(nameStr);
133
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
134
          time   = Integer.parseInt(timeStr);
135
136
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
137
            {
138
            mRecords[object][size][scramble] = time;
139
140
            if( time<Integer.MAX_VALUE )
141
              {
142
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
143
              }
144
            }
145
          else
146
            {
147
            android.util.Log.e("solv", "error: object="+object+" size="+size);
148
            }
149
          }
150
        }
151
      }
152
    }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
  public void newRecord(int object, int size, int scramble, long timeTaken)
157
    {
158
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
159
160
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
161
      {
162
      if( mRecords[object][size][scramble-1]> timeTaken )
163
        {
164
        mRecords[object][size][scramble-1] = timeTaken;
165 714292f1 Leszek Koltunski
        android.util.Log.e("RubikScores","new record: ("+object+","+size+","+scramble+") ="+timeTaken);
166 f0e87514 Leszek Koltunski
        }
167
      }
168
    }
169 714292f1 Leszek Koltunski
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172
  public long getRecord(int object, int size, int scramble)
173
    {
174
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
175
176
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
177
      {
178
      return mRecords[object][size][scramble];
179
      }
180
181
    return -1;
182
    }
183 f0e87514 Leszek Koltunski
  }