1

This program is supposed to return the location of a piece on a board.The board is being represented as a 2D array. The loop simply cycles through the columns and then if the piece is still not found it goes to the next row. However,only the [-1,-1] array is being returned( no pieces are being found). I have not been able to find the error on my own, so any help

public int[] pieceFinder(int piece) {

    int[][] board={{43,44,45,50,47,48,49},
                   {36,37,38,39,40,41,42},
                   {29,30,31,32,33,34,35},
                   {22,23,24,25,26,27,28},
                   {15,16,17,18,19,20,21},
                   {8,9,10,11,12,13,14},
                   {1,2,3,50,5,6,7}};    

    int row=0;  
    boolean found=false;
    int[] location={-1,-1} ;  

    for(int column=0;found==true;column++) {
        if(board[row][column]==piece) {
            found=true ;
            location[0]=row; 
            location[1]=column;
        }

        else if(column==6) {
            if(row==6) { 
                break;
            }

        else {
            row++;
        }
    } 
    return location;
}             
1
  • Think carefully about the loop condition. Double-check with your book what it means. Commented Jul 5, 2015 at 6:59

1 Answer 1

4
boolean found=false;
for(int column=0;found==true;column++){

As you can see found is false. And for loop checks for true value for found. It will never enter the loop. That's why locations gets returned as it is.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.