Chinaunix
标题:
java移动拼图游戏模拟
[打印本页]
作者:
so_brave
时间:
2011-12-12 23:12
标题:
java移动拼图游戏模拟
java移动拼图游戏模拟
自己写的一个移动拼图的雏形,自己可以在此基础上发展
Java代码
1.public class MovePuzzle {
2.
3. /**
4. *
5. */
6. static int rank[][];
7.
8. /**
9. * 0数组的索引
10. */
11. public static int ZERO_INDEX_Y;
12. public static int ZERO_INDEX_X;
13.
14. /**
15. *
16. */
17. private static int x = 3;
18. private static int y = 3;
19.
20. /**
21. *
22. * @param rak
23. */
24. public MovePuzzle(int x, int y) {
25. this.rank = new int[x][y];
26. this.x = x;
27. this.y = y;
28.
29. }
30.
31. /**
32. * 初始化this.rank
33. *
34. * @return 返回一个初始化的二维数组
35. */
36. public int[][] initRank() {
37. return this.initRank(initRank(this.rank));
38. }
39.
40. /**
41. * 初始化参数1
42. *
43. * @param rak
44. * @return 将参数1的二维数组初始化成一个 从0开始的一个二维数组 0 1 2 3 4 5 6 7 8
45. */
46. private int[][] initRank(int[][] rak) {
47. for (int i = 0; i < rak.length; i++) {
48. for (int j = 0; j < rak[i].length; j++) {
49. rak[i][j] = i * rak.length + j;
50. if (rak[i][j] == 0) {
51. this.ZERO_INDEX_X = i;
52. this.ZERO_INDEX_Y = j;
53. }
54. }
55. }
56. return rak;
57. }
58.
59. /**
60. * 打印数组
61. */
62. public void printArr() {
63. this.print2DArr(this.rank);
64. }
65.
66. /**
67. * 打印出所有的二维数组
68. */
69. public void print2DArr(int[][] twoDArr) {
70. for (int i = 0; i < twoDArr.length; i++) {
71. for (int j = 0; j < twoDArr[i].length; j++) {
72. System.out.print(twoDArr[i][j] + "\t");
73. }
74. System.out.println();
75. }
76. }
77.
78. /**
79. * 得到0值得x索引和y索引
80. *
81. * @param twoDArr
82. */
83. public void getZero_index(int[][] twoDArr) {
84. for (int i = 0; i < twoDArr.length; i++) {
85. for (int j = 0; j < twoDArr[i].length; j++) {
86. if (twoDArr[i][j] == 0) {
87. this.ZERO_INDEX_X = i;
88. this.ZERO_INDEX_Y = j;
89. }
90. }
91. }
92. }
93.
94. /**
95. * 调换this.rank[oldX][oldY] 和 this.rank[newX][newY]的值
96. *
97. * @param oldX
98. * @param oldY
99. * @param newX
100. * @param newY
101. * @return 是否调换成功
102. */
103. public boolean exchangeBlock(int oldX, int oldY, int newX, int newY) {
104. int tmp = 1;
105. tmp = this.rank[oldX][oldY];
106. this.rank[oldX][oldY] = this.rank[newX][newY];
107. this.rank[newX][newY] = tmp;
108. if (tmp == 0) {
109. this.ZERO_INDEX_X = newX;
110. this.ZERO_INDEX_Y = newY;
111. }
112. return true;
113. }
114.
115. /**
116. * 检查是否可以移动
117. *
118. * @param oldX
119. * @param oldY
120. * @param newX
121. * @param newY
122. * @return
123. */
124. public boolean checkCanMove(int oldX, int oldY, int newX, int newY) {
125. boolean result = true;
126. if (this.rank[oldX][oldY] != 0 && this.rank[newX][newY] != 0) {
127. result = false;
128. } else if ((oldX != newX) && (oldY != newY)) {
129. result = false;
130. }
131. return result;
132. }
133.
134. /**
135. * 移动数字index
136. *
137. * @param index
138. */
139. public void moveBlock(int index) {
140. int index_x = 0;
141. int index_y = 0;
142. /**
143. * 找出要移动的数字的位置
144. */
145. for (int i = 0; i < this.rank.length; i++) {
146. for (int j = 0; j < this.rank[i].length; j++) {
147. if (this.rank[i][j] == index) {
148. index_x = i;
149. index_y = j;
150. }
151. }
152. }
153. if (checkCanMove(this.ZERO_INDEX_X, this.ZERO_INDEX_Y, index_x, index_y)) {
154. exchangeBlock(this.ZERO_INDEX_X, this.ZERO_INDEX_Y, index_x,
155. index_y);
156. }
157. }
158.}
复制代码
下面是test类:
Java代码
1.public class Test {
2.
3. /**
4. * @param args
5. */
6. public static void main(String[] args) {
7. MovePuzzle movePuzzle = new MovePuzzle(3, 3);
8. movePuzzle.initRank();
9. movePuzzle.printArr();
10. move(movePuzzle,1);
11. move(movePuzzle,2);
12. move(movePuzzle,5);
13. move(movePuzzle,4);
14. move(movePuzzle,3);
15. move(movePuzzle,1);
16. move(movePuzzle,2);
17. move(movePuzzle,3);
18. move(movePuzzle,4);
19. move(movePuzzle,8);
20. move(movePuzzle,7);
21. move(movePuzzle,6);
22. move(movePuzzle,1);
23. move(movePuzzle,2);
24. move(movePuzzle,3);
25. move(movePuzzle,5);
26. move(movePuzzle,8);
27. move(movePuzzle,7);
28. move(movePuzzle,6);
29. move(movePuzzle,4);
30. move(movePuzzle,5);
31. move(movePuzzle,8);
32. move(movePuzzle,7);
33. move(movePuzzle,6);
34. move(movePuzzle,4);
35. move(movePuzzle,1);
36. move(movePuzzle,2);
37. move(movePuzzle,3);
38. move(movePuzzle,8);
39. move(movePuzzle,5);
40. move(movePuzzle,6);
41. move(movePuzzle,4);
42. move(movePuzzle,1);
43. move(movePuzzle,2);
44. move(movePuzzle,3);
45. move(movePuzzle,6);
46. move(movePuzzle,5);
47. move(movePuzzle,7);
48. move(movePuzzle,4);
49. move(movePuzzle,1);
50. move(movePuzzle,2);
51. move(movePuzzle,3);
52. move(movePuzzle,6);
53. move(movePuzzle,8);
54. move(movePuzzle,7);
55. move(movePuzzle,4);
56. move(movePuzzle,1);
57. move(movePuzzle,2);
58. move(movePuzzle,3);
59. move(movePuzzle,6);
60. move(movePuzzle,8);
61. move(movePuzzle,7);
62. move(movePuzzle,4);
63. move(movePuzzle,1);
64. move(movePuzzle,2);
65. move(movePuzzle,3);
66. move(movePuzzle,6);
67. move(movePuzzle,8);
68. move(movePuzzle,7);
69. move(movePuzzle,4);
70. move(movePuzzle,1);
71. move(movePuzzle,2);
72. move(movePuzzle,3);
73. move(movePuzzle,6);
74. move(movePuzzle,8);
75. move(movePuzzle,7);
76. move(movePuzzle,7);
77. move(movePuzzle,8);
78. }
79.
80. public static void move(MovePuzzle movePuzzle,int i){
81. movePuzzle.moveBlock(i);
82. System.out.println("-----调换后"+i+"后------");
83. movePuzzle.printArr();
84. }
85.}
复制代码
作者:
遗矢的幸福
时间:
2011-12-20 16:20
学习鸟
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2