-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
297 lines (274 loc) · 10.5 KB
/
Copy pathMainActivity.java
File metadata and controls
297 lines (274 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.example.itay.sumika;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
final int maxNumOfSquare =7, minNumOfSquare =3;
int validSpace=1, timeForMove=0;//the spacing is initialize between 1 to 3, the time limit to off
boolean onePlayer=false;
int compLevel=0;// 0 is without comp, 1 is easy, 2 is medium, 3 is hard
ImageView gameImage;
SquareImageView info;
TextView namOfSquareTv;
Button buttonVsComp, buttonVsFriend, buttonPractice, rightArrow, leftArrow, buttonTimer, buttonSpacing;
int numOfSquareCounter = 3; //3=3on3, 4=4on4, 5=5on5, 6=6on6
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVariables();
changeFonts();
initializeIntentListeners();
initializeMenuListeners();
}
private void initializeVariables() {
buttonVsComp = findViewById(R.id.buttonVsComp);
buttonVsFriend = findViewById(R.id.buttonVsFriend);
buttonPractice = findViewById(R.id.practice);
buttonTimer= findViewById(R.id.buttonTimer);
buttonSpacing=findViewById(R.id.buttonSpaceing);
rightArrow = findViewById(R.id.rightArrowButton);
leftArrow = findViewById(R.id.leftArrowButton);
gameImage = findViewById(R.id.imageViewGame);
info=findViewById(R.id.info);
namOfSquareTv = findViewById(R.id.numOfSqure);
}
public void changeFonts(){
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/concertone-regular.ttf");
namOfSquareTv.setTypeface(face);
buttonVsComp.setTypeface(face);
buttonVsFriend.setTypeface(face);
buttonPractice.setTypeface(face);
}
private void initializeIntentListeners(){
final Intent intentToGame = new Intent(this, GameActivity.class);
buttonVsComp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onePlayer=false;
createDialog(intentToGame);
}
});
buttonVsFriend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compLevel=0;
onePlayer=false;
putExtraForIntent(intentToGame);
startActivity(intentToGame);
}
});
buttonPractice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
compLevel=0;
onePlayer=true;
putExtraForIntent(intentToGame);
startActivity(intentToGame);
}
});
final Intent infoIntent=new Intent(this,InfoActivity.class);
info.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(infoIntent);
}
});
}
private void initializeMenuListeners(){
leftArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(updateNumOfSquare(false)){
updateGameSizeMenu();
}
}
});
rightArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(updateNumOfSquare(true)){
updateGameSizeMenu();
}
}
});
buttonSpacing.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateSpacing();
}
});
buttonTimer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
updateTimer();
}
});
}
public void putExtraForIntent(Intent intent){
intent.putExtra("onePlayer", onePlayer);
intent.putExtra("compLevel", compLevel);
intent.putExtra("numOfSqure", numOfSquareCounter);
intent.putExtra("timeLimit", timeForMove);
intent.putExtra("spacingCounter", validSpace);
intent.putExtra("validSpace", validSpace);
intent.putExtra("timeForMove",timeForMove);
}
//returns true if did updated, false if not
public boolean updateNumOfSquare(boolean clickRight) {
boolean returnFlag = false;
if (clickRight) {
if (numOfSquareCounter < maxNumOfSquare) {
numOfSquareCounter++;
returnFlag = true;
}
} else {
if (numOfSquareCounter > minNumOfSquare) {
numOfSquareCounter--;
returnFlag = true;
}
}
return returnFlag;
}
public void updateGameSizeMenu() {
namOfSquareTv.setText(numOfSquareCounter+" X "+numOfSquareCounter);
switch (numOfSquareCounter) {
case 3:
gameImage.setImageResource(R.drawable.image3on3);
break;
case 4:
gameImage.setImageResource(R.drawable.image4on4);
break;
case 5:
gameImage.setImageResource(R.drawable.image5on5);
break;
case 6:
gameImage.setImageResource(R.drawable.image6on6);
break;
case 7:
gameImage.setImageResource(R.drawable.image7on7);
}
}
public void updateSpacing() {
switch (validSpace) {
case 1:
validSpace=2;
buttonSpacing.setBackgroundResource(R.drawable.spacing2);
break;
case 2:
validSpace=3;
buttonSpacing.setBackgroundResource(R.drawable.spacing3);
break;
case 3:
validSpace = 1;
buttonSpacing.setBackgroundResource(R.drawable.spacing1);
break;
}
}
public void updateTimer(){
switch (timeForMove){
case 0:
timeForMove=5;
buttonTimer.setBackgroundResource(R.drawable.stopwatch_5);
break;
case 5:
timeForMove=10;
buttonTimer.setBackgroundResource(R.drawable.stopwatch_10);
break;
case 10:
timeForMove=15;
buttonTimer.setBackgroundResource(R.drawable.stopwatch_15);
break;
case 15:
timeForMove=20;
buttonTimer.setBackgroundResource(R.drawable.stopwatch_20);
break;
case 20:
timeForMove=30;
buttonTimer.setBackgroundResource(R.drawable.stopwatch_30);
break;
case 30:
timeForMove=60;
buttonTimer.setBackgroundResource(R.drawable.stopwatch_60);
break;
case 60:
timeForMove=0;
buttonTimer.setBackgroundResource(R.drawable.stopwatch_off);
break;
}
}
public void createDialog(Intent intentToGame){
final Intent intent=intentToGame;
AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
mBuilder.setPositiveButton(R.string.hard_level, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
compLevel=3;
dialogInterface.dismiss();
putExtraForIntent(intent);
startActivity(intent);
}
});
mBuilder.setNeutralButton(R.string.easy_level, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
compLevel=1;
dialogInterface.dismiss();
putExtraForIntent(intent);
startActivity(intent);
}
});
mBuilder.setNegativeButton(R.string.mediun_level, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
compLevel=2;
dialogInterface.dismiss();
putExtraForIntent(intent);
startActivity(intent);
}
});
AlertDialog alertDialog=mBuilder.create();
setDialogCustomTitle(alertDialog);
alertDialog.show();
setDialogButtonToMiddle(alertDialog);
}
public void setDialogCustomTitle(AlertDialog alertDialog){
TextView title=new TextView(this);
title.setText(R.string.titleDialog);
title.setBackgroundColor(getResources().getColor(R.color.pinkSqure_uneven));
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
title.setGravity(Gravity.CENTER);
alertDialog.setCustomTitle(title);
}
public void setDialogButtonToMiddle(AlertDialog alertDialog){
Button btnPositive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button btnNeutral = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
Button btnNegative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
layoutParams.weight = 10;
btnPositive.setLayoutParams(layoutParams);
btnNeutral.setLayoutParams(layoutParams);
btnNegative.setLayoutParams(layoutParams);
}
public void changeButtonDialogColor(AlertDialog alertDialog,int color){
Button btnPositive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
Button btnNeutral = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL);
Button btnNegative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
btnPositive.setTextColor(color);
btnNeutral.setTextColor(color);
btnNegative.setTextColor(color);
}
}