Quote (labatymo @ Apr 26 2013 10:53am)
The CheckRow column works, but I wasn't able to test the rest cus it's a pain in the ass typing in all those numbers and i'm pretty busy at work right now. Let me know if it works. If not, I'll work on it some more.
And yea, this is Java, not Javascript
Code
import java.util.HashSet;
import java.util.Scanner;
public class Sudoku {
static int board[][];
public Sudoku( ) {
board = new int[9][9];
}
public String toString( ) {
String result = "";
for ( int rowOfBlocks = 0; rowOfBlocks < 3; rowOfBlocks = rowOfBlocks + 1 ) {
for ( int rowInABlock = 0; rowInABlock < 3; rowInABlock = rowInABlock + 1 ) {
for ( int colOfBlocks = 0; colOfBlocks < 3; colOfBlocks = colOfBlocks + 1 ) {
for ( int colInABlock = 0; colInABlock < 3; colInABlock = colInABlock + 1 ) {
result = result
+ board[colOfBlocks * 3 + colInABlock][rowOfBlocks * 3
+ rowInABlock] + " ";
}
result = result + " ";
}
result = result + "\n";
}
result = result + "\n";
}
return result;
}
public void read( Scanner scParam ) {
for ( int rowOfBlocks = 0; rowOfBlocks < 3; rowOfBlocks = rowOfBlocks + 1 ) {
for ( int rowInABlock = 0; rowInABlock < 3; rowInABlock = rowInABlock + 1 ) {
for ( int colOfBlocks = 0; colOfBlocks < 3; colOfBlocks = colOfBlocks + 1 ) {
for ( int colInABlock = 0; colInABlock < 3; colInABlock = colInABlock + 1 ) {
board[colOfBlocks * 3 + colInABlock][rowOfBlocks * 3 + rowInABlock] = scParam.nextInt( );
}
}
}
}
}
public static boolean checkRow( int row ) {
for ( int j = 0; j < 9; j++ )
for ( int k = j + 1; k < 9; k++ )
if ( k != j && board[k][row] == board[j][row] ) return false;
return true;
}
public boolean checkColumn( int column ) {
for ( int j = 0; j < 9; j++ )
for ( int k = j + 1; k < 9; k++ )
if ( k != j && board[column][k] == board[column][j] ) return false;
return true;
}
public boolean checkBlock( int colOfBlocks, int rowOfBlocks ) {
for ( int i = colOfBlocks; i < colOfBlocks + 3; i++ ) {
HashSet<Integer> set = new HashSet<Integer>( );
for ( int j = rowOfBlocks; j < rowOfBlocks + 3; j++ ) {
if ( set.contains( board[j][i] ) ) return false;
set.add( board[j][i] );
}
}
return true;
}
public boolean checkAll( ) {
for ( int i = 0; i < 9; i++ ) {
if ( !checkRow( i ) ) {
return false;
}
if ( !checkColumn( i ) ) {
return false;
}
}
return true;
}
public static void main( String[] a ) {
Scanner s = new Scanner( System.in );
Sudoku sudokuRef = new Sudoku( );
sudokuRef.read( s );
System.out.print( sudokuRef );
}
}
Thank you very much. I'm waiting to hear back from my professor on a question about the project and then I'll be able to tell you how it runs.
I did run into a compiler issue though, that HashSet cannot be resolved to a type. He's never mentioned HashSet in lecture so I'm not sure what to do.