Java: Verhinderung von array-going out-of-bounds

Arbeite ich an einem Spiel der Kontrolleure, wenn Sie mehr Lesen möchten über man kann es hier; http://minnie.tuhs.org/I2P/Assessment/assig2.html

Wenn ich Tue mein test, um zu sehen, wenn der Spieler ist in der Lage, um auf einem bestimmten Platz auf dem Netz (D. H. +1 +1, +1 -1 .etc) von der aktuellen Position, ich bekomme eine java.lang.ArrayIndexOutOfBoundsException-Fehler.

Dies ist der code, den ich benutze, um den Umzug machen;

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;
                        }

                    }
                 }

        }

Ich bin mir nicht sicher, wie kann ich verhindern, dass der Fehler passiert, was empfehlen Sie?

InformationsquelleAutor Troy | 2010-03-12
Schreibe einen Kommentar