import java.awt.Button\x0d\x0aimport java.util.Set\x0d\x0a// 每一个
小方块类\x0d\x0apublic class Diamond extends Button {\x0d\x0aprivate Diamond[] diamonds\x0d\x0a\x0d\x0a// 该小方块周围的八个方向上的小方块\x0d\x0aprivate Diamond east\x0d\x0aprivate Diamond north\x0d\x0aprivate Diamond northEast\x0d\x0aprivate Diamond northWest\x0d\x0aprivate Diamond south\x0d\x0aprivate Diamond southEast\x0d\x0aprivate Diamond southWest\x0d\x0aprivate Diamond west\x0d\x0a\x0d\x0aprivate boolean isBomb// 是否是雷\x0d\x0aprivate boolean isChange// 又没有被翻过\x0d\x0aprivate int no// 产生的方块的编号\x0d\x0a\x0d\x0a// 持有所有小方块的引用,方便进行 *** 作\x0d\x0apublic Diamond(Diamond[] diamonds) {\x0d\x0athis.diamonds = diamonds\x0d\x0a}\x0d\x0a\x0d\x0a// 按键
时方块发生改变\x0d\x0apublic boolean change() {\x0d\x0athis.isChange = true// 说明已经翻过了\x0d\x0aif(isBomb) {// 触雷\x0d\x0a//this.setBackground(Color.red)\x0d\x0areturn true\x0d\x0a} else {// 不是雷,就显示周围雷的数目\x0d\x0a//this.setLabel(this.getNearBombNo() + "")\x0d\x0athis.setLabel(this.getNearBombNo() + "")\x0d\x0a//if(this.getNearBombNo() == 0) {\x0d\x0a//this.moveon()\x0d\x0a//}\x0d\x0areturn false\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0a// 获得该小方块周围雷的数量\x0d\x0apublic int getNearBombNo() {\x0d\x0aint no = 0\x0d\x0aif(this.northWest != null &&this.northWest.isBomb) no++\x0d\x0aif(this.north != null &&this.north.isBomb) no++\x0d\x0aif(this.northEast != null &&this.northEast.isBomb) no++\x0d\x0aif(this.east != null &&this.east.isBomb) no++\x0d\x0aif(this.southEast != null &&this.southEast.isBomb) no++\x0d\x0aif(this.south != null &&this.south.isBomb) no++\x0d\x0aif(this.southWest != null &&this.southWest.isBomb) no++\x0d\x0aif(this.west != null &&this.west.isBomb) no++\x0d\x0a\x0d\x0areturn no\x0d\x0a}\x0d\x0a\x0d\x0a// 获得该小方块周围的小方块\x0d\x0apublic Diamond getNearDimaond(int i) {\x0d\x0aint index = -1\x0d\x0aswitch (i) {\x0d\x0acase 1:// 1表示西北,2,表示北,以此类推\x0d\x0aindex = no - 10\x0d\x0aif(index return null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0acase 2:\x0d\x0aindex = no - 9\x0d\x0aif(index return null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0acase 3:\x0d\x0aindex = no - 8\x0d\x0aif(index return null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0acase 4:\x0d\x0aindex = no + 1\x0d\x0aif(no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {\x0d\x0areturn null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0acase 5:\x0d\x0aindex = no + 10\x0d\x0aif(index >= 81 ||no == 9 || no == 18 || no == 27 || no == 36 || no == 45 || no == 54 || no == 63 || no == 72 || no == 81) {\x0d\x0areturn null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0acase 6:\x0d\x0aindex = no + 9\x0d\x0aif(index >81) {\x0d\x0areturn null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0acase 7:\x0d\x0aindex = no + 8\x0d\x0aif(index >= 81 || no==1 || no == 10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {\x0d\x0areturn null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0acase 8:\x0d\x0aindex = no - 1\x0d\x0aif(no==1 || no==10 || no == 19 || no == 28 || no == 37 || no == 46 || no == 55 || no == 64 || no == 73) {\x0d\x0areturn null\x0d\x0a} else {\x0d\x0areturn diamonds[index]\x0d\x0a}\x0d\x0a}\x0d\x0areturn null\x0d\x0a}\x0d\x0a\x0d\x0a//
递归,set是用来装已经翻过的小方块的,不然会死循环,为什么用set,因为set是不重复的\x0d\x0apublic void moveon(Set set) {\x0d\x0a\x0d\x0aset.add(this)// 先把自己加上\x0d\x0aif(this.getNorthWest() != null &&this.getNorthWest().isBomb == false) {\x0d\x0athis.getNorthWest().change()\x0d\x0a\x0d\x0aif(this.getNorthWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorthWest()) == false)\x0d\x0athis.getNorthWest().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorthWest())\x0d\x0a}\x0d\x0a\x0d\x0aif(this.getNorth() != null &&this.getNorth().isBomb == false) {\x0d\x0athis.getNorth().change()\x0d\x0aif(this.getNorth().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorth()) == false)\x0d\x0athis.getNorth().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorth())\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getNorthEast() != null &&this.getNorthEast().isBomb == false) {\x0d\x0athis.getNorthEast().change()\x0d\x0aif(this.getNorthEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getNorthEast()) == false)\x0d\x0athis.getNorthEast().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getNorthEast())\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getEast() != null &&this.getEast().isBomb == false) {\x0d\x0athis.getEast().change()\x0d\x0aif(this.getEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getEast()) == false)\x0d\x0athis.getEast().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getEast())\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouthEast() != null &&this.getSouthEast().isBomb == false) {\x0d\x0athis.getSouthEast().change()\x0d\x0aif(this.getSouthEast().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouthEast()) == false)\x0d\x0athis.getSouthEast().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouthEast())\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouth() != null &&this.getSouth().isBomb == false) {\x0d\x0athis.getSouth().change()\x0d\x0aif(this.getSouth().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouth()) == false)\x0d\x0athis.getSouth().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouth())\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getSouthWest() != null &&this.getSouthWest().isBomb == false) {\x0d\x0athis.getSouthWest().change()\x0d\x0aif(this.getSouthWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getSouthWest()) == false)\x0d\x0athis.getSouthWest().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getSouthWest())\x0d\x0a} \x0d\x0a\x0d\x0aif(this.getWest() != null &&this.getWest().isBomb == false) {\x0d\x0athis.getWest().change()\x0d\x0aif(this.getWest().getNearBombNo() == 0) {\x0d\x0aif(set.contains(this.getWest()) == false)\x0d\x0athis.getWest().moveon(set)\x0d\x0a}\x0d\x0a\x0d\x0aset.add(this.getWest())\x0d\x0a} \x0d\x0a}\x0d\x0a\x0d\x0a/*public Diamond[] getDiamonds() {\x0d\x0areturn diamonds\x0d\x0a}*/\x0d\x0a\x0d\x0apublic Diamond getEast() {\x0d\x0areturn east\x0d\x0a}\x0d\x0a\x0d\x0apublic int getNo() {\x0d\x0areturn no\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorth() {\x0d\x0areturn north\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorthEast() {\x0d\x0areturn northEast\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getNorthWest() {\x0d\x0areturn northWest\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouth() {\x0d\x0areturn south\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouthEast() {\x0d\x0areturn southEast\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getSouthWest() {\x0d\x0areturn southWest\x0d\x0a}\x0d\x0a\x0d\x0apublic Diamond getWest() {\x0d\x0areturn west\x0d\x0a}\x0d\x0a\x0d\x0apublic boolean isBomb() {\x0d\x0areturn isBomb\x0d\x0a}\x0d\x0a\x0d\x0apublic boolean isChange() {\x0d\x0areturn isChange\x0d\x0a}\x0d\x0a\x0d\x0apublic void setBomb(boolean isBomb) {\x0d\x0athis.isBomb = isBomb\x0d\x0a}\x0d\x0a\x0d\x0apublic void setChange(boolean isChange) {\x0d\x0athis.isChange = isChange\x0d\x0a}\x0d\x0a\x0d\x0apublic void setDiamonds(Diamond[] diamonds) {\x0d\x0athis.diamonds = diamonds\x0d\x0a}\x0d\x0a\x0d\x0apublic void setEast(Diamond east) {\x0d\x0athis.east = east\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNo(int no) {\x0d\x0athis.no = no\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorth(Diamond north) {\x0d\x0athis.north = north\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorthEast(Diamond northEast) {\x0d\x0athis.northEast = northEast\x0d\x0a}\x0d\x0a\x0d\x0apublic void setNorthWest(Diamond northWest) {\x0d\x0athis.northWest = northWest\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouth(Diamond south) {\x0d\x0athis.south = south\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouthEast(Diamond southEast) {\x0d\x0athis.southEast = southEast\x0d\x0a}\x0d\x0a\x0d\x0apublic void setSouthWest(Diamond southWest) {\x0d\x0athis.southWest = southWest\x0d\x0a}\x0d\x0a\x0d\x0apublic void setWest(Diamond west) {\x0d\x0athis.west = west\x0d\x0a}\x0d\x0a\x0d\x0a}首先要写一个UI,也就是 *** 作界面,使用java.swing.*内的东西就可以搞定;
其次写一个hander,也就是具体的按钮响应,UI的初始化(哪里有雷),怎么触发雷和其他的;
一般来说简单的扫雷模型就好了,如果需要更有意思点,可以写一些数据库的 *** 作内容的tool类具体的就是处理历史 *** 作记录,场均数据或多人竞技的特点。
如果你是说你没有设计思路,我可以给你个提示:递归算法是触发扫雷的方法,初始化用随机数来做。
评论列表(0条)