Map.java
Upload User: shjixinjx
Upload Date: 2020-12-29
Package Size: 985k
Code Size: 6k
Category:

Games

Development Platform:

Java

  1. package kyodai.map;
  2. import java.awt.Point;
  3. import java.util.Random;
  4. import java.util.Vector;
  5. /**
  6.  * 生成连连看方块的类
  7. */
  8. public class Map{
  9. private int level;
  10. private int map[][];
  11. int array[];
  12. private int restBlock;
  13. private Vector vector;
  14. AnimateDelete animate;
  15. private boolean test;
  16. public Map(){
  17. level = 28;
  18. map = new int[10][17];
  19. array = new int[170];
  20. restBlock = level * 4;
  21. vector = new Vector();
  22. test = false;
  23. initMap();
  24. }
  25. public Map(int level){
  26. this.level = 28;
  27. map = new int[10][17];
  28. array = new int[170];
  29. restBlock = this.level * 4;
  30. vector = new Vector();
  31. test = false;
  32. this.level = level;
  33. restBlock = level * 4;
  34. initMap();
  35. }
  36. public void setTest(boolean test){
  37. this.test = test;
  38. }
  39. public void setLevel(int level){
  40. this.level = level;
  41. restBlock = level * 4;
  42. initMap();
  43. }
  44. private void initMap(){
  45. for(int i = 0; i < level; i++){
  46. array[i * 4] = i + 1;
  47. array[i * 4 + 1] = i + 1;
  48. array[i * 4 + 2] = i + 1;
  49. array[i * 4 + 3] = i + 1;
  50. }
  51. random(array);
  52. for(int i = 0; i < 10; i++){
  53. for(int j = 0; j < 17; j++)
  54. map[i][j] = array[i * 17 + j];
  55. }
  56. }
  57. private void random(int array[]){
  58. Random random = new Random();
  59. for(int i = array.length; i > 0; i--){
  60. int j = random.nextInt(i);
  61. int temp = array[j];
  62. array[j] = array[i - 1];
  63. array[i - 1] = temp;
  64. }
  65. }
  66. public void earse(Point a, Point b){
  67. map[a.x][a.y] = 0;
  68. map[b.x][b.y] = 0;
  69. restBlock -= 2;
  70. }
  71. public int getCount(){
  72. return restBlock <= 0 ? 0 : restBlock;
  73. }
  74. public void refresh(){
  75. int count = getCount();
  76. if(count <= 0)
  77. return;
  78. int temp[] = new int[count];
  79. count = 0;
  80. for(int row = 0; row < 10; row++){
  81. for(int col = 0; col < 17; col++)
  82. if(map[row][col] > 0){
  83. temp[count] = map[row][col];
  84. count++;
  85. }
  86. }
  87. random(temp);
  88. count = 0;
  89. for(int row = 0; row < 10; row++){
  90. for(int col = 0; col < 17; col++)
  91. if(map[row][col] > 0){
  92. map[row][col] = temp[count];
  93. count++;
  94. }
  95. }
  96. }
  97. private boolean horizon(Point a, Point b, boolean recorder){
  98. if(a.x == b.x && a.y == b.y)
  99. return false;
  100. int x_start = a.y <= b.y ? a.y : b.y;
  101. int x_end = a.y <= b.y ? b.y : a.y;
  102. for(int x = x_start + 1; x < x_end; x++)
  103. if(map[a.x][x] != 0)
  104. return false;
  105. if(!test && recorder)
  106. animate = new AnimateDelete(1, a, b);
  107. return true;
  108. }
  109. private boolean vertical(Point a, Point b, boolean recorder){
  110. if(a.x == b.x && a.y == b.y)
  111. return false;
  112. int y_start = a.x <= b.x ? a.x : b.x;
  113. int y_end = a.x <= b.x ? b.x : a.x;
  114. for(int y = y_start + 1; y < y_end; y++)
  115. if(map[y][a.y] != 0)
  116. return false;
  117. if(!test && recorder)
  118. animate = new AnimateDelete(0, a, b);
  119. return true;
  120. }
  121. private boolean oneCorner(Point a, Point b){
  122. Point c = new Point(a.x, b.y);
  123. Point d = new Point(b.x, a.y);
  124. if(map[c.x][c.y] == 0){
  125. boolean method1 = horizon(a, c, false) && vertical(b, c, false);
  126. if(method1){
  127. if(!test)
  128. animate = new AnimateDelete(1, a, c, b);
  129. return method1;
  130. }
  131. }
  132. if(map[d.x][d.y] == 0){
  133. boolean method2 = vertical(a, d, false) && horizon(b, d, false);
  134. if(method2 && !test)
  135. animate = new AnimateDelete(0, a, d, b);
  136. return method2;
  137. else{
  138. return false;
  139. }
  140. }
  141. private Vector scan(Point a, Point b){
  142. Vector v = new Vector();
  143. Point c = new Point(a.x, b.y);
  144. Point d = new Point(b.x, a.y);
  145. for(int y = a.y; y >= 0; y--)
  146. if(map[a.x][y] == 0 && map[b.x][y] == 0 && vertical(new Point(a.x, y), new Point(b.x, y), false))
  147. v.add(new Line(0, new Point(a.x, y), new Point(b.x, y)));
  148. for(int y = a.y; y < 17; y++)
  149. if(map[a.x][y] == 0 && map[b.x][y] == 0 && vertical(new Point(a.x, y), new Point(b.x, y), false))
  150. v.add(new Line(0, new Point(a.x, y), new Point(b.x, y)));
  151. for(int x = a.x; x >= 0; x--)
  152. if(map[x][a.y] == 0 && map[x][b.y] == 0 && horizon(new Point(x, a.y), new Point(x, b.y), false))
  153. v.add(new Line(1, new Point(x, a.y), new Point(x, b.y)));
  154. for(int x = a.x; x < 10; x++)
  155. if(map[x][a.y] == 0 && map[x][b.y] == 0 && horizon(new Point(x, a.y), new Point(x, b.y), false))
  156. v.add(new Line(1, new Point(x, a.y), new Point(x, b.y)));
  157. return v;
  158. }
  159. private boolean twoCorner(Point a, Point b){
  160. vector = scan(a, b);
  161. if(vector.isEmpty())
  162. return false;
  163. for(int index = 0; index < vector.size(); index++){
  164. Line line = (Line)vector.elementAt(index);
  165. if(line.direct == 1){
  166. if(vertical(a, line.a, false) && vertical(b, line.b, false)){
  167. if(!test)
  168. animate = new AnimateDelete(0, a, line.a, line.b, b);
  169. return true;
  170. }
  171. else if(horizon(a, line.a, false) && horizon(b, line.b, false)){
  172. if(!test)
  173. animate = new AnimateDelete(1, a, line.a, line.b, b);
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. public boolean test(Point a, Point b){
  180. if(map[a.x][a.y] != map[b.x][b.y])
  181. return false;
  182. if(a.x == b.x && horizon(a, b, true))
  183. return true;
  184. if(a.y == b.y && vertical(a, b, true))
  185. return true;
  186. if(oneCorner(a, b))
  187. return true;
  188. else
  189. return twoCorner(a, b);
  190. }
  191. public Line findNext(Point a){
  192. Point b = new Point();
  193. a = findFirst(a);
  194. if(a.equals(new Point(-1, -1)))
  195. return new Line(0, a, b);
  196. for(; !a.equals(new Point(-1, -1)); a = findFirst(a))
  197. for(b = findSecond(a, b); !b.equals(new Point(-1, -1)); b = findSecond(a, b))
  198. if(test(a, b))
  199. return new Line(1, a, b);
  200. return new Line(0, a, b);
  201. }
  202. private Point findFirst(Point a){
  203. int offset = 0;
  204. if(a != null)
  205. offset = a.x * 17 + a.y;
  206. if(offset < 0)
  207. offset = -1;
  208. for(int x = offset + 1; x < 170; x++){
  209. int row = Math.round(x / 17);
  210. int col = x - row * 17;
  211. if(map[row][col] != 0)
  212. return new Point(row, col);
  213. }
  214. return new Point(-1, -1);
  215. }
  216. private Point findSecond(Point a, Point b){
  217. if(a == null)
  218. return new Point(-1, -1);
  219. if(a.x + a.y < 0)
  220. return new Point(-1, -1);
  221. if(b == null)
  222. b = new Point(0, 0);
  223. int offset = Math.max(a.x * 17 + a.y, b.x * 17 + b.y);
  224. for(int x = offset + 1; x < 170; x++){
  225. int row = Math.round(x / 17);
  226. int col = x - row * 17;
  227. if(map[row][col] == map[a.x][a.y])
  228. return new Point(row, col);
  229. }
  230. return new Point(-1, -1);
  231. }
  232. public int[][] getMap(){
  233. return map;
  234. }
  235. }