-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain2Activity.java
More file actions
197 lines (157 loc) · 6.99 KB
/
Main2Activity.java
File metadata and controls
197 lines (157 loc) · 6.99 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
package com.example.build.blockpuzzle;
import android.media.Image;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.build.blockpuzzle.R;
import java.sql.Time;
import java.util.Arrays;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
public class Main2Activity extends AppCompatActivity {
//9 pieces of the puzzle
ImageView[] iw = new ImageView[9];
private final String IMAGE_VIEW_NAME_PREFIX = "iw";
//this array contains 9 image IDs. It is fixed and it is the correct answer
//dummy values (-1) will be replaced with the correct IDs in create()
int correct_id_seq[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1};
//this array is the working array. Its element's values are similar to correct_id_seq[] except at diff locations
int rand_id_seq[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1};
//array to keep 2 indexes of 2 elements in the array rand_id_seq from 2 clicks
int two_indexes_to_swap_img[] = {-1, -1};
int num_of_clicks = 0; //need to have 2 clicks to swap images
ImageView two_imageView_to_swap_img[] = {null, null};
TextView textView;
Timer T;
int time_to_solve_puzzle = -1;
//make the array elements appear randomly
public void rand_arr_elements(int[] arr) {
Random random = new Random();
int temp_index;
int temp_obj;
for (int i = 0; i < arr.length - 1; i++) {
// a random number between i + 1 and arr.length - 1
temp_index = i + 1 + random.nextInt(arr.length - 1 - i);
// swap the element at i with the element at temp_index
temp_obj = arr[i];
arr[i] = arr[temp_index];
arr[temp_index] = temp_obj;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get ImageViews
for (int i = 0; i < 9; i++) {
iw[i] = (ImageView) findViewById(this.getResources().getIdentifier(IMAGE_VIEW_NAME_PREFIX + Integer.toString(i), "id", this.getPackageName()));
}
//set the values for the correct_id_seq array
for (int i = 0; i < 9; i++) {
correct_id_seq[i] = this.getResources().getIdentifier("brba" + Integer.toString(i),"drawable", this.getPackageName());
}
//set the resource images of ImageViews
for (int i = 0; i < 9; i++) {
iw[i].setImageResource(correct_id_seq[i]);
}
//display image in 10s
for (int i = 0; i < 9; i++) {
iw[i].setClickable(false);
}
textView = (TextView) findViewById(R.id.textView);
new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
textView.setText("Time: " + Long.toString(millisUntilFinished / 1000));
}
@Override
public void onFinish() {
for (int i = 0; i < 9; i++) {
iw[i].setClickable(true);
}
display_puzzle_with_time_tick();
}
}.start();
}
public void display_puzzle_with_time_tick() {
//for the array rand_id_seq, firstly, start with the correct sequence of ids
rand_id_seq = Arrays.copyOf(correct_id_seq, correct_id_seq.length);
//and then call the function rand_arr_elements to randomly swap elements
rand_arr_elements(rand_id_seq);
// based on the values of rand_id_seq, re-set the resource images of ImageViews
for (int i = 0; i < 9; i++) {
iw[i].setImageResource(rand_id_seq[i]);
}
//set time - ticking by seconds
time_to_solve_puzzle = -1;
T = new Timer();
T.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
time_to_solve_puzzle++;
textView.setText("TIME: " + time_to_solve_puzzle);
}
});
}
},1000, 1000);
} // display_puzzle_with_time_tick
public void on_click(View v) {
ImageView view = (ImageView) v;
Log.i("Click = ", view.toString());
if (view == iw[0]) {
two_indexes_to_swap_img[num_of_clicks] = 0;
two_imageView_to_swap_img[num_of_clicks] = iw[0];
} else if (view == iw[1]) {
two_indexes_to_swap_img[num_of_clicks] = 1;
two_imageView_to_swap_img[num_of_clicks] = iw[1];
} else if (view == iw[2]) {
two_indexes_to_swap_img[num_of_clicks] = 2;
two_imageView_to_swap_img[num_of_clicks] = iw[2];
} else if (view == iw[3]) {
two_indexes_to_swap_img[num_of_clicks] = 3;
two_imageView_to_swap_img[num_of_clicks] = iw[3];
} else if (view == iw[4]) {
two_indexes_to_swap_img[num_of_clicks] = 4;
two_imageView_to_swap_img[num_of_clicks] = iw[4];
} else if (view == iw[5]) {
two_indexes_to_swap_img[num_of_clicks] = 5;
two_imageView_to_swap_img[num_of_clicks] = iw[5];
} else if (view == iw[6]) {
two_indexes_to_swap_img[num_of_clicks] = 6;
two_imageView_to_swap_img[num_of_clicks] = iw[6];
} else if (view == iw[7]) {
two_indexes_to_swap_img[num_of_clicks] = 7;
two_imageView_to_swap_img[num_of_clicks] = iw[7];
} else {
two_indexes_to_swap_img[num_of_clicks] = 8;
two_imageView_to_swap_img[num_of_clicks] = iw[8];
}
if (num_of_clicks ==1) {
//2 clicks already - swap images now
two_imageView_to_swap_img[0].setImageResource(rand_id_seq[two_indexes_to_swap_img[1]]);
two_imageView_to_swap_img[1].setImageResource(rand_id_seq[two_indexes_to_swap_img[0]]);
//update the rand_id_seq array
int temp = rand_id_seq[two_indexes_to_swap_img[0]];
rand_id_seq[two_indexes_to_swap_img[0]] = rand_id_seq[two_indexes_to_swap_img[1]];
rand_id_seq[two_indexes_to_swap_img[1]] = temp;
//check if the 2 array rand_id_seq and correct_id_seq are equal
//if it is then the user wins
if (Arrays.equals(correct_id_seq, rand_id_seq)) {
Toast.makeText(Main2Activity.this,
"You won!", Toast.LENGTH_LONG).show();
}
}
num_of_clicks++;
if (num_of_clicks == 2)
num_of_clicks = 0;
}//end of onClick
}