Java:防止数组超出范围

时间:2010-03-12 07:33:20

标签: java arrays multidimensional-array

我正在制作一个跳棋游戏,如果你想了解更多关于你的信息,可以在这里查看; http://minnie.tuhs.org/I2P/Assessment/assig2.html

当我在进行测试时,看看玩家是否能够从其当前位置到达网格上的某个方格(即+1 + 1,+ 1 - 1 .etc),我得到了一个java.lang .ArrayIndexOutOfBoundsException错误。

这是我用来移动的代码;

public static String makeMove(String move, int playerNumber)
   {
       // variables to contain the starting and destination coordinates, subtracting 1 to match array size
       int colStart = move.charAt(1) - FIRSTCOLREF - 1;
       int rowStart = move.charAt(0) - FIRSTROWREF - 1;
       int colEnd   = move.charAt(4) - FIRSTCOLREF - 1;
       int rowEnd   = move.charAt(3) - FIRSTROWREF - 1;


       // variable to contain which player is which
       char player, enemy;
       if (playerNumber==1)
        {
            player= WHITEPIECE;
            enemy=  BLACKPIECE;
        }
       else
        {
            player= BLACKPIECE;
            enemy=  WHITEPIECE;
        }

        // check that the starting square contains a player piece
        if (grid [ colStart ] [ rowStart ] == player)
        {
            // check that the player is making a diagonal move
            if (grid [ colEnd ] [ rowEnd ] == grid [ (colStart++) ] [ (rowEnd++) ] &&
                grid [ colEnd ] [ rowEnd ] == grid [ (colStart--) ] [ (rowEnd++) ] &&
                grid [ colEnd ] [ rowEnd ] == grid [ (colStart++) ] [ (rowEnd--) ] &&
                grid [ colEnd ] [ rowEnd ] == grid [ (colStart--) ] [ (rowEnd--) ])
                {
                    // check that the destination square is free
                    if (grid [ colEnd ] [ rowEnd ] == BLANK)
                    {
                        grid [ colStart ] [ rowStart ] = BLANK;
                        grid [ colEnd ] [ rowEnd ]     = player;

                    }
                }
            // check if player is jumping over a piece
            else if (grid [ colEnd ] [ rowEnd ] == grid [ (colStart+2) ] [ (rowEnd+2) ] &&
                     grid [ colEnd ] [ rowEnd ] == grid [ (colStart-2) ] [ (rowEnd+2) ] &&
                     grid [ colEnd ] [ rowEnd ] == grid [ (colStart+2) ] [ (rowEnd-2) ] &&
                     grid [ colEnd ] [ rowEnd ] == grid [ (colStart-2) ] [ (rowEnd-2) ])
                 {
                   // check that the piece in between contains an enemy
                    if ((grid [ (colStart++) ] [ (rowEnd++) ] == enemy ) &&
                        (grid [ (colStart--) ] [ (rowEnd++) ] == enemy ) &&
                        (grid [ (colStart++) ] [ (rowEnd--) ] == enemy ) &&
                        (grid [ (colStart--) ] [ (rowEnd--) ] == enemy ))
                    {
                        // check that the destination is free
                        if (grid [ colEnd ] [ rowEnd ] == BLANK)
                        {
                            grid [ colStart ] [ rowStart ] = BLANK;
                            grid [ colEnd ] [ rowEnd ]     = player;
                        }

                    }
                 }

        }

我不确定如何防止错误发生,你推荐什么?

3 个答案:

答案 0 :(得分:1)

首先想到的是你在(colstart++)语句条件的中间使用if等后增量表达式。有些情况下这可能会有用,但我不相信你的是其中之一。

改用(colstart+1);它不会改变colstart变量本身的值,看起来就像你真正想做的那样。

更详细地说,假设colstart是4:

System.out.println(colstart); // prints 4
System.out.println(colstart++); // prints 4
System.out.println(colstart); // prints 5

比较:

System.out.println(colstart); // prints 4
System.out.println(colstart+1); // prints 5
System.out.println(colstart); // prints 4

答案 1 :(得分:0)

添加条件语句以确保您为数组提供的值介于0和大小-1之间

答案 2 :(得分:0)

每当您通过索引检查数组的内容时,有三种方法:

  1. “知道”您的索引在界限内
  2. 事先检查索引是否在范围内
  3. 访问索引越界后处理错误
  4. 因此,对于网格的每个维度以及事先检查索引的每个值或处理错误。

    我同意格雷格的看法,虽然我在这里看到了优点,但后期增量通常都会遇到麻烦。您可能想要检查后增量:

    // check that the player is making a diagonal move
            if (grid [ colEnd ] [ rowEnd ] == grid [ (colStart++) ] [ (rowEnd++) ] &&
                grid [ colEnd ] [ rowEnd ] == grid [ (colStart--) ] [ (rowEnd++) ] &&
                grid [ colEnd ] [ rowEnd ] == grid [ (colStart++) ] [ (rowEnd--) ] &&
                grid [ colEnd ] [ rowEnd ] == grid [ (colStart--) ] [ (rowEnd--) ])
    

    从一些假设的观点(2,2)开始:

    colStart = 2 - > 3 - > 2 - > 3 => 2 rowEnd = 2 - > 3 - > 4 - > 3 => 2

    因此,从(2,2)开始,您再次检查(3,3),(2,4)和(3,3),以原始值结束。这对我来说似乎非常不受欢迎,因为你正在进行两次相同的测试。 (可能是你打算打其他地方。)