-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.java
More file actions
363 lines (338 loc) · 12.4 KB
/
memory.java
File metadata and controls
363 lines (338 loc) · 12.4 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package com.example.findme_technovation;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.os.Handler;
import android.graphics.Color;
import java.util.ArrayList;
public class memory extends GameActivity{
Button back;
Button b1;
Button b2;
Button b3;
Button b4;
Button b5;
Button b6;
String[] colors;
int[] buttonColors;
String[] buttonNum;
int previousColor;
int[] randNums;
String[] pairs;
int prevButtonNum;
int turn;
int count; //count of button presses
int lastPressed; // track the last index
boolean[] buttonPressed;
public memory(){
count = 0;
colors = new String[]{"Blue", "Green", "Pink", "Purple", "Yellow", "Orange", "Maroon"};
buttonNum = new String[]{"1","2", "3", "4", "5", "6"};
randNums = new int[3];
pairs = new String[3];
lastPressed = -1;
buttonPressed = new boolean[6];
buttonColors = new int[6];
previousColor = 0;
turn = 1;
}
@SuppressLint("MissingInflatedId")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memory);
back = (Button) findViewById(R.id.backButtonM);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
back.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
startActivity(new Intent(memory.this, GameActivity.class));
}
});
setColors();
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b1.setBackgroundColor(buttonColors[0]);
if(turn == 1) {
System.out.println("ONEDISABLE");
b1.setEnabled(false);
previousColor = buttonColors[0];
prevButtonNum = 1;
turn++;
}
else if(turn == 2) {
if(buttonColors[0] == previousColor) {
System.out.println("ONEDISABLE");
b1.setEnabled(false);
}
else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int defaultColor = Color.rgb(255, 255, 255);
b1.setBackgroundColor(defaultColor);
}
}, 200);
System.out.println("REENABLE");
resetPrevButton();
}
turn--;
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b2.setBackgroundColor(buttonColors[1]);
if(turn == 1) {
System.out.println("TWO DISABLE");
b2.setEnabled(false);
previousColor = buttonColors[1];
prevButtonNum = 2;
turn++;
}
else if(turn == 2) {
if(buttonColors[1] == previousColor) {
System.out.println("TWO DISABLE");
b2.setEnabled(false);
}
else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int defaultColor = Color.rgb(255, 255, 255);
b2.setBackgroundColor(defaultColor);
}
}, 200);
System.out.println("REENABLE");
resetPrevButton();
}
turn--;
}
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("ENTERED3");
System.out.print(buttonColors[2]);
b3.setBackgroundColor(buttonColors[2]);
if(turn == 1) {
previousColor = buttonColors[2];
prevButtonNum = 3;
turn++;
System.out.println("THREEE DISABLE");
b3.setEnabled(false);
}
else if(turn == 2) {
if(buttonColors[2] == previousColor) {
System.out.println("THREE DISABLE");
b3.setEnabled(false);
}
else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int defaultColor = Color.rgb(255, 255, 255);
b3.setBackgroundColor(defaultColor);
}
}, 200);
resetPrevButton();
}
turn--;
}
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.print(buttonColors[3]);
b4.setBackgroundColor(buttonColors[3]);
if(turn == 1) {
System.out.println("FOUR DISABLE");
b4.setEnabled(false);
previousColor = buttonColors[3];
prevButtonNum = 4;
turn++;
}
else if(turn == 2) {
if(buttonColors[3] == previousColor) {
System.out.println("FOUR DISABLE");
b4.setEnabled(false);
}
else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int defaultColor = Color.rgb(255, 255, 255);
b4.setBackgroundColor(defaultColor);
}
}, 200);
resetPrevButton();
}
turn--;
}
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b5.setBackgroundColor(buttonColors[4]);
if(turn == 1) {
System.out.println("FIVE DISABLE");
b5.setEnabled(false);
previousColor = buttonColors[4];
prevButtonNum = 5;
turn++;
}
else if(turn == 2) {
if(buttonColors[4] == previousColor) {
System.out.println("FIVE DISABLE");
b5.setEnabled(false);
}
else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int defaultColor = Color.rgb(255, 255, 255);
b5.setBackgroundColor(defaultColor);
}
}, 200);
resetPrevButton();
}
turn--;
}
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b6.setBackgroundColor(buttonColors[5]);
if(turn == 1) {
System.out.println("TWO DISABLE");
b6.setEnabled(false);
previousColor = buttonColors[5];
prevButtonNum = 6;
turn++;
}
else if(turn == 2) {
if(buttonColors[5] == previousColor) {
System.out.println("SIX DISABLE");
b6.setEnabled(false);
}
else {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
int defaultColor = Color.rgb(255, 255, 255);
b6.setBackgroundColor(defaultColor);
}
}, 200);
resetPrevButton();
}
turn--;
}
}
});
}
public void setColors() {
//3 random numbers 0 - 6;
ArrayList<Integer> colorRands = new ArrayList<Integer>();
int colorRand = 0;
while(colorRands.size() < 3) {
colorRand = (int)(Math.random() * 7);
if(colorRands.indexOf(colorRand) == -1) {
colorRands.add(colorRand);
}
}
//randomizes position of each color
ArrayList<Integer> buttonNumbers= new ArrayList<Integer>();
buttonNumbers.add(0);
buttonNumbers.add(1);
buttonNumbers.add(2);
buttonNumbers.add(3);
buttonNumbers.add(4);
buttonNumbers.add(5);
int colorRandIndex = 0;
for(int i = 0; i < 6; i++) {
int buttonIndex = (int)(Math.random() * buttonNumbers.size());
//sets array of corresponding colors
buttonColors[buttonNumbers.get(buttonIndex)] = getColorForIndex(colorRandIndex);
if(i%2 == 1) {
colorRandIndex++;
}
buttonNumbers.remove(buttonIndex);
}
}
public void resetPrevButton() {
if(prevButtonNum == 1) {
System.out.println("ONE ENABLE");
b1.setEnabled(true);
int defaultColor = Color.rgb(255, 255, 255);
b1.setBackgroundColor(defaultColor);
}
else if(prevButtonNum == 2) {
System.out.println("TWO ENABLE");
b2.setEnabled(true);
int defaultColor = Color.rgb(255, 255, 255);
b2.setBackgroundColor(defaultColor);
}
else if(prevButtonNum == 3) {
System.out.println("THREE ENABLE");
b3.setEnabled(true);
int defaultColor = Color.rgb(255, 255, 255);
b3.setBackgroundColor(defaultColor);
}
else if(prevButtonNum == 4) {
System.out.println("FOUR ENABLE");
b4.setEnabled(true);
int defaultColor = Color.rgb(255, 255, 255);
b4.setBackgroundColor(defaultColor);
}
else if(prevButtonNum == 5) {
System.out.println("FIVE ENABLE");
b5.setEnabled(true);
int defaultColor = Color.rgb(255, 255, 255);
b5.setBackgroundColor(defaultColor);
}
else if(prevButtonNum == 6) {
System.out.println("SIX ENABLE");
b6.setEnabled(true);
int defaultColor = Color.rgb(255, 255, 255);
b6.setBackgroundColor(defaultColor);
}
}
public int getColorForIndex(int index) {
if (index == 0) {
return Color.rgb(84, 179, 247) ;// blue
}
else if (index == 1) {
return Color.rgb(126, 207, 139); // green
}
else if (index == 2) {
return Color.rgb(255, 173, 245); // Pink
}
else if (index == 3) {
return Color.rgb(157, 150, 255); // purple
}
else if (index == 4) {
return Color.rgb(247, 233, 129); // yellow
}
else if (index == 5) {
return Color.rgb(255, 163, 112); // orange
}
else if(index == 6){
return Color.rgb(150, 75, 92); // maroon
}
else {
return Color.rgb(255,255,255); // white
}
}
}