Sudoku Solver Scanner/2D arrays Help

Sudoku Sudoku using scanner and 2D arrays

Page 1 of 1

6 Replies - 8217 Views - Last Post: 16 February 2009 - 06:32 PM Rate Topic: -----

#1 JMChurch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-February 09

Sudoku Solver Scanner/2D arrays Help

Posted 15 February 2009 - 08:15 PM

Hello,
This is my first post on here so be gentle :) lol. Anyway, I'm currently working on a Sudoku Solver for my CS 2420 class and I am having some major blocks with it. My goal is to represent a Sudoku puzzle using a Sudoku class, have the user input a file and build the Sudoku object putting the data in to a 2D array, print it to the console, and solve it using a standard backtracking algorithm. Here is the current code I have for the SudokuApp:


public class SudokuApp
{
	public static void main (String[] args) throws IOException
	{
		Scanner glaDOS = new Scanner (System.in);
		System.out.print("PLEASE ENTER A VALID FILE NAME THAT CONTAINS A SUDOKU: ");
		File sudokuFile = new File (glaDOS.next());
		Scanner glaDOS2 = new Scanner (sudokuFile);
		Sudoku puzzle = new Sudoku (glaDOS2);
		System.out.println ("Unsolved: ");
		System.out.println (puzzle);
		System.out.println ("Solved: " );
		System.out.println (puzzle);
		System.out.println("Backtracking steps: ");
		
  
	}
		
	}



and my current code for the Sudoku class itself

public class Sudoku
{
	//instance variables
	private int row;
	private int col;
	private String numbers;
	private char [][] puzzle;
	/**
	 * 
	 * @param sudokuReader the scanner containing the puzzle
	 * file to be read and scanned into an array
	 * @throws IOException 
	 * 
	 */
	public Sudoku (Scanner sudokuReader) 
	{
		puzzle = new char [9][9];
		
		for (int i = 0; i < 8; i++)
		{
			if(sudokuReader.hasNext())
			{
				numbers = sudokuReader.next();
				for (int j = 0; j < 8; j++)
				{
					puzzle[row][col] = numbers.charAt(j);
				}
			}
			
		}
		System.out.println(puzzle[9][9]);
	}
	/**
	 * Method that checks to see if a number can be put in to
	 * a specific position on the Sudoku board
	 * @param row row position
	 * @param col column position
	 * @param num number being analyzed
	 * @return value stating if the move is legal
	 */
	public boolean isLegal (int row, int col, int num)
	{
		return true;
	   
	}
	
	/**
	 * Methods that uses a backtracking algorithm to put
	 * numbers into positions on the puzzle and tries to
	 * solve it. If there is a solution, it returns true.
	 * If there is no solution, it returns false.
	 * @param row row position to start solving from
	 * @param col column position to start solving from
	 * @return value stating if puzzle can be solved
	 */
	public boolean solve (int row, int col, Sudoku puzzle)
	{
		// if(row++ == 8)
			//return true;
		//if (numbers.charAt(row, col) == '.')
			//return(solve(row++, col++, puzzle));
		//for(int = 1; i < 10; i++)
		//{
			//if(IsLegal(row, col, num))
			//if(solve(row, col++, puzzle))
			//  return true;
			
		//else
			return false;
		
			
	}
	
	/**
	 * Returns a string representing the contents of the
	 * sudoku.
	 * @return
	 */
	public String ToString()
	{
		return "Unsolved: " + puzzle;
		
	}

}




I am getting all sorts of weird errors right now. One of them I know is something to do with Eclipse which I am working on. The other one I get is:
StringOutOfBoundsException:
String index out of range: 1 (in java.lang.string)
whenever I run the code in BlueJ. I know it has something to do with how the data is being read in to the array but I'm not exactly sure what is...Also for the solve and IsLegal methods, I'm not sure whether I should have the Sudoku puzzle itself be a parameter or have the 2-D array being used be the parameter or what.... If anyone could please give me a little bit of direction and help with the troubles I'm having, I would greatly appreciate it. Thank you to all

Is This A Good Question/Topic? 0
  • +

Replies To: Sudoku Solver Scanner/2D arrays Help

#2 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Sudoku Solver Scanner/2D arrays Help

Posted 15 February 2009 - 08:27 PM

Some things that would be useful to know would be your input, and where it is saying the error is at. I'm going to guess it's saying the error is here:

puzzle[row][col] = numbers.charAt(j);


Also, did you set the ints row and col to anything? I don't see that anywhere. Seems like nothing is being set.

This post has been edited by BlakeJustBlake: 15 February 2009 - 08:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Sudoku Solver Scanner/2D arrays Help

Posted 15 February 2009 - 08:29 PM

i just compiled your code, and there were no errors
Was This Post Helpful? 1
  • +
  • -

#4 JMChurch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-February 09

Re: Sudoku Solver Scanner/2D arrays Help

Posted 15 February 2009 - 08:42 PM

Yes, that is exactly where the error is at actually and the input is a txt puzzle file that looks like this:

6 8 . . . . . 5 .
. . . . . 5 . . .
. . 3 8 . . 2 6 .

1 . 7 . 2 . . . .
. . 9 5 . 8 6 . .
. . . . 1 . 7 . 2

. 2 1 . . 9 4 . .
. . . 4 . . . . .
. 3 . . . . . 2 8

which the Sudoku app scans and uses in the Sudoku constructor.
And I have changed the code so that it uses the row and col variables like so:

 public Sudoku (Scanner sudokuReader) 
	{
		puzzle = new char [9][9];
		
		for (row = 0; row < 8; row++)
		{
			if(sudokuReader.hasNext())
			{
				numbers = sudokuReader.next();
				for (col = 0; col < 8; col++)
				{
					puzzle[row][col] = numbers.charAt(col);
				}
			}
			
		}
		System.out.println(puzzle[9][9]);
	}




