0

I've missed the deadline for this assignment but it is still bugging me that I do not understand what I'm doing for this project. Its part 2 of a sudoku solution checker that needs four methods added to it which are

public boolean checkAndPrintReport( ) {*/return true;}

which should check all and print lines for every failed row or column. The others are

public boolean isGoodRow( int yRowParam ) {return true;}
public boolean isGoodColumn( int xColParam ) {return true;}
public boolean isGoodBlock(int xBlockP, int yBlockP) {return true;} 

Lastly, my checkAll() method is supposed to have three nested loops calling the above three 9 times each.

I don't understand what is needed for this part since I thought I already coded a solution checker here

public int timesRowHas( int yParam, int number ) { 
    int nTimesHas = 0;
    for( int x = 0; x < 9; x++ )
        if( this.getCell(x, yParam) == number )
            nTimesHas = nTimesHas + 1;

    return( nTimesHas );
}

public int timesColHas( int xParam, int number ) {
    int nTimesHas = 0;
    for( int y = 0; y < 9; y++ )
        if( this.getCell(xParam, y) == number )
            nTimesHas = nTimesHas + 1;

    return( nTimesHas );
}

public int timesBlockHas( int xBlockParam, int yBlockParam, int number ) {
    if( xBlockParam < 0 || xBlockParam > 2 || yBlockParam < 0 || yBlockParam > 2 )
        throw new IllegalArgumentException("Bad xBlockParam or bad yBlockParam or both..");

    int nTimesHas = 0; 
    for (int x=0; x<3; x++)
        for (int y=0; y<3;y++)
            nTimesHas = nTimesHas +getCell(xBlockParam+x, yBlockParam+y);

    return(nTimesHas);
 }
bcorso
  • 45,608
  • 10
  • 63
  • 75
user3060040
  • 77
  • 1
  • 9
  • I think the OP is asking if the functions he's already written accomplish the goals of the functions he is being asked to write, and why/why not? – bcorso Dec 04 '13 at 06:54

1 Answers1

0

The functions you wrote are not exactly what you need. Your functions only check how many times a single number is in a row (column or box).

To tell if a row (column or box) is good, you really need to check all numbers (1-9), not just one of them.

However, the good news is you can implement the needed functions using your functions:

public boolean isGoodRow( int yRowParam ){
    // for number = 1,..,9, ensure the count is not > 1
    for(int i=1; i<=9; i++)
        if(timesRowHas(yRowParam, i) > 1)
            return false;
    return true;
}

(In case you are interested): This is not the most efficient solution, it runs in O(n2) time. isGoodRow() can be found in O(n) time by histograming the #'s in the row.


Once you have implemented the necessary functions:

public boolean isGoodRow( int yRowParam )
public boolean isGoodColumn( int xColParam )
public boolean isGoodBlock(int xBlockP, int yBlockP)

Then you just need to use those to implement checkAndPrintReport():

public boolean checkAndPrintReport(){ 
   for(int i=0; i<9; i++)
        if(!isGoodRow(i) || !isGoodColumn(i) || !isGoodBlock(i/3, i%3)
            return false;
   return true;
}
bcorso
  • 45,608
  • 10
  • 63
  • 75
  • I don't get what I am supossed to put in the suggested functions though, it seems like they are asking for what I already did above. – user3060040 Dec 04 '13 at 05:13
  • Okay the way you explain it makes more sense now. Does the Block function work as well? And is checkAll supossed to be in a different .java to call the other methods? – user3060040 Dec 04 '13 at 06:05
  • It will work with your Block function once you fix it. getCell(xBlockParam+x, yBlockParam+y) should be getCell(3*xBlockParam+x, 3*yBlockParam+y) – bcorso Dec 04 '13 at 06:33
  • @user3060040 if this helped you please choose this as the correct answer and up-vote, thanks! – bcorso Dec 04 '13 at 07:19