Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. import java.util.Scanner;
  2. /** A class whose instances represent fully or partially filled
  3. * Sudoku boards. The board cells are implemented by the elements
  4. * of a publically accessible two dimensional Java array named board
  5. * of ints.
  6.  
  7. */
  8. public class Sudoku {
  9. /**
  10. * Within this two dimensional array that stores the contents of
  11. * the board's cells:
  12. * The first subscript locates which COLUMN contains the element.
  13. * So, it is like an x coordinate.
  14. * The the second subscript locates which ROW contains the element.
  15. * So, it is like a y coordinate.
  16. * field board
  17. */
  18. public int board[][];
  19. /**
  20. * Constructor to build an "empty" board.
  21. * "Empty" or "blank" cells are represented by 0s.
  22. */
  23. public Sudoku()
  24. {
  25. board = new int[9][9];
  26. }
  27. /**
  28. * Method to translate the board contents into a printable String.
  29. * When the returned String is printed, the board is shown with
  30. * extra spaces separating the 3x3 blocks, for example, <br/>
  31. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  32. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  33. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  34. * <br/>
  35. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  36. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  37. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  38. * <br/>
  39. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  40. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  41. * 1 2 3 &nbsp; 4 5 6 &nbsp; 7 8 9 <br/>
  42. *
  43. * Note: The Java API provides that when System.out.print
  44. * or System.out.println is called ON a Sudoku object, the
  45. * toString method is called and the String so returned is printed.
  46. * @return The Sudoku board contents in string form.
  47. */
  48. public String toString()
  49. {
  50. String result = "";
  51. for( int rowOfBlocks = 0; rowOfBlocks < 3; rowOfBlocks = rowOfBlocks + 1 )
  52. {
  53. for( int rowInABlock = 0; rowInABlock < 3; rowInABlock = rowInABlock + 1 )
  54. {
  55. for( int colOfBlocks = 0; colOfBlocks < 3; colOfBlocks = colOfBlocks + 1 )
  56. {
  57. for( int colInABlock = 0; colInABlock < 3; colInABlock = colInABlock + 1 )
  58. {
  59. result = result + board[colOfBlocks*3 + colInABlock][rowOfBlocks*3 + rowInABlock] + " ";
  60. }
  61. result = result + " ";
  62. }
  63. result = result + "\n";
  64. }
  65. result = result + "\n";
  66. }
  67. return result;
  68. }
  69. /**
  70. * Method to read 81 ints that should each be in the 1-9
  71. * range and store them into this Sudoko board.
  72. * Note: The method will operate correctly to read data that
  73. * had been returned by our toString() method.
  74. *
  75. * @param scParam The Scanner object to use for reading.
  76. */
  77. public void read( Scanner scParam )
  78. {
  79. for( int rowOfBlocks = 0; rowOfBlocks < 3; rowOfBlocks = rowOfBlocks + 1 )
  80. {
  81. for( int rowInABlock = 0; rowInABlock < 3; rowInABlock = rowInABlock + 1 )
  82. {
  83. for( int colOfBlocks = 0; colOfBlocks < 3; colOfBlocks = colOfBlocks + 1 )
  84. {
  85. for( int colInABlock = 0; colInABlock < 3; colInABlock = colInABlock + 1 )
  86. {
  87. board[colOfBlocks*3 + colInABlock][rowOfBlocks*3 + rowInABlock] = scParam.nextInt();
  88. }
  89. }
  90. }
  91. return ;
  92. }
  93.  
  94. }
  95. public static void main(String[]a)
  96. {
  97. System.out.println("Sudoku should not be run as an app.");
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement