0

I'm working on a Sudoku Solution checker in Java right now that had been split into two parts. This second part wants five new "specific methods" added into the code which are boolean checks for rows, columns, blocks, and then return if they are true or false, in loops. checkAndPrintReport is supossed to also print one line for every single failed check of a row, failed check of a column and failed check of a block.This is what I have so far.

 public boolean checkAndPrintReport( )
  {
   return false;
  }
 public boolean isGoodRow( int yRowParam ) 
  {  
  int sum = 0;
  for ( int x = 0; x <9; x++)
    {
      sum = sum + getCell (x,yRowParam);
    }
  return( true );
  } 
 public boolean isGoodColumn( int xColParam ) 
  {
   int sum = 0;
   for (int y = 0; y < 9 ;y++)
    {
        sum = sum + getCell (xColParam, y);
    }
    return( true );
  } 
 public boolean isGoodBlock(int xBlockP, int yBlockP)  
  { 
    int sum = 0;
    for (int x=0; x<3; x++)
    {
        for (int y=0; y<3;y++)
        {
            sum = sum + getCell (xBlockP+x, yBlockP+y);
        }
    }
   return( true );
  }
 public boolean checkAll()
  {
  }

I think the main part confusing me right now is how exactly this differs from the code I had already created that checks for these things...so I'm kind of confused as to whats being asked of me.

user3060040
  • 77
  • 1
  • 9
  • 2
    Make the existing code work first. All these functions always return true, because the sum value is not tested. – PMF Dec 03 '13 at 06:06

1 Answers1

0

Define

/** sum of 1+2+...+9 */
static final int SUM = 45;

Then your code looks like this

public boolean isGoodColumn( int xColParam ) {
   int sum = 0;
   for (int y = 0; y < 9 ;y++) {
        sum = sum + getCell (xColParam, y);
    }
   return SUM == sum;
 } 


public boolean isGoodColumn( int xColParam ) {
   int sum = 0;
   for (int y = 0; y < 9 ;y++) {
        sum = sum + getCell(xColParam, y);
    }
   return SUM == sum;
 } 

 public boolean isGoodBlock(int xBlockP, int yBlockP) { 
    int sum = 0;
    for (int x=0; x<3; x++) {
        for (int y=0; y<3;y++) {
            sum = sum + getCell(xBlockP+x, yBlockP+y);
        }
    }
   return SUM == sum;
 }
  • I don't think that testing the sum is enough to ensure a Sudoku is valid. I'll fill the whole table with fives and will pass all your tests. – PMF Dec 03 '13 at 15:41
  • @PMF what is your code passing your test? I think user would benefit from seeing it :) – Developer Marius Žilėnas Dec 04 '13 at 05:37
  • I did once write some code for this, gotta dig for it. But that's not the point. I was just saying that the algorithm proposed here is not getting the expected result. – PMF Dec 04 '13 at 05:50