Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ efa682e8

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.uistate;
21

    
22
import android.content.SharedPreferences;
23
import android.util.DisplayMetrics;
24
import android.view.LayoutInflater;
25
import android.widget.Button;
26
import android.widget.LinearLayout;
27
import android.widget.TextView;
28

    
29
import org.distorted.magic.R;
30
import org.distorted.magic.RubikActivity;
31
import org.distorted.object.RubikObjectList;
32

    
33
import java.util.Timer;
34
import java.util.TimerTask;
35

    
36
import static android.view.View.INVISIBLE;
37
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
38
import static org.distorted.object.RubikObjectList.MAX_SIZE;
39
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
public class RubikStateSolving extends RubikStateAbstract
44
  {
45
  private static final long NO_RECORD = Integer.MAX_VALUE;
46

    
47
  private TextView mTime;
48
  private Timer mTimer;
49
  private long mStartTime;
50
  private boolean mRunning;
51

    
52
  private long[][][] mRecords;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  RubikStateSolving()
57
    {
58
    mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
59

    
60
    for(int i=0; i<NUM_OBJECTS; i++)
61
      for(int j=0; j<MAX_SIZE; j++)
62
        for(int k=0; k<MAX_SCRAMBLE; k++)
63
          {
64
          mRecords[i][j][k] = NO_RECORD;
65
          }
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  void leaveState(RubikActivity act)
71
    {
72
    stopCounting();
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  void enterState(RubikActivity act)
78
    {
79
    LayoutInflater inflater = act.getLayoutInflater();
80

    
81
    // TOP ////////////////////////////
82
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
83
    layoutTop.removeAllViews();
84
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
85
    mTime.setText(R.string.ready);
86
    layoutTop.addView(mTime);
87

    
88
    // BOT ////////////////////////////
89
    LinearLayout layoutBot = act.findViewById(R.id.mainBar);
90
    layoutBot.removeAllViews();
91

    
92
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
93
    float scale = metrics.density;
94
    int size = (int)(60*scale +0.5f);
95
    int padding = (int)(5*scale + 0.5f);
96
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size,0.5f);
97

    
98
    Button buttonL = new Button(act);
99
    buttonL.setLayoutParams(params);
100
    buttonL.setId(BUTTON_ID_BACK);
101
    buttonL.setPadding(padding,0,padding,0);
102
    buttonL.setText(R.string.back);
103
    buttonL.setOnClickListener(act);
104
    layoutBot.addView(buttonL);
105

    
106
    Button buttonR = new Button(act);
107
    buttonR.setLayoutParams(params);
108
    buttonR.setId(BUTTON_ID_BACK);
109
    buttonR.setPadding(padding,0,padding,0);
110
    buttonR.setText(R.string.back);
111
    buttonR.setOnClickListener(act);
112
    layoutBot.addView(buttonR);
113

    
114
    buttonL.setVisibility(INVISIBLE);
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  public void savePreferences(SharedPreferences.Editor editor)
120
    {
121
    StringBuilder builder = new StringBuilder();
122
    RubikObjectList list;
123
    int[] sizes;
124
    int length;
125

    
126
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
127
      {
128
      builder.setLength(0);
129

    
130
      for(int object=0; object<NUM_OBJECTS; object++)
131
        {
132
        list = RubikObjectList.getObject(object);
133
        sizes = list.getSizes();
134
        length = sizes.length;
135

    
136
        for(int size=0; size<length; size++)
137
          {
138
          builder.append(list.name());
139
          builder.append("_");
140
          builder.append(sizes[size]);
141
          builder.append("=");
142
          builder.append(mRecords[object][size][scramble]);
143
          builder.append(" ");
144
          }
145
        }
146

    
147
      editor.putString("record"+scramble, builder.toString());
148
      }
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  public void restorePreferences(SharedPreferences preferences)
154
    {
155
    String recordStr, subStr, nameStr, sizeStr, timeStr;
156
    int start, end, equals, underscore;
157
    int object, size, time;
158

    
159
    for(int scramble=0; scramble<MAX_SCRAMBLE; scramble++)
160
      {
161
      start = end = 0;
162
      recordStr = preferences.getString("record"+scramble, "");
163

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

    
166
      while( end!=-1 )
167
        {
168
        end = recordStr.indexOf(" ", start);
169

    
170
        if( end==-1 ) subStr = recordStr.substring(start);
171
        else          subStr = recordStr.substring(start,end);
172

    
173
        start = end+1;
174

    
175
        underscore = subStr.indexOf("_");
176
        equals = subStr.indexOf("=");
177

    
178
        if( underscore>=0 && equals>=0 )
179
          {
180
          nameStr = subStr.substring(0,underscore);
181
          sizeStr = subStr.substring(underscore+1, equals);
182
          timeStr = subStr.substring(equals+1);
183

    
184
          object = RubikObjectList.getOrdinal(nameStr);
185
          size   = RubikObjectList.getSize(object,Integer.parseInt(sizeStr));
186
          time   = Integer.parseInt(timeStr);
187

    
188
          if( object>=0 && object< NUM_OBJECTS && size>=0 && size<MAX_SIZE )
189
            {
190
            mRecords[object][size][scramble] = time;
191

    
192
            if( time<Integer.MAX_VALUE )
193
              {
194
              android.util.Log.e("solv", "Set record for: object="+object+" size="+size+" scramble="+scramble+" time: "+time);
195
              }
196
            }
197
          else
198
            {
199
            android.util.Log.e("solv", "error: object="+object+" size="+size);
200
            }
201
          }
202
        }
203
      }
204
    }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public void startCounting(final RubikActivity act)
209
    {
210
    if( !mRunning )
211
      {
212
      mRunning = true;
213
      mStartTime = System.currentTimeMillis();
214
      mTimer = new Timer();
215

    
216
      mTimer.scheduleAtFixedRate(new TimerTask()
217
        {
218
        @Override
219
        public void run()
220
          {
221
          act.runOnUiThread(new Runnable()
222
            {
223
            @Override
224
            public void run()
225
              {
226
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
227
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
228
              }
229
            });
230
          }
231
        }, 0, 1000);
232
      }
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  public long stopCounting()
238
    {
239
    if( mRunning )
240
      {
241
      if( mTimer!=null )
242
        {
243
        mTimer.cancel();
244
        mTimer = null;
245
        }
246
      mRunning = false;
247

    
248
      long timeTaken = System.currentTimeMillis()-mStartTime;
249

    
250
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
251
      int object  = play.getObject();
252
      int size    = play.getSize();
253
      int scramble= play.getPicker();
254
      int maxsize = RubikObjectList.getObject(object).getSizes().length;
255

    
256
      if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
257
        {
258
        if( mRecords[object][size][scramble-1]> timeTaken )
259
          {
260
          mRecords[object][size][scramble-1] = timeTaken;
261
          android.util.Log.e("solv","new record!");
262
          }
263
        }
264

    
265
      return timeTaken;
266
      }
267

    
268
    return 0;
269
    }
270
  }
(5-5/5)