But I am still getting the same error unfortunately......
Was This Post Helpful? 0
  • +
  • -

#5 JMChurch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-February 09

Re: Sudoku Solver Scanner/2D arrays Help

Posted 15 February 2009 - 08:53 PM

OK, it's weird, I tried retooling the Sudoku constructor a little bit and I'm still getting an error, but I think it's a lot of progress from where I started:

 public Sudoku (Scanner sudokuReader) 
	{
		puzzle = new char [9][9];
		
		for (row = 0; row < 8; row++)
		{
			for (col = 0; col < 8; col++)
			{
				if(sudokuReader.hasNext())
				{
					number = (char)sudokuReader.nextInt();
					puzzle[row][col] = number;
					col++;
				}
				
			}
			
		}
		System.out.println(puzzle[9][9]);
	}



So now I'm getting a different error....
InputMismatchException:
null (in java.util.Scanner)
which is progress I'm sure, but I'm still not quite sure where to go from here or what the error is (the main goal being to print out the puzzle itself to the console like in my previous post....

This post has been edited by JMChurch: 15 February 2009 - 09:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 BlakeJustBlake  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 26
  • View blog
  • Posts: 441
  • Joined: 15-February 09

Re: Sudoku Solver Scanner/2D arrays Help

Posted 15 February 2009 - 09:19 PM

If your input looks like that in a text file then try getting it to output the String numbers for some debugging, because whenever the scanner gets the next in a file I think it uses whitespace as a delimiter for finding the next string, so it might just be getting those single characters and you're trying to iterate over 9 of them.

This post has been edited by BlakeJustBlake: 15 February 2009 - 09:20 PM

Was This Post Helpful? 1
  • +
  • -

#7 JMChurch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 15-February 09

Re: Sudoku Solver Scanner/2D arrays Help

Posted 16 February 2009 - 06:32 PM

Ok, I've made a lot of progress since I last posted. I am at least printing out an array, but it is an array of zeroes which is not what I want obviously compared to the input. Currently the output I'm getting looks like this:

PLEASE ENTER A VALID FILE NAME THAT CONTAINS A SUDOKU: puzzle1.txt
000000000
000000000
000000000
000000000
000000000
000000000
000000000
000000000
000000000
Unsolved:
assignment03.Sudoku@10b30a7
Solved:
assignment03.Sudoku@10b30a7
Backtracking steps:

Here is the code I have right now:

public class Sudoku
{
	//instance variables
	private int row;
	private int col;
	private int number;
	private int [][] puzzle;
	private long steps;
	/**
	 * 
	 * @param sudokuReader the scanner containing the puzzle
	 * file to be read and scanned into an array
	 * @throws IOException 
	 * 
	 */
	public Sudoku (File sudokuFile) 
	{
		try
		{
		Scanner sudokuReader = new Scanner(sudokuFile);
		puzzle = new int [9][9];
		
		for (row = 0; row < 8; row++)
		{
			for (col = 0; col < 8; col++)
			{
				if(sudokuReader.hasNext())
				{
					if(sudokuReader.hasNextInt())
					{
						number = sudokuReader.nextInt();
						puzzle[row][col] = number;
					}
				}
			}
		 }
		 for(int row = 0; row < puzzle.length; row++)
		 {
			 for(int col = 0; col < puzzle.length; col++)
			 {
				 System.out.print(puzzle[row][col]);
			 }
			 System.out.println();
		 }
		 }
		catch (IOException e)
		{
		System.out.println("Cannot find file");
		}
   }
	/**
	 * Method that checks to see if a number can be put in to
	 * a specific position on the Sudoku board
	 * @param row row position
	 * @param col column position
	 * @param num number being analyzed
	 * @return value stating if the move is legal
	 */
	public boolean isLegal (int row, int col, int num)
	{
		for(int i = 0; i < 9; i++)//checks values row-wise
			if (num == puzzle[i][col])
				return false;
		
		for(int j = 0; j < 9; j++)//checks values column-wise
			if (num == puzzle[row][j])
				return false;
				
		//for(int k = 0; k < 9; k++)
			
		 
		 return true; //no violations, so it's a legal move
	}
	
	/**
	 * Methods that uses a backtracking algorithm to put
	 * numbers into positions on the puzzle and tries to
	 * solve it. If there is a solution, it returns true.
	 * If there is no solution, it returns false.
	 * @param row row position to start solving from
	 * @param col column position to start solving from
	 * @return value boolean stating if puzzle can be solved
	 */
	public boolean solve (int row, int col)
	{
		if(++row == 8)
			return true;
		if (puzzle[row][col] == '.')
			return(solve(row++, col++));
		for(int num = 1; num <= 9; ++num)
		{
			if(isLegal(row, col, num))
			{
				puzzle[row][col] = num;
				steps++;
				if(solve(row, col++))
					return true;
			}
		}   
			return false;
		
			
	}
	
	/**
	 * Returns a string representing the contents of the
	 * sudoku.
	 * @return string representation of the puzzle
	 */
	public String ToString()
	{
		return "Unsolved: " + puzzle;
		
	}

}



I did modify my constructor a bit to take in the file as a parameter instead of the scanner on a suggestion from a friend, but this shouldn't be a problem in this case. The main question I have now is why is it only printing zeroes here for the puzzle array in this case....

(Also, I think I have the solve/isLegal methods finished although I'm still working on checking 3x3 squares)

This post has been edited by JMChurch: 16 February 2009 - 06:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1