I'm writing a game on Android platform. I have array of buttons and when I click in one of them, it will show up "X" on it and neighbouring buttons like this:
X
X X X
X
But when I click one of the border buttons application is stopping.
This is my method responsible for setting text on buttons (if you need more code, please tell me):
private void changeState(int locationVer, int locationHor){
String buttonText = BoardButtons[locationVer][locationHor].getText().toString();
String rightButtonText = BoardButtons[locationVer+1][locationHor].getText().toString();
String leftButtonText = BoardButtons[locationVer-1][locationHor].getText().toString();
String lowerButtonText = BoardButtons[locationVer][locationHor+1].getText().toString();
String upperButtonText = BoardButtons[locationVer][locationHor-1].getText().toString();
if(locationVer == 0) {
if (buttonText == String.valueOf(Game.PLAYER)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
}
else if(locationVer == 6) {
if (buttonText == String.valueOf(Game.PLAYER)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
}
else if(locationHor == 0) {
if (buttonText == String.valueOf(Game.PLAYER)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
}
if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
}
}
else if(locationHor == 6) {
if (buttonText == String.valueOf(Game.PLAYER)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
}
else {
if (buttonText == String.valueOf(Game.PLAYER)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
if (buttonText == String.valueOf(Game.EMPTY_SPACE)) {
BoardButtons[locationVer][locationHor].setText((String.valueOf(Game.PLAYER)));
if (rightButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (rightButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer + 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (leftButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.EMPTY_SPACE)));
if (leftButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer - 1][locationHor].setText((String.valueOf(Game.PLAYER)));
if (lowerButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (lowerButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor + 1].setText((String.valueOf(Game.PLAYER)));
if (upperButtonText == String.valueOf(Game.PLAYER))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.EMPTY_SPACE)));
if (upperButtonText == String.valueOf(Game.EMPTY_SPACE))
BoardButtons[locationVer][locationHor - 1].setText((String.valueOf(Game.PLAYER)));
}
}
}
This is what logcat shows:
06-06 17:13:56.727 2232-2232/pl.edu.uksw.fieldsgame E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: pl.edu.uksw.fieldsgame, PID: 2232
java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
at pl.edu.uksw.fieldsgame.MainActivity.changeState(MainActivity.java:149)
at pl.edu.uksw.fieldsgame.MainActivity.access$200(MainActivity.java:13)
at pl.edu.uksw.fieldsgame.MainActivity$ButtonClick.onClick(MainActivity.java:137)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)