package sudoku.parse;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import sudoku.Puzzle;

/**
 * <p>Parses the initial puzzle out of a Sadman Sudoku puzzle 
 * file. This format is defined at 
 * http://www.sadmansoftware.com/sudoku/faq19.php (retrieved 25 
 * May 2016).</p>
 * 
 * <p>All elements of a Sadman format file other than the 
 * initial state of the puzzle are ignored, including the 
 * number of solutions.</p>
 * @author fiveham
 *
 */
public class SadmanParser implements Parser{
	
	/**
	 * <p>Content of the line prior to the statement of the initial 
	 * state of the puzzle.</p>
	 */
	public static final String INITIAL_PUZZLE_MARKER = "[Puzzle]";
	
	/**
	 * <p>Indicates an empty cell in the initial puzzle state.</p>
	 */
	public static final char EMPTY_CELL = '.';
	
	private final int mag;
	private final List<Integer> values;
	
	public SadmanParser(File f, String charset) throws FileNotFoundException{
		Scanner s = new Scanner(f, charset);
		while(s.hasNext() && !INITIAL_PUZZLE_MARKER.equals(s.nextLine()));
		
		if(!s.hasNext()){
			s.close();
			s = new Scanner(f, charset);
		}
		
		StringBuilder initCells;
		try{
			initCells = new StringBuilder(s.nextLine());
			this.mag = (int)Math.sqrt(initCells.length());
			for(int i=1; i<mag*mag; ++i){
				initCells.append(s.nextLine());
			}
		} catch(NoSuchElementException e){
			throw new IllegalArgumentException("Could not parse the file as Sadman format", e);
		} finally{
			s.close();
		}
		this.values = new ArrayList<>(initCells.length());
		for(int i=0; i<initCells.length(); ++i){
			char c = initCells.charAt(i);
			values.add(c == EMPTY_CELL 
					? Puzzle.BLANK_CELL 
					: Integer.parseInt(Character.toString(c), mag*mag+1));
		}
	}
	
	@Override
	public int mag() {
		return mag;
	}
	
	@Override
	public List<Integer> values() {
		return values;
	}
}

@@ -134,16 +134,52 @@
 canner;%0A
+import java.util.function.Supplier;%0A
 import j
@@ -1020,118 +1020,901 @@
 %0A%09%0A%09
-public SadmanParser(File f, String charset) throws FileNotFoundException%7B%0A%09%09Scanner s = new Scanner(f, charset
+/**%0A%09 * %3Cp%3EConstructs a Parser that interprets the contents of the file %7B@code f%7D %0A%09 * as a Sadman-format sudoku puzzle, using the specified %7B@code charset%7D to %0A%09 * read the file.%3C/p%3E%0A%09 * @param f the file to be interpreted%0A%09 * @param charset the name of the charset to be used in reading the file. %0A%09 * If the %0A%09 * @throws FileNotFoundException%0A%09 */%0A%09public SadmanParser(File f, String charset) throws FileNotFoundException%7B%0A%09%09this(() -%3E %7B%0A%09%09%09try%7B%0A%09%09%09%09return new Scanner(f, charset);%0A%09%09%09%7D catch(FileNotFoundException e)%7B%0A%09%09%09%09throw new IllegalArgumentException(e);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%09%0A%09public SadmanParser(File f) throws FileNotFoundException%7B%0A%09%09this(() -%3E %7B%0A%09%09%09try%7B%0A%09%09%09%09return new Scanner(f);%0A%09%09%09%7D catch(FileNotFoundException e)%7B%0A%09%09%09%09throw new IllegalArgumentException(e);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%09%0A%09private SadmanParser(Supplier%3CScanner%3E supplier) throws FileNotFoundException%7B%0A%09%09Scanner s = supplier.get(
 );%0A%09
@@ -2025,38 +2025,29 @@
 %0A%09%09%09s = 
-new Scanner(f, chars
+supplier.g
 et
+(
 );%0A%09%09%7D%0A%09

add constructor for system-default charset