扫雷游戏异常错误

发布于 2025-01-04 17:20:01 字数 2605 浏览 0 评论 0原文

您好,我目前正在尝试创建一个扫雷游戏,我有我的图形用户界面并使用二维数组来存储某个位置是否有地雷,但是当我尝试使用此方法单击地雷来结束游戏时代码:

if (board[row][col] == 1) {
    return GameStatus.Lost; }  
else {
    return GameStatus.Continue;
 }

我收到错误为

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at Game.getGameStatus(Game.java:55)
at MineSweeperPanel$ButtonListener.actionPerformed(MineSweeperPanel.java:71)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Hello I am currently attempting to create a minesweeper game, i have my gui and am using a 2d array in order to store whether or not a location has a mine, however when i attempt to have the game end for clicking on a mine using this code:

if (board[row][col] == 1) {
    return GameStatus.Lost; }  
else {
    return GameStatus.Continue;
 }

I get error as

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at Game.getGameStatus(Game.java:55)
at MineSweeperPanel$ButtonListener.actionPerformed(MineSweeperPanel.java:71)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城泪 2025-01-11 17:20:01

检查您在 rowcol 中调用的边界。例如,如果您有 25 行和 25 列,并且您引用的是 board[25][25],则这超出了数组的边界。尽管行数的总大小为 25,但在数组中索引将从 025-1

Check the bounds that you call in row and col. If, for example, you have 25 rows and columns and you're referring to board[25][25], this is past the bounds of the array. Although the overall size of the number of rows is 25, in the array the indices will go from 0 to 25-1.

醉梦枕江山 2025-01-11 17:20:01

数组索引越界意味着您的数组中有(比如说)10 个元素,但您尝试访问(比如说)第 11 个元素 - 它根本不存在。

健全性检查 - 数组从 0 开始索引,rowcol 中的值是否从 1 开始索引?

Array Index Out Of Bounds means that your array has (say) 10 elements in it, but you've tried to access (say) the 11th element - it just doesn't exist.

Sanity check - arrays are indexed starting from 0, are your values in row and col indexed from 1?

秉烛思 2025-01-11 17:20:01

什么时候发生?

当您尝试访问索引超出其长度的数组时,会发生越界异常。 java数组的最大索引是(长度-1)
例如:

String [] stringArray = new String[10];
stringArray[10]
// the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.

如果您不知道数组的大小或长度,可以通过stringArray.length知道。

怎么处理呢?

您应该确保您的程序不会访问索引大于 length - 1 的数组。
示例:

for(int i=0;i<stringArray.lenght;i++) {
    //write your code here
}

上面的代码将保证 stringArray 的访问永远不会超出其最大索引。

您的情况

在您的情况下,您必须定义了数组限制并尝试访问超出定义限制范围的数组数据。

另请阅读了解更多信息...

示例二维数组出现越界异常

When it occurs?

Out of bounds exception is occurred when you try to access an array with index that exceeded its length. maximum index of a java array is (length -1)
for example:

String [] stringArray = new String[10];
stringArray[10]
// the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.

If you don't know the size or length of an array, you can know it from stringArray.length.

How to handle it?

You should make sure that your program doesn't access an array with index bigger than length - 1.
example:

for(int i=0;i<stringArray.lenght;i++) {
    //write your code here
}

the above code will guarantee that stringArray will never be accessed beyond its maximum index.

Your Case

In your case, you must have defined the array limit and trying to access the data of array which is out of range of defined limit.

Also read this for more info...

Example of 2d array having out of bound exception

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文