1 |
ad9e8bb3
|
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.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 |
4888e97c
|
Leszek Koltunski
|
import org.distorted.object.RubikObjectList;
|
32 |
ad9e8bb3
|
Leszek Koltunski
|
|
33 |
0333d81e
|
Leszek Koltunski
|
import java.util.Timer;
|
34 |
|
|
import java.util.TimerTask;
|
35 |
|
|
|
36 |
ad9e8bb3
|
Leszek Koltunski
|
import static android.view.View.INVISIBLE;
|
37 |
4888e97c
|
Leszek Koltunski
|
import static org.distorted.object.RubikObjectList.NUM_OBJECTS;
|
38 |
|
|
import static org.distorted.object.RubikObjectList.MAX_SIZE;
|
39 |
329c0aeb
|
Leszek Koltunski
|
import static org.distorted.uistate.RubikStatePlay.MAX_SCRAMBLE;
|
40 |
ad9e8bb3
|
Leszek Koltunski
|
|
41 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
42 |
|
|
|
43 |
|
|
public class RubikStateSolving extends RubikStateAbstract
|
44 |
|
|
{
|
45 |
27a70eae
|
Leszek Koltunski
|
private static final long NO_RECORD = Integer.MAX_VALUE;
|
46 |
329c0aeb
|
Leszek Koltunski
|
|
47 |
0333d81e
|
Leszek Koltunski
|
private TextView mTime;
|
48 |
|
|
private Timer mTimer;
|
49 |
|
|
private long mStartTime;
|
50 |
|
|
private boolean mRunning;
|
51 |
|
|
|
52 |
4888e97c
|
Leszek Koltunski
|
private long[][][] mRecords;
|
53 |
329c0aeb
|
Leszek Koltunski
|
|
54 |
0333d81e
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
55 |
|
|
|
56 |
329c0aeb
|
Leszek Koltunski
|
RubikStateSolving()
|
57 |
|
|
{
|
58 |
4888e97c
|
Leszek Koltunski
|
mRecords = new long[NUM_OBJECTS][MAX_SIZE][MAX_SCRAMBLE];
|
59 |
329c0aeb
|
Leszek Koltunski
|
|
60 |
4888e97c
|
Leszek Koltunski
|
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 |
329c0aeb
|
Leszek Koltunski
|
}
|
67 |
|
|
|
68 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69 |
|
|
|
70 |
|
|
void leaveState(RubikActivity act)
|
71 |
ad9e8bb3
|
Leszek Koltunski
|
{
|
72 |
0333d81e
|
Leszek Koltunski
|
stopCounting();
|
73 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
74 |
|
|
|
75 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76 |
|
|
|
77 |
329c0aeb
|
Leszek Koltunski
|
void enterState(RubikActivity act)
|
78 |
ad9e8bb3
|
Leszek Koltunski
|
{
|
79 |
|
|
LayoutInflater inflater = act.getLayoutInflater();
|
80 |
|
|
|
81 |
|
|
// TOP ////////////////////////////
|
82 |
|
|
LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
|
83 |
|
|
layoutTop.removeAllViews();
|
84 |
0333d81e
|
Leszek Koltunski
|
mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
|
85 |
|
|
mTime.setText(R.string.ready);
|
86 |
|
|
layoutTop.addView(mTime);
|
87 |
ad9e8bb3
|
Leszek Koltunski
|
|
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 |
c86f9f1f
|
Leszek Koltunski
|
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 |
329c0aeb
|
Leszek Koltunski
|
{
|
138 |
c86f9f1f
|
Leszek Koltunski
|
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 |
329c0aeb
|
Leszek Koltunski
|
}
|
145 |
c86f9f1f
|
Leszek Koltunski
|
}
|
146 |
|
|
|
147 |
|
|
editor.putString("record"+scramble, builder.toString());
|
148 |
|
|
}
|
149 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
150 |
|
|
|
151 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152 |
|
|
|
153 |
|
|
public void restorePreferences(SharedPreferences preferences)
|
154 |
|
|
{
|
155 |
c86f9f1f
|
Leszek Koltunski
|
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 |
4888e97c
|
Leszek Koltunski
|
{
|
180 |
c86f9f1f
|
Leszek Koltunski
|
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 |
efa682e8
|
Leszek Koltunski
|
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 |
4888e97c
|
Leszek Koltunski
|
}
|
202 |
c86f9f1f
|
Leszek Koltunski
|
}
|
203 |
|
|
}
|
204 |
ad9e8bb3
|
Leszek Koltunski
|
}
|
205 |
0333d81e
|
Leszek Koltunski
|
|
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 |
329c0aeb
|
Leszek Koltunski
|
long timeTaken = System.currentTimeMillis()-mStartTime;
|
249 |
|
|
|
250 |
|
|
RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
|
251 |
4888e97c
|
Leszek Koltunski
|
int object = play.getObject();
|
252 |
|
|
int size = play.getSize();
|
253 |
329c0aeb
|
Leszek Koltunski
|
int scramble= play.getPicker();
|
254 |
4888e97c
|
Leszek Koltunski
|
int maxsize = RubikObjectList.getObject(object).getSizes().length;
|
255 |
329c0aeb
|
Leszek Koltunski
|
|
256 |
efa682e8
|
Leszek Koltunski
|
if( object>=0 && object<NUM_OBJECTS && size>=0 && size<maxsize && scramble>=1 && scramble<=MAX_SCRAMBLE )
|
257 |
329c0aeb
|
Leszek Koltunski
|
{
|
258 |
c86f9f1f
|
Leszek Koltunski
|
if( mRecords[object][size][scramble-1]> timeTaken )
|
259 |
329c0aeb
|
Leszek Koltunski
|
{
|
260 |
c86f9f1f
|
Leszek Koltunski
|
mRecords[object][size][scramble-1] = timeTaken;
|
261 |
329c0aeb
|
Leszek Koltunski
|
android.util.Log.e("solv","new record!");
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
return timeTaken;
|
266 |
0333d81e
|
Leszek Koltunski
|
}
|
267 |
|
|
|
268 |
|
|
return 0;
|
269 |
|
|
}
|
270 |
ad9e8bb3
|
Leszek Koltunski
|
}
|