"
代码<a rel=""nofollow"" href=""http://pan.baidu.com/s/1pJAtcy3"">http://pan.baidu.com/s/1pJAtcy3
package minewiper;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;
import ui.Panel;
public class gridClicked extends MouseAdapter{
private MineWiper mineWiper;
private int x;
private int y;
private Panel panel;
public gridClicked(int x,int y,MineWiper mineWiper,Panel panel) {
this.mineWiper = mineWiper;
this.x=x;
this.y=y;
this.panel = panel;
}
@Override
public void mousePressed(MouseEvent e) {
//如果左键按下
if (e.getButton() == MouseEvent.BUTTON1) {
mineWiper.dfs(this.x, this.y);
boolean[][] visited = mineWiper.getIsVisited();
visited[this.x][this.y] = true;
mineWiper.setIsVisited(visited);
if (mineWiper.getMap()[this.x][this.y] == 9) {
this.gameOver();
}
}
//如果右键按下
if(e.getButton() == MouseEvent.BUTTON3) {
if(!mineWiper.getIsVisited()[this.x][this.y]) {
boolean[][] flag = mineWiper.getIsFlag();
flag[this.x][this.y] = !flag[this.x][this.y];
mineWiper.setIsFlag(flag);
}
}
this.panel.repaint();
}
/*
* 游戏结束
*/
private void gameOver() {
boolean[][] visited = mineWiper.getIsVisited();
for(int x=0;x<mineWiper.getMapSizeX();x++)
for(y=0;y<mineWiper.getMapSizeY();y++)
{
if(mineWiper.getMap()[x][y]==9) {
visited[x][y] = true;
mineWiper.setIsVisited(visited);
}
}
this.panel.repaint();
if(JOptionPane.showConfirmDialog(panel,"游戏结束,还想继续吗", "Game Over", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION)
{
mineWiper.initMap();
mineWiper.initMines();
}
else
System.exit(0);
}
}
package minewiper;
import java.util.Random;
public class MineWiper {
private int mapSizeX = 10;
private int mapSizeY = 10;
/*
* 表示对应方格是否翻转
/
private boolean[][] isVisited;
/
* map[x][y==0~8表示该方格不是雷,对应数字表示方格周围的雷数,9表示该方格是个雷
*/
private int[][] map;
//是否放置旗帜
private boolean[][] isFlag;
private int mineNumber;
public MineWiper() {
this.isVisited = new boolean[this.mapSizeX][this.mapSizeY];
this.map = new int[this.mapSizeX][this.mapSizeY];
this.isFlag = new boolean[this.mapSizeX][this.mapSizeY];
this.mineNumber = this.mapSizeX * this.mapSizeY / 10;
this.initMap();
this.initMines();
}
//深度优先搜索相邻数值为0的格子
public void dfs(int x,int y) {
if(isOverBound(x, y)) return;
if(isVisited[x][y] || map[x][y]!=0 || isFlag[x][y]) return;
isVisited[x][y] = true;
dfs(x-1, y-1); dfs(x-1, y); dfs(x-1, y+1);
dfs(x, y-1); dfs(x, y+1);
dfs(x+1, y+1); dfs(x+1, y); dfs(x+1, y-1);
}
//判断是否越界
private boolean isOverBound(int x,int y) {
if(x=this.mapSizeX || y>=this.mapSizeY) return true;
else return false;
}
//初始化数组
public void initMap() {
for(int x=0;x<this.mapSizeX;x++)
for(int y=0;y<this.mapSizeY;y++)
{
this.isVisited[x][y] = false;
this.map[x][y] = 0;
this.isFlag[x][y] = false;
}
}
//初始化地雷(随机生成地雷)
public void initMines() {
Random random = new Random();
int mx;
int my;
for (int i = 0; i < this.mineNumber; i++) {
mx = random.nextInt(mapSizeX-1);
my = random.nextInt(mapSizeY-1);
while(map[mx][my]!=0) {
mx = random.nextInt(mapSizeX);
my = random.nextInt(mapSizeY);
}
map[mx][my]=9;
}
for(int x=0;x<this.mapSizeX;x++)
for(int y=0;y<this.mapSizeY;y++)
{
if(map[x][y]==0) {
if(!isOverBound(x-1, y-1)) if(map[x-1][y-1]==9) map[x][y]++;
if(!isOverBound(x-1, y)) if(map[x-1][y]==9) map[x][y]++;
if(!isOverBound(x-1, y+1)) if(map[x-1][y+1]==9) map[x][y]++;
if(!isOverBound(x, y-1)) if(map[x][y-1]==9) map[x][y]++;
if(!isOverBound(x, y+1)) if(map[x][y+1]==9) map[x][y]++;
if(!isOverBound(x+1, y+1)) if(map[x+1][y+1]==9) map[x][y]++;
if(!isOverBound(x+1, y)) if(map[x+1][y]==9) map[x][y]++;
if(!isOverBound(x+1, y-1)) if(map[x+1][y-1]==9) map[x][y]++;
}
}
}
public int getMapSizeX() {
return mapSizeX;
}
public void setMapSizeX(int mapSizeX) {
this.mapSizeX = mapSizeX;
}
public int getMapSizeY() {
return mapSizeY;
}
public void setMapSizeY(int mapSizeY) {
this.mapSizeY = mapSizeY;
}
public boolean[][] getIsVisited() {
return isVisited;
}
public void setIsVisited(boolean[][] isVisited) {
this.isVisited = isVisited;
}
public int[][] getMap() {
return map;
}
public void setMap(int[][] map) {
this.map = map;
}
public boolean[][] getIsFlag() {
return isFlag;
}
public void setIsFlag(boolean[][] isFlag) {
this.isFlag = isFlag;
}
}
错误提示
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at minewiper.gridClicked.mousePressed(gridClicked.java:29)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:280)
at java.awt.Component.processMouseEvent(Component.java:6502)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:688)
at java.awt.EventQueue$3.run(EventQueue.java:686)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:702)
at java.awt.EventQueue$4.run(EventQueue.java:700)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
"
因为int stck[] = new int[4];
这句已经设置了栈的最大容量了,
所以
void push (int item) { if (this.tos == 9) { System.out.println("stack is full"); } else { this.stck[++this.tos] = item; } }
方法是有问题的。
" ![image.png](https://ucc.alicdn.com/pic/developer-ecology/45cfe32f018947a29f39046f477bd49a.png)
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。