Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- /** A class whose instances represent fully or partially filled
- * Sudoku boards. The board cells are implemented by the elements
- * of a publically accessible two dimensional Java array named board
- * of ints.
- */
- public class Sudoku {
- /**
- * Within this two dimensional array that stores the contents of
- * the board's cells:
- * The first subscript locates which COLUMN contains the element.
- * So, it is like an x coordinate.
- * The the second subscript locates which ROW contains the element.
- * So, it is like a y coordinate.
- * field board
- */
- public int board[][];
- /**
- * Constructor to build an "empty" board.
- * "Empty" or "blank" cells are represented by 0s.
- */
- public Sudoku()
- {
- board = new int[9][9];
- }
- /**
- * Method to translate the board contents into a printable String.
- * When the returned String is printed, the board is shown with
- * extra spaces separating the 3x3 blocks, for example, <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- * 1 2 3 4 5 6 7 8 9 <br/>
- *
- * Note: The Java API provides that when System.out.print
- * or System.out.println is called ON a Sudoku object, the
- * toString method is called and the String so returned is printed.
- * @return The Sudoku board contents in string form.
- */
- 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;
- }
- /**
- * Method to read 81 ints that should each be in the 1-9
- * range and store them into this Sudoko board.
- * Note: The method will operate correctly to read data that
- * had been returned by our toString() method.
- *
- * @param scParam The Scanner object to use for reading.
- */
- 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();
- }
- }
- }
- return ;
- }
- }
- public static void main(String[]a)
- {
- System.out.println("Sudoku should not be run as an app.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement