2
2
3
3
4
4
import android .content .Context ;
5
+ import android .content .Intent ;
5
6
import android .content .SharedPreferences ;
6
7
import android .os .Bundle ;
7
8
import android .os .SystemClock ;
8
9
import android .view .LayoutInflater ;
9
10
import android .view .View ;
10
11
import android .view .ViewGroup ;
12
+ import android .widget .Button ;
11
13
import android .widget .Chronometer ;
14
+ import android .widget .EditText ;
15
+ import android .widget .Toast ;
12
16
13
17
import androidx .annotation .NonNull ;
14
18
import androidx .annotation .Nullable ;
19
+ import androidx .constraintlayout .widget .ConstraintLayout ;
20
+ import androidx .constraintlayout .widget .ConstraintSet ;
15
21
import androidx .fragment .app .Fragment ;
16
22
17
23
import com .google .gson .Gson ;
18
24
25
+ import ro .tav .pavgame .PavGameApplication ;
19
26
import ro .tav .pavgame .R ;
27
+ import ro .tav .pavgame .presentation .PavGameService ;
28
+ import ro .tav .pavgame .presentation .PavGameViewModel ;
29
+ import ro .tav .pavgame .presentation .notification .PavGameNotificationFactory ;
20
30
import ro .tav .pavgame .presentation .view .MainActivity ;
21
31
import timber .log .Timber ;
22
32
23
33
public class GameFragment extends Fragment {
24
34
private SharedPreferences sharedPreferences ; ///folosim sharedPreferences pt a stoca pt fiecare utilizator timpul total de joc (de cand a instalat aplicatia)
25
35
private final static String pavGameSharedPreference = "pavGameSharedPreference" ;
26
36
private Chronometer chronometer ;
37
+ private int lat ;
38
+ private int nrDala ;
39
+ private static final int nrGreseliMax = 5 ;
40
+ private int nrGreseli ;
41
+ private final int [][] matrix = new int [ 256 ][ 256 ];
42
+ private EditText input ;
43
+ private Boolean started ;
44
+ private Boolean finished ;
45
+ private Intent gameInProgressServiceIntent ;
27
46
28
47
public View onCreateView ( @ NonNull LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
29
48
return inflater .inflate ( R .layout .fragment_game , container , false );
@@ -36,6 +55,8 @@ public GameFragment() {
36
55
@ Override
37
56
public void onActivityCreated ( @ Nullable Bundle savedInstanceState ) {
38
57
super .onActivityCreated ( savedInstanceState );
58
+
59
+ //obtin din sharedPreferances cat timp am avut in app pana acum, pt a seta cronometrul
39
60
long timeSpent ; //timpul petrecut(cu precizie de minute) in joc
40
61
sharedPreferences = super .requireContext ().getSharedPreferences ( pavGameSharedPreference , Context .MODE_PRIVATE );
41
62
try {
@@ -46,20 +67,240 @@ public void onActivityCreated( @Nullable Bundle savedInstanceState ) {
46
67
timeSpent = 0 ;
47
68
}
48
69
70
+ //setez cronometrul
49
71
chronometer = requireView ().findViewById ( R .id .chronometer );
50
72
chronometer .setBase ( SystemClock .elapsedRealtime () - timeSpent * 60000 );//asa se initializeaza cronometrul
51
73
chronometer .start ();
74
+
75
+ //intent pt serviciul din foreground ce se creeaza cand e un joc in desfasurare
76
+ gameInProgressServiceIntent = new Intent ( requireContext (), PavGameService .class );
77
+
78
+ //adaugam listener pt butonul de joc, pt a putea incepe jocul
79
+ Button button = ( Button ) requireView ().findViewById ( R .id .startGameButton );
80
+ button .setOnClickListener ( new Button .OnClickListener () {
81
+ @ Override
82
+ public void onClick ( View v ) {
83
+ startGame ( v );
84
+ }
85
+ } );
52
86
}
53
87
54
88
@ Override
55
89
public void onDestroyView () {
56
90
super .onDestroyView ();
91
+ //luam noul timpul total petrecut in aplicatie (cat era initial + cat s-a jucat utilizatorul acum)
57
92
long newTime = SystemClock .elapsedRealtime () - chronometer .getBase ();//milisecunde petrecut in total, dupa acest joc
58
93
newTime /= 1000 ; //secunde
59
94
newTime /= 60 ; //minute
60
95
96
+ //convertim acest numar (valoare rotunjita la minute) tinut pe Long la json
97
+ //si ii fac update in shared preferances
61
98
Gson gson = new Gson ();
62
99
String json = gson .toJson ( newTime ); ///convertim acest timp la un json string
63
100
sharedPreferences .edit ().putString ( MainActivity .getUserName () + "" , json ).apply (); ///updatam valoarea in shared preferances
101
+
102
+ stopGameService ();
64
103
}
104
+
105
+ public void startGame ( View view ) { //crearea si initializarea butoanelor jocului
106
+ String s = "" , messege ;
107
+ started = finished = Boolean .FALSE ;
108
+ input = requireView ().findViewById ( R .id .pavGameInputText );
109
+
110
+ try {
111
+ //obtinem nr introdus si calculam latura
112
+ s = input .getText ().toString ();
113
+ lat = 1 << Integer .parseInt ( s );
114
+ input .setHint ( R .string .pozDala );
115
+ nrDala = nrGreseli = 0 ;
116
+ ConstraintLayout pavGameBoard = requireView ().findViewById ( R .id .pavGameBoard );
117
+ pavGameBoard .removeAllViews ();
118
+
119
+ for ( int i = 0 ; i < lat ; i ++ ) {
120
+ for ( int j = 0 ; j < lat ; j ++ ) {
121
+ //adaugam cate un buton pt fiecare casuta
122
+ matrix [ i ][ j ] = 0 ;
123
+ Button button = new Button ( getContext () );
124
+ button .setLayoutParams ( new ConstraintLayout .LayoutParams ( ConstraintLayout .LayoutParams .MATCH_CONSTRAINT_SPREAD , ConstraintLayout .LayoutParams .WRAP_CONTENT ) );
125
+ button .setId ( lat * i + j );
126
+ button .setOnClickListener ( new handleClick () );//listener pt a putea selecta optiunea
127
+ pavGameBoard .addView ( button );
128
+
129
+
130
+ //setam constraint-urile
131
+ ConstraintSet constraintSet = new ConstraintSet ();
132
+ constraintSet .clone ( pavGameBoard );
133
+
134
+ //legatura in sus
135
+ if ( i == 0 ) { //top->topOfParrent
136
+ constraintSet .connect ( button .getId (), ConstraintSet .TOP , R .id .pavGameBoard , ConstraintSet .TOP );
137
+ } else {//top->bottomOfButton
138
+ constraintSet .connect ( button .getId (), ConstraintSet .TOP , button .getId () - lat , ConstraintSet .BOTTOM );
139
+ //bottomOfButton->top
140
+ constraintSet .connect ( button .getId () - lat , ConstraintSet .BOTTOM , button .getId (), ConstraintSet .TOP );
141
+ }
142
+
143
+ //legatura la stanga
144
+ if ( j == 0 ) {//left->leftOfParrent
145
+ constraintSet .connect ( button .getId (), ConstraintSet .START , R .id .pavGameBoard , ConstraintSet .START );
146
+ } else {//left->rightOfButton
147
+ constraintSet .connect ( button .getId (), ConstraintSet .START , button .getId () - ( 1 ), ConstraintSet .END );
148
+ //rightOfButton->left
149
+ constraintSet .connect ( button .getId () - ( 1 ), ConstraintSet .END , button .getId (), ConstraintSet .START );
150
+ }
151
+ //legatura la dreapta
152
+ //right->endOfParrent
153
+ constraintSet .connect ( button .getId (), ConstraintSet .END , R .id .pavGameBoard , ConstraintSet .END );
154
+
155
+ //legatura in jos
156
+ //bottom->bottomOfParrent
157
+ constraintSet .connect ( button .getId (), ConstraintSet .BOTTOM , R .id .pavGameBoard , ConstraintSet .BOTTOM );
158
+
159
+ //aplicam modificarile
160
+ constraintSet .applyTo ( pavGameBoard );
161
+ }
162
+ }
163
+ //modificam mesajul de pe buton si il dezactivam
164
+ messege = getString ( R .string .copac );
165
+ Button gameButton = ( Button ) view ;
166
+ gameButton .setEnabled ( false );
167
+ gameButton .setText ( getString ( R .string .start_game ) );
168
+ requireActivity ().startService ( gameInProgressServiceIntent );
169
+
170
+ } catch ( Exception e ) {
171
+ if ( s .length () == 0 ) //daca s-a introdus stringul vid
172
+ messege = getString ( R .string .errLipsaDimLatura );
173
+ else
174
+ messege = getString ( R .string .unknownError );
175
+ }
176
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_LONG ).show ();
177
+ }
178
+
179
+ private class handleClick implements View .OnClickListener { //controller-ele in joc
180
+ //un butonel din tabla mare
181
+ private Button button ;
182
+
183
+ //colorare buton
184
+ private void setButon ( int nr ) {
185
+ button = requireView ().findViewById ( nr );
186
+ button .setText ( String .valueOf ( nrDala ) );
187
+ button .setBackgroundColor ( getResources ().getColor ( R .color .colorPrimary , requireContext ().getTheme () ) );
188
+ button .setTextColor ( getResources ().getColor ( R .color .colorAccent , requireContext ().getTheme () ) );
189
+ }
190
+
191
+ public void onClick ( View view ) {
192
+ String messege , s1 = "" , s2 , result ;
193
+ button = ( Button ) view ;
194
+ //luam pozitia dalei de inserare
195
+ input = requireView ().findViewById ( R .id .pavGameInputText );
196
+ int l = button .getId () / lat ;
197
+ int c = button .getId () % lat ;
198
+ if ( !started ) {//initializarea primul buton pt gaura
199
+ started = true ;
200
+ button .setBackgroundColor ( getResources ().getColor ( R .color .colorAccent , requireContext ().getTheme () ) );
201
+ button .setText ( "0" );
202
+ button .setTextColor ( getResources ().getColor ( R .color .colorPrimary , requireContext ().getTheme () ) );
203
+ matrix [ l ][ c ] = -1 ;
204
+ button = requireView ().findViewById ( R .id .startGameButton );
205
+ button .setText ( R .string .alegeDala );
206
+ } else if ( nrGreseli > nrGreseliMax ) { //pierdere
207
+ messege = getString ( R .string .esec ) + " :(" ;
208
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_LONG ).show ();
209
+ } else if ( nrDala * 3 + 1 == lat * lat ) { //castig
210
+ messege = getString ( R .string .victorie ) + " :)" ;
211
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_LONG ).show ();
212
+ } else {//jocul continua
213
+ try {
214
+ s1 = input .getText ().toString ();
215
+ int tipDala = Integer .parseInt ( s1 ); //obtinem val de int din input
216
+ messege = getString ( R .string .mutareIncorecta );
217
+ if ( tipDala < 1 || tipDala > 4 ) ///verificam sa fie un tip de pozitie correct
218
+ throw new Exception ( getString ( R .string .dalaIncorecta ) );
219
+ else if ( tipDala == 1 ) {//mutarile
220
+ if ( matrix [ l ][ c ] == 0 && matrix [ l + 1 ][ c ] == 0 && matrix [ l ][ c + 1 ] == 0 && ( l + 1 ) < lat && ( c + 1 ) < lat ) {
221
+ nrDala ++;
222
+ matrix [ l ][ c ] = matrix [ l + 1 ][ c ] = matrix [ l ][ c + 1 ] = nrDala ;
223
+ setButon ( lat * l + c );
224
+ setButon ( lat * ( l + 1 ) + c );
225
+ setButon ( lat * l + ( c + 1 ) );
226
+ } else {
227
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_SHORT ).show ();
228
+ nrGreseli ++;
229
+ }
230
+ } else if ( tipDala == 2 ) {
231
+ if ( matrix [ l ][ c ] == 0 && matrix [ l + 1 ][ c ] == 0 && matrix [ l ][ c - 1 ] == 0 && ( l + 1 ) < lat ) {
232
+ nrDala ++;
233
+ matrix [ l ][ c ] = matrix [ l + 1 ][ c ] = matrix [ l ][ c - 1 ] = nrDala ;
234
+ setButon ( lat * l + c );
235
+ setButon ( lat * ( l + 1 ) + c );
236
+ setButon ( lat * l + ( c - 1 ) );
237
+ } else {
238
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_SHORT ).show ();
239
+ nrGreseli ++;
240
+ }
241
+ } else if ( tipDala == 3 ) {
242
+ if ( matrix [ l ][ c ] == 0 && matrix [ l - 1 ][ c ] == 0 && matrix [ l ][ c + 1 ] == 0 && ( c + 1 ) < lat ) {
243
+ nrDala ++;
244
+ matrix [ l ][ c ] = matrix [ l - 1 ][ c ] = matrix [ l ][ c + 1 ] = nrDala ;
245
+ setButon ( lat * l + c );
246
+ setButon ( lat * ( l - 1 ) + c );
247
+ setButon ( lat * l + ( c + 1 ) );
248
+ } else {
249
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_SHORT ).show ();
250
+ nrGreseli ++;
251
+ }
252
+ } else {
253
+ if ( matrix [ l ][ c ] == 0 && matrix [ l - 1 ][ c ] == 0 && matrix [ l ][ c - 1 ] == 0 ) {
254
+ nrDala ++;
255
+ matrix [ l ][ c ] = matrix [ l - 1 ][ c ] = matrix [ l ][ c - 1 ] = nrDala ;
256
+ setButon ( lat * l + c );
257
+ setButon ( lat * ( l - 1 ) + c );
258
+ setButon ( lat * l + ( c - 1 ) );
259
+ } else {
260
+ nrGreseli ++;
261
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_SHORT ).show ();
262
+ }
263
+ }
264
+ } catch ( Exception e ) {
265
+ if ( s1 .length () == 0 )
266
+ messege = getString ( R .string .errLipsaDimLatura );
267
+ else
268
+ messege = getString ( R .string .dalaIncorecta );
269
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_SHORT ).show ();
270
+ }
271
+ }
272
+ if ( finished == Boolean .FALSE && ( nrDala * 3 + 1 == lat * lat || nrGreseli > nrGreseliMax ) ) {
273
+ finished = Boolean .TRUE ;//daca se ajunge la finish
274
+ requireView ().findViewById ( R .id .startGameButton ).setEnabled ( true );
275
+ button = requireView ().findViewById ( R .id .startGameButton );
276
+ if ( nrDala * 3 + 1 == lat * lat ) {
277
+ messege = getString ( R .string .victorie ) + " :)" ;
278
+ button .setText ( messege );
279
+ result = "Win" ;
280
+ s1 = getString ( R .string .Win );
281
+ s2 = getString ( R .string .victorie ) + ", " + MainActivity .getUserName () + "!" ;
282
+ } else {
283
+ messege = getString ( R .string .esec ) + " :(" ;
284
+ button .setText ( messege );
285
+ result = "Lose" ;
286
+ s1 = getString ( R .string .Lose );
287
+ s2 = getString ( R .string .esec ) + ", " + MainActivity .getUserName () + "!" ;
288
+ }
289
+
290
+ //adaugam jocul folosindu-ne de viewModel
291
+ PavGameViewModel .addResult ( MainActivity .getUserName (), result , "Game Type: " + lat + "x" + lat );
292
+ PavGameApplication .getNotificationManager ().notify ( PavGameNotificationFactory .getHelloNotificationId (),
293
+ PavGameNotificationFactory .createCustomHelloNotification ( getContext (),
294
+ s1 , s2 ) );
295
+ Toast .makeText ( getContext (), messege , Toast .LENGTH_LONG ).show ();
296
+ stopGameService (); //optimi si serviciul cand se castiga/pierde jocul
297
+ }
298
+ }
299
+
300
+ }
301
+
302
+ private void stopGameService () {
303
+ requireActivity ().stopService ( gameInProgressServiceIntent );//oprirea serviciului
304
+ }
305
+
65
306
}
0 commit comments