I'm writing a bit of code for a school project. However, I'm getting a:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 5 at TicTacToe.main(TicTacToe.java:25)
(This is the board[3-w1[i][0]][(w1[i][1])]='O'; bit)
This is a sample input:
5 (size of array)
1 0 0 (the number of black pieces to be placed on the board, followed by B pairs and the row and column of B black pieces)
1 4 4 (the number of white pieces to be placed on the board, followed by W pairs and the row and column of W white pieces)
4 0 0 1 1 1 1 2 2 2 2 3 1 3 1 4 0 (the number of moves, followed by 4 integers: Row 1 Column 1 Row 2 Column 2 which is the row and column of the start and end position of a move. I.e., the piece at R1 C1 is moved to R2 C2)
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Write your code here
int i,j;
int n=in.nextInt();
char board[][]=new char[n][n];
int b=in.nextInt();
int b1[][]=new int[b][2];
for(i=0;i<b;i++){
b1[i][0]=in.nextInt();
b1[i][1]=in.nextInt();
board[3-b1[i][0]][b1[i][1]]='*';
}
int w=in.nextInt();
int w1[][]=new int[w][2];
for(i=0;i<w;i++){
w1[i][0]=in.nextInt();
w1[i][1]=in.nextInt();
board[3-w1[i][0]][(w1[i][1])]='O';
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(board[i][j]!='*' && board[i][j]!='O')
board[i][j]='.';
}
}
int moves=in.nextInt();
char tag;
int m[][]=new int[moves*2][2];
for(i=0;i<(moves*2);i++){
m[i][0]=in.nextInt();
m[i][1]=in.nextInt();
}
for(i=0;i<(moves*2);i++){
tag=' ';
tag=board[3-m[i][0]][m[i][1]];
board[3-m[i][0]][m[i][1]]='.';
i++;
board[3-m[i][0]][m[i][1]]=tag;
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
System.out.print(board[i][j]+"");
}
System.out.println();
}
}
}
Is there a way to fix the error?