Can't figure out Java Sudoku Solution Checker

233 views Asked by At

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.

1

There are 1 answers

3
Developer Marius Žilėnas On

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;
 }