免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1884 | 回复: 1
打印 上一主题 下一主题

java移动拼图游戏模拟 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-12 23:12 |只看该作者 |倒序浏览
java移动拼图游戏模拟









自己写的一个移动拼图的雏形,自己可以在此基础上发展



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



Java代码
  1. 1.public class Test {  
  2. 2.  
  3. 3.    /**
  4. 4.     * @param args
  5. 5.     */  
  6. 6.    public static void main(String[] args) {  
  7. 7.        MovePuzzle movePuzzle = new MovePuzzle(3, 3);  
  8. 8.        movePuzzle.initRank();  
  9. 9.        movePuzzle.printArr();  
  10. 10.        move(movePuzzle,1);  
  11. 11.        move(movePuzzle,2);  
  12. 12.        move(movePuzzle,5);  
  13. 13.        move(movePuzzle,4);  
  14. 14.        move(movePuzzle,3);  
  15. 15.        move(movePuzzle,1);  
  16. 16.        move(movePuzzle,2);  
  17. 17.        move(movePuzzle,3);  
  18. 18.        move(movePuzzle,4);  
  19. 19.        move(movePuzzle,8);  
  20. 20.        move(movePuzzle,7);  
  21. 21.        move(movePuzzle,6);  
  22. 22.        move(movePuzzle,1);  
  23. 23.        move(movePuzzle,2);  
  24. 24.        move(movePuzzle,3);  
  25. 25.        move(movePuzzle,5);  
  26. 26.        move(movePuzzle,8);  
  27. 27.        move(movePuzzle,7);  
  28. 28.        move(movePuzzle,6);  
  29. 29.        move(movePuzzle,4);  
  30. 30.        move(movePuzzle,5);  
  31. 31.        move(movePuzzle,8);  
  32. 32.        move(movePuzzle,7);  
  33. 33.        move(movePuzzle,6);  
  34. 34.        move(movePuzzle,4);  
  35. 35.        move(movePuzzle,1);  
  36. 36.        move(movePuzzle,2);  
  37. 37.        move(movePuzzle,3);  
  38. 38.        move(movePuzzle,8);  
  39. 39.        move(movePuzzle,5);  
  40. 40.        move(movePuzzle,6);  
  41. 41.        move(movePuzzle,4);  
  42. 42.        move(movePuzzle,1);  
  43. 43.        move(movePuzzle,2);  
  44. 44.        move(movePuzzle,3);  
  45. 45.        move(movePuzzle,6);  
  46. 46.        move(movePuzzle,5);  
  47. 47.        move(movePuzzle,7);  
  48. 48.        move(movePuzzle,4);  
  49. 49.        move(movePuzzle,1);  
  50. 50.        move(movePuzzle,2);  
  51. 51.        move(movePuzzle,3);  
  52. 52.        move(movePuzzle,6);  
  53. 53.        move(movePuzzle,8);  
  54. 54.        move(movePuzzle,7);  
  55. 55.        move(movePuzzle,4);  
  56. 56.        move(movePuzzle,1);  
  57. 57.        move(movePuzzle,2);  
  58. 58.        move(movePuzzle,3);  
  59. 59.        move(movePuzzle,6);  
  60. 60.        move(movePuzzle,8);  
  61. 61.        move(movePuzzle,7);  
  62. 62.        move(movePuzzle,4);  
  63. 63.        move(movePuzzle,1);  
  64. 64.        move(movePuzzle,2);  
  65. 65.        move(movePuzzle,3);  
  66. 66.        move(movePuzzle,6);  
  67. 67.        move(movePuzzle,8);  
  68. 68.        move(movePuzzle,7);  
  69. 69.        move(movePuzzle,4);  
  70. 70.        move(movePuzzle,1);  
  71. 71.        move(movePuzzle,2);  
  72. 72.        move(movePuzzle,3);  
  73. 73.        move(movePuzzle,6);  
  74. 74.        move(movePuzzle,8);  
  75. 75.        move(movePuzzle,7);  
  76. 76.        move(movePuzzle,7);  
  77. 77.        move(movePuzzle,8);  
  78. 78.    }  
  79. 79.  
  80. 80.    public static void move(MovePuzzle movePuzzle,int i){  
  81. 81.        movePuzzle.moveBlock(i);  
  82. 82.        System.out.println("-----调换后"+i+"后------");  
  83. 83.        movePuzzle.printArr();  
  84. 84.    }  
  85. 85.}  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-20 16:20 |只看该作者
学习鸟
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP