-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
97 lines (71 loc) · 1.87 KB
/
Map.java
File metadata and controls
97 lines (71 loc) · 1.87 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
/*
* ============================================================================================
* Map.java : Construct map.
*
* ============================================================================================
*/
import java.util.Arrays;
import java.util.ArrayList;
public class Map {
private int[][] map;
private int count;
private int z;
public Map(int count,int z){//count how many image inside the game,nÐÐnÁÐ
map = MapCreation.getMap(z);//gain array of z rows z coloumns
this.count = count;
this.z = z;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getN() {
return z;
}
public void setN(int k) {
this.z = k;
}
public int[][] getMap(){
ArrayList<Integer> maplist = new ArrayList<Integer>();
for(int i=0;i<z*z/10;i++){
for(int j=0;j<count;j++){
maplist.add(j);
}
}
for(int i=0;i<z;i++){
for(int j=0;j<z;j++){
int index = (int) (Math.random()*maplist.size());//get one image id from list randomly,add that image into array and delete it from list.
map[i][j] = maplist.get(index);
maplist.remove(index);
}
}
return map;
}
public int[][] getResetMap(){
ArrayList<Integer> maplist = new ArrayList<Integer>();
for(int i=0;i<z;i++){
for(int j=0;j<z;j++){
if(map[i][j]!=-1)
maplist.add(map[i][j]);
map[i][j]=-1;
}
}
//mess up left image
while(!maplist.isEmpty()){
int index = (int) (Math.random()*maplist.size());
boolean state = false;
while(!state){
int i = (int) (Math.random()*z);
int j = (int) (Math.random()*z);
if(map[i][j]==-1){//if there is no image in that position
map[i][j] = maplist.get(index);
maplist.remove(index);
state = true;
}
}
}
return map;
}
}