mercredi 25 février 2015

Array count logic error

I am trying to take my array char[][] mazeValue (which has a boarder of X's around my actual values I am comparing) and compare each element inside that array to the element to the left,right,top,bottom, and diagonals to see if there are two O's in a row, and if so I add one to the openfactor. I have walked through my logic all morning, I do not see where I'm going wrong. An extra set of eyes always helps!


Output:



TOTAL OPENESS: 0

cout: 1

cout: 1

cout: 1

cout: 2

cout: 2

cout: 2

TOTAL OPENESS: 2

cout: 2

TOTAL OPENESS: 2

cout: 2

TOTAL OPENESS: 2




public static void openfactor(char[][] mazeValue, int n)
{
int count=0;
int totalOpeness=0;

for(int i = 1; i<mazeValue.length-1; i++){
for(int j=1;j<mazeValue[i].length-1;j++){

if(mazeValue[i][j]=='X'){
System.out.println("cout: "+count);
}

else if(mazeValue[i][j-1]=='O'){
count++;
System.out.println("cout: "+count);
if(mazeValue[i-1][j-1]=='O')
count++;
System.out.println("cout: "+count);
if(mazeValue[i-1][j]=='O')
count++;
System.out.println("cout: "+count);
if(mazeValue[i][j+1]=='O')
count++;
System.out.println("cout: "+count);
if(mazeValue[i+1][j+1]=='O')
count++;
if(mazeValue[i+1][j]=='O')
count++;
System.out.println("cout: "+count);
if(mazeValue[i+1][j-1]=='O')
count++;
System.out.println("cout: "+count);

totalOpeness = totalOpeness +count;

}
System.out.println("TOTAL OPENESS: "+totalOpeness);
}
}
}
public static void printMaze(char mazeValue[][]) {
System.out.println("MAZE");
for (int i = 1; i < mazeValue.length; i++) {
for (int j = 1; j < mazeValue[i].length; j++) {
System.out.printf("%5c", mazeValue[i][j]);
}
System.out.printf("\n");
}
}
public static void main(String[] args) {
// TODO code application logic here
// 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 + 1][n + 1];
System.out.println("ENTER A PATH: ");
for (int i = 0; i < mazeValue.length; i++) {
for (int j = 0; j < mazeValue[i].length; j++) {
if (i == 0 || j == 0 || i == n + 1 || j == n + 1)
mazeValue[i][j] = 'X';
else {
mazeValue[i][j] = kbd.next().charAt(0);
}
}
}
printMaze(mazeValue);
openfactor(mazeValue, n);
}

Aucun commentaire:

Enregistrer un commentaire