Need help on counting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickels
    New Member
    • Sep 2008
    • 20

    Need help on counting

    Well im writing a program in java and my program had to do with Sudoku but its only 4 by 4 not 9 by 9. My program checks to see weather the rows, columns, and regions add up to 10. The output would look like this...

    Enter Row 1: 3 2 4 1
    Enter Row 2: 4 1 3 2
    Enter Row 3: 1 4 2 3
    Enter Row 4: 2 3 1 4

    REG-1:GOOD
    REG-2:GOOD
    REG-3:GOOD
    REG-4:GOOD

    ROW-1:GOOD
    ROW-2:GOOD
    ROW-3:GOOD
    ROW-4:GOOD

    COL-1:GOOD
    COL-2:GOOD
    COL-3:GOOD
    COL-4:GOOD

    SUDO:VALID

    My problem is the last part where it says sudo:valid. I know i need to make a variable that counts the number of things that are invalid and if that variable's value is 0 at the end of the program then i have a valid sudoku. But im not sure how to do this. Anyone know how? Heres my code

    Code:
    import java.util.Scanner;
    
    public class test
     {
    public static void main( String args[] )
     {
    Scanner input = new Scanner( System.in );
      int a1, a2, a3, a4, b1, b2, b3, b4, c1, c2, c3, c4, d1, d2, d3, d4;
    
    System.out.print ( "Enter First Row:" ); 
     a1 = input.nextInt();  
     a2 = input.nextInt(); 
     a3 = input.nextInt();
     a4 = input.nextInt(); 
    
    System.out.print ( "Enter Second Row:" ); 
     b1 = input.nextInt();  
     b2 = input.nextInt(); 
     b3 = input.nextInt();
     b4 = input.nextInt(); 
    
    System.out.print ( "Enter Third Row:" ); 
     c1 = input.nextInt();  
     c2 = input.nextInt(); 
     c3 = input.nextInt();
     c4 = input.nextInt(); 
    
    System.out.print ( "Enter Fourth Row:" ); 
     d1 = input.nextInt();  
     d2 = input.nextInt(); 
     d3 = input.nextInt();
     d4 = input.nextInt(); 
    
    // Regions
    
    if (a1 + a2 + b1 + b2 == 10)
     {
    System.out.println ( "\nREG-1:GOOD" );
     }
    else
     {
    System.out.println ( "\nREG-1:BAD" );
     }
    
    if (c1 + c2 + d1 + d2 == 10)
     {
    System.out.println ( "REG-2:GOOD" );
     }
    else
     {
    System.out.println ( "REG-2:BAD" );
     }
    
    if (a3 + a4 + b3 + b4 == 10)
     {
    System.out.println ( "REG-3:GOOD" );
     }
    else
     {
    System.out.println ( "REG-3:BAD" );
     }
    
    if (c3 + c4 + d3 + d4 == 10)
     {
    System.out.println ( "REG-4:GOOD" );
     }
    else
     {
    System.out.println ( "REG-4:BAD" );
     }
    
    // Rows
    
    if (a1 + a2 + a3 + a4 == 10)
     {
    System.out.println ( "\nROW-1:GOOD" );
     }
    else
     {
    System.out.println ( "\nROW-1:BAD" );
     }
    
    if (b1 + b2 + b3 + b4 == 10)
     {
    System.out.println ( "ROW-2:GOOD" );
     }
    else
     {
    System.out.println ( "ROW-2:BAD" );
     }
    
    if (c1 + c2 + c3 + c4 == 10)
     {
    System.out.println ( "ROW-3:GOOD" );
     }
    else
     {
    System.out.println ( "ROW-3:BAD" );
     }
    
    if (d1 + d2 + d3 + d4 == 10)
     {
    System.out.println ( "ROW-4:GOOD" );
     }
    else
     {
    System.out.println ( "ROW-4:BAD" );
     }
    
    
    // comlumns
    
    if (a1 + b1 + c1 + d1 == 10)
     {
    System.out.println ( "\nCOL-1:GOOD" );
     }
    else
     {
    System.out.println ( "\nCOL-1:BAD" );
     }
    
    if (a2 + b2 + c2 + d2 == 10)
     {
    System.out.println ( "COL-2:GOOD" );
     }
    else
     {
    System.out.println ( "COL-2:BAD" );
     }
    
    if (a3 + b3 + c3 + d3 == 10)
     {
    System.out.println ( "COL-3:GOOD" );
     }
    else
     {
    System.out.println ( "COL-3:BAD" );
     }
    
    if (a4 + b4 + c4 + d4 == 10)
     {
    System.out.println ( "COL-4:GOOD" );
     }
    else
     {
    System.out.println ( "COL-4:BAD" );
     }
    
    
    }
    
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Check an article in the Java howTos area by Jos which talks about sudoku solvers.

    Comment

    • ManidipSengupta
      New Member
      • Sep 2008
      • 4

      #3
      Have you thought about using arrays? Here you go -

      Code:
      import java.util.Scanner; 
        
      public class Sudoku4 { 
      	protected	int	n=4, checkSum=10, failCount=0;
      	protected	Scanner	input = new Scanner(System.in);
      
      	public Sudoku4 () {
      		int[][] a = new int[n][n];
      		for (int iRow = 0 ; iRow < n ; iRow++) {
      			System.out.print ( "Enter Row #"+(iRow+1)+ ": ");
      			for (int jCol = 0 ; jCol < n ; jCol++)
      				a[iRow][jCol] = input.nextInt();
      		}
      
      		// Regions
      		System.out.println();
      		int regionIndex = 1, xCount = n/2, yCount = n/2;
      		for (int ix = 0 ; ix < n ; ix += xCount) {
      			for (int jy = 0 ; jy < n ; jy += yCount) {
      				failCount += checkRegion(a,
      					ix, jy, xCount, yCount,
      					"REG-" + regionIndex + ":");
      				regionIndex++;
      		}}
      
      		// Rows
      		System.out.println();
      		regionIndex = 1; xCount = 1; yCount = n;
      		for (int ix = 0 ; ix < n ; ix += xCount) {
      			for (int jy = 0 ; jy < n ; jy += yCount) {
      				failCount += checkRegion(a,
      					ix, jy, xCount, yCount,
      					"ROW-" + regionIndex + ":");
      				regionIndex++;
      		}}
      
      		// Columns
      		System.out.println();
      		regionIndex = 1; xCount = n; yCount = 1;
      		for (int ix = 0 ; ix < n ; ix += xCount) {
      			for (int jy = 0 ; jy < n ; jy += yCount) {
      				failCount += checkRegion(a,
      					ix, jy, xCount, yCount,
      					"COL-" + regionIndex + ":");
      				regionIndex++;
      		}}
                      }
      
      	private int checkRegion (int[][] a, int rowStart, int colStart,
      			int rowCount, int colCount, String identifier) {
      		int sum = 0, errCountInRegion = 1;
      		for (int i = 0; i < rowCount ; i++)
      			for (int j = 0 ; j < colCount ; j++)
      				sum += a[i+rowStart][j+colStart];
      		if (sum == checkSum)
      			errCountInRegion = 0;
      		System.out.println (identifier + 
      			((errCountInRegion == 0) ? "GOOD" : "BAD"));
      		return errCountInRegion;
      	}
      
      	public void printFailCount () {
      		System.out.println();
      		if (failCount == 0)
      			System.out.println ("Sudoku: VALID");
      		else
      			System.out.println("Fail Count = " + failCount);
      	}
      
      	public static void main(String args[] ) { 
      		new Sudoku4().printFailCount();
      	}
      }
      I learnt about the Scanner class today, thanks to you. Best wishes, - M.

      Comment

      • moorcroft
        New Member
        • Mar 2008
        • 57

        #4
        you could always just add a count variable at the top ie:

        Code:
        int failCount = 0;
        Then each time it hits the code where it says

        Code:
        {
        //if rows do not add to 10 output fail;
        failCount++;
        }
        Then at the end, say

        Code:
        if failCount>0 output "Fail"

        Comment

        Working...