Project

General

Profile

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

magiccube / src / main / java / org / distorted / scores / RubikScores.java @ f0e87514

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  private long[][][] mRecords;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
  public RubikScores()
40
    {
41
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
42

    
43
    for(int i=0; i<NUM_OBJECTS; i++)
44
      for(int j=0; j<MAX_SIZE; j++)
45
        for(int k=0; k<MAX_SCRAMBLE; k++)
46
          {
47
          mRecords[i][j][k] = NO_RECORD;
48
          }
49
    }
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public void savePreferences(SharedPreferences.Editor editor)
54
    {
55
    StringBuilder builder = new StringBuilder();
56
    RubikObjectList list;
57
    int[] sizes;
58
    int length;
59

    
60
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
61
      {
62
      builder.setLength(0);
63

    
64
      for(int object=0; object<NUM_OBJECTS; object++)
65
        {
66
        list = RubikObjectList.getObject(object);
67
        sizes = list.getSizes();
68
        length = sizes.length;
69

    
70
        for(int size=0; size<length; size++)
71
          {
72
          builder.append(list.name());
73
          builder.append("_");
74
          builder.append(sizes[size]);
75
          builder.append("=");
76
          builder.append(mRecords[object][size][scramble]);
77
          builder.append(" ");
78
          }
79
        }
80

    
81
      editor.putString("record"+scramble, builder.toString());
82
      }
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  public void restorePreferences(SharedPreferences preferences)
88
    {
89
    String recordStr, subStr, nameStr, sizeStr, timeStr;
90
    int start, end, equals, underscore;
91
    int object, size, time;
92

    
93
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
94
      {
95
      start = end = 0;
96
      recordStr = preferences.getString("record"+scramble, "");
97

    
98
      //android.util.Log.e("solving", scramble+" record string: "+recordStr);
99

    
100
      while( end!=-1 )
101
        {
102
        end = recordStr.indexOf(" ", start);
103

    
104
        if( end==-1 ) subStr = recordStr.substring(start);
105
        else          subStr = recordStr.substring(start,end);
106

    
107
        start = end+1;
108

    
109
        underscore = subStr.indexOf("_");
110
        equals = subStr.indexOf("=");
111

    
112
        if( underscore>=0 && equals>=0 )
113
          {
114
          nameStr = subStr.substring(0,underscore);
115
          sizeStr = subStr.substring(underscore+1, equals);
116
          timeStr = subStr.substring(equals+1);
117

    
118
          object = RubikObjectList.getOrdinal(nameStr);
119
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
120
          time   = Integer.parseInt(timeStr);
121

    
122
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
123
            {
124
            mRecords[object][size][scramble] = time;
125

    
126
            if( time<Integer.MAX_VALUE )
127
              {
128
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
129
              }
130
            }
131
          else
132
            {
133
            android.util.Log.e("solv", "error: object="+object+" size="+size);
134
            }
135
          }
136
        }
137
      }
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  public void newRecord(int object, int size, int scramble, long timeTaken)
143
    {
144
    int maxsize = RubikObjectList.getObject(object).getSizes().length;
145

    
146
    if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
147
      {
148
      if( mRecords[object][size][scramble-1]> timeTaken )
149
        {
150
        mRecords[object][size][scramble-1] = timeTaken;
151
        android.util.Log.e("solv","new record!");
152
        }
153
      }
154
    }
155
  }
(1-1/2)