java - 检查条件时数组索引越界异常

标签 java android arrays

我开始在android上制作一款名为GO的游戏(日本传统游戏)。 推荐人:GO

我想在每个玩家获得一个回合时设定一个条件,所以当玩家回合时,条件会去查看石头的位置,如果放置的石头位置有可能获得自由,则运行条件,否则不可以。这是编码:

public void checkCondition(int position, View v){

    final ImageButton[] arrPlayer1 = { //Board
            g1  ,g2,  g3,  g4,  g5,  g6,  g7,  g8,  g9, 
            g10, g11, g12, g13, g14, g15, g16, g17, g18, 
            g19, g20, g21, g22, g23, g24, g25, g26, g27, 
            g28, g29, g30, g31, g32, g33, g34, g35, g36, 
            g37, g38, g39, g40, g41, g42, g43, g44, g45, 
            g46, g47, g48, g49, g50, g51, g52, g53, g54, 
            g55, g56, g57, g58, g59, g60, g61, g62, g63, 
            g64, g65, g66, g67, g68, g69, g70, g71, g72, 
            g73, g74, g75, g76, g77, g78, g79, g80, g81};

    int posT = position - 9; //Top Position
    int posD = position + 9;  //Down Position
    int posL = position - 1; //Left Position
    int posR = position + 1; //Right Position

    ImageView BlackStone = (ImageView) findViewById(R.drawable.BlackStone);

    if(v == arrPlayer1[position] && arrPlayer1[posT] != null){
        if(arrPlayer1[posT] == BlackStone){
            if(v == arrPlayer1[position] && arrPlayer1[posD] != null){
                if(arrPlayer1[posD] == BlackStone){
                    if(v == arrPlayer1[position] && arrPlayer1[posR] != null){
                        if(arrPlayer1[posR] == BlackStone){
                            if(v == arrPlayer1[position] && arrPlayer1[posL] != null){
                                if(arrPlayer1[posL] == BlackStone){
                                    ChangeTurn(v);
                                }else{
                                    CheckLiberty(position, v);
                                }
                            }
                        }else{
                            CheckLiberty(position, v);
                        }
                    }
                }else{
                    CheckLiberty(position, v);
                }
            }
        }else{
            CheckLiberty(position, v);
        }
    }

} 

我的逻辑是,我在这个编码中创建了一个动态条件,这样无论玩家走到哪里,它都会看到石头的状况,例如:如果玩家在 G20 放置石头,那么逻辑将在 G11、G19 中看到, G21、G29等互连石材。

int posT = position - 9; //Top Position
int posD = position + 9;  //Down Position
int posL = position - 1; //Left Position
int posR = position + 1; //Right Position

但它仍然错误,并且 logcat 在方法 (CheckCondition) 处给我错误数组越界异常。所以我该怎么做 ?感谢任何帮助

最佳答案

第一个变化:

ImageView BlackStone = (ImageView) findViewById(R.drawable.BlackStone);

ImageView BlackStone = (ImageView) findViewById(R.id.IdOfImageview);

当您使用 findViewById 时,您需要通过其 Id 而不是其 background 来查找任何组件的 View 。

关于java - 检查条件时数组索引越界异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19920688/

相关文章:

java - 为什么Java不释放内存

java - DLQueryExample 无法加载本体

java - 如何调试 Java 内存错误?

android - 从 imageview 和 textview 创建图像?

android - 如何让安卓模拟器进入休眠模式?

arrays - angular2 bool 数组

java - Android:如何让 App Locale 独立于 System Locale 工作?

java - 使用首选 DateFormat 格式化 JodaTime DateTime

java - 从Java中的字符串数组中提取

c++ - 创建对象的动态数组