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?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Ist die erste Sache, springt in den Sinn ist Ihr Gebrauch post-Inkrement-Ausdrücke wie
(colstart++)
in der Mitte eineif
Anweisung Bedingung. Es gibt sicherlich Fälle, in denen dies nützlich sein könnte, aber ich glaube nicht, dass Sie solch einen von Ihnen.Verwenden
(colstart+1)
statt; es ändert nicht den Wert dercolstart
variable selbst und es sieht aus wie das, was Sie wirklich wollen, zu tun.Im detail, vermute
colstart
4:Vergleichen:
grid[colstart-1]
überprüfen, um sicherzustellen, dasscolstart-1 >= 0
. Ebenso, vorgrid[colstart+1]
überprüfen, dasscolstart+1 < grid.length
.Fügen Sie bedingte Anweisungen, um sicherzustellen, dass die Werte, die Sie für die Fütterung des Arrays liegen zwischen 0 und size - 1
Wann immer Sie prüfen den Inhalt von einem array-index gibt es drei Wege:
Somit für jede dimension des Rasters und jedem Wert für einen index, im Vorfeld zu überprüfen oder den Fehler behandeln.
Stimme ich Greg, dass postincrements sind in der Regel ärger, obwohl ich die Verdienste hier. Möchten Sie vielleicht zu prüfen, die postincrements:
Beginnend mit einigen hypothetischen Punkt (2,2):
colStart = 2 -> 3 -> 2 -> 3 => 2
rowEnd = 2 -> 3 -> 4 -> 3 => 2
So, ausgehend von (2,2), das Sie prüfen (3,3), (2,4) und (3,3) wieder, die Ziellinie mit dem ursprünglichen Wert. Dies scheint äußerst unerwünscht zu mir, denn du machst den gleichen test zweimal. (Könnte sein, dass bedeutete, schlagen andere stellen.)