Skip to content

Sudoku File Formats

For testing the solver I obviously need few puzzles to work with. Since I am no fan of manually inserting the values I was looking around for some database to download. Whauh! It turned out there are tons of different file formats, solving techniques and so on. This page shows just few. I am amazed how many different techniques for solving sudokus there are; or helping techniques or what should they have being called, at least according to that rating table on the page.

I haven’t actually managed to find too many free databases with Sudoku games. I did found few on page for SimpleSudoku and quite a few overhere. The former seems to be devoted to a special problem (the smallest number of cues required for unique solution). Database consists of almost 50 000 puzzles in sdm format which is quite trivial and easy to parse so I have kind opted to use those puzzles to start with.

Here is fast and dirty parser for puzzles in sdm format:

import java.io.*;

public class SDMFileReader implements SudokuFileReader {
    
    public SDMFileReader(String filename){
	try {
	    br = new BufferedReader(new FileReader(filename));	    
	}catch(Exception e){
	    e.printStackTrace();
	    try{
		br.close();
	    }catch(Exception e2){
	    }
	}
    }
    
    public boolean hasMoreRows(){
	return rowoffset < kind*kind;
    }

    public boolean hasMoreGames(){
	boolean b = false;
	try{
	    b = br.ready();
	}catch(Exception e){
	}
	return b;
    }

    public void readGame(){
	try{
	    rowoffset = 0;
	    game = br.readLine();
	}catch(Exception e){
	}
    }

    public String nextRow(){
	String row = game.substring(rowoffset,rowoffset+kind);
	rowoffset += 9;
	return row;
    }

    public void close(){
	try{
	    br.close();
	}catch(Exception e2){
	}
    }
    
    private File file;
    private String game;
    private int kind = 9;
    private BufferedReader br;
    private int rowoffset = 0;
}

Obviously some more control on input correctness might be in place, but for a starter and time invested, this one is just fine :).

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*