Skip to content

Instantly share code, notes, and snippets.

@kokx
Created October 31, 2010 13:39
Show Gist options
  • Save kokx/656596 to your computer and use it in GitHub Desktop.
Save kokx/656596 to your computer and use it in GitHub Desktop.
/*
* @date 2010/10/04
* @author Pieter Kokx <p.a.kokx@student.tue.nl> (0747517)
* @author Mitchel Brunings <m.d.brunings@student.tue.nl> (0746097)
*/
import java.util.*;
import java.io.*;
public class Sudoku
{
Scanner stdin;
int grid[][];
/**
* Get the next integer from the file.
*/
int getNextInteger(Scanner file)
{
String next;
while (file.hasNext()) {
next = file.next();
// first check if it is a .
if (next.equals(".")) {
return 0;
}
try {
// execute this only if we have an integer
return Integer.parseInt(next);
} catch (NumberFormatException e) {
continue;
}
}
// this will only happen if the input file is incorrectly formatted
return 0;
}
/**
* Read a file and parse the grid.
*/
void readGrid(String filename)
{
grid = new int[9][9];
try {
// read the file and
Scanner file = new Scanner(new File(filename));
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
grid[x][y] = getNextInteger(file);
}
}
} catch (FileNotFoundException e) {
System.out.println("http://www.youtube.com/watch?v=EK2tWVj6lXw");
}
}
/**
* Display the grid
*/
void displayGrid()
{
System.out.println("-------------------------");
// now display the grid itself
for (int x = 0; x < 9; x++) {
System.out.print("| ");
for (int y = 0; y < 9; y++) {
// display the number, or a . if it is a 0
if (grid[x][y] > 0) {
System.out.print(grid[x][y] + " ");
} else {
System.out.print(". ");
}
// show the horizontal bars
if ((y + 1) % 3 == 0) {
System.out.print("| ");
}
}
System.out.println("");
// show the vertical bars
if ((x + 1) % 3 == 0) {
System.out.println("-------------------------");
}
}
}
boolean checkRow(int x, int y, int n)
{
// false means no conflict
for (int i = 0; i < 9; i++) {
if (!(i == x)) {
if (n == grid[i][y]) {
return true;
}
}
}
return false;
}
boolean checkCol(int x, int y, int n)
{
// false means no conflict
for (int i = 0; i < 9; i++) {
if (!(i == y)) {
if (n == grid[x][i]) {
return true;
}
}
}
return false;
}
boolean checkBox(int x, int y, int n)
{
// false means no conflict
int x0 = x - (x % 3);
int y0 = y - (y % 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!((x0 + i == x) && (yo + j == y))) {
if (n == grid[x0+i][y0+j]) {
return true;
}
}
}
}
return false;
}
boolean check(int x, int y, int n)
{
return checkBox(x, y, n) || checkCol(x, y, n) || checkRow(x, y, n);
}
int[] searchZero()
{
for (int x = 0; x < 9; x++) {
for (int y = 0; y < 9; y++) {
if (grid[x][y] == 0) {
return new int[] {x, y};
}
}
}
return new int[] {-1, -1};
}
/**
* Solve the sudoku.
*/
void execute()
{
stdin = new Scanner(System.in);
readGrid(stdin.next());
displayGrid();
}
/**
* Main method
*/
public static void main(String[] args)
{
new Sudoku().execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment