mardi 24 février 2015

Placing array into another array trouble



I'm trying to get this maze program down, but I have been at a stand still for about 3 days on trying to print out my maze inside a boarder of X's. I have created a new array (that is what made the most sense to me) bigger than the original array and just display X's around the outside. I keep getting and out of bounds error on the line of mazeValue[0][i] = 'X';. I mean obviously I know what that means, but I can't make since of how to fix it. I think my logic is right, maybe not. Any help would be much much appreciated.





public static void main(String[] args) {
// TODO code application logic here
Scanner kbd = new Scanner(System.in);
System.out.println("ENTER A SINGLE INTEGER: ");
int n = kbd.nextInt();
char[][] mazeValue = new char[n][n];
System.out.println("ENTER A PATH: ");
for(int i = 0; i < mazeValue.length; i++ ){
for(int j = 0; j< mazeValue[i].length; j++){
mazeValue[i][j]= kbd.next().charAt(0);
}
}
boarder(mazeValue,n);
printMaze(mazeValue);
horizontalPath(mazeValue,n);
//veritcalPath(mazeValue);
}
public static char[][] boarder(char[][] mazeValue,int n)
{
char[][] newArray = new char[n+2][n+2];
newArray = mazeValue;
for(int i =0; i<n+2;i++){
mazeValue[0][i] = 'X';
}
for(int i=0; i<n+2;i++){
mazeValue[i][0] = 'X';
}
for(int i= 0; i<n+2;i++){
mazeValue[n+1][0]= 'X';
}
return newArray;
}
public static void printMaze(char newArray[][])
{
System.out.println("MAZE");
for(int i = 0; i < newArray.length; i ++)
{
for (int j = 0; j < newArray[i].length; j++)
{
System.out.printf("%5c",newArray[i][j]);
}
System.out.printf("\n");
}
}





Aucun commentaire:

Enregistrer un commentaire