Advertisement
Guest User

project

a guest
Sep 26th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Iterator;
  3. import java.io.*;
  4.  
  5. public class Kenken implements Iterable<Cell []>
  6. {
  7. private final int SIZE;
  8. private Cell [][] puzzle;
  9. private Constraint [] constraints;
  10.  
  11. /** Puzzle File Format:
  12. *
  13. * ASCII text file with following data in each line:
  14. * 4 8 // size and number of constraints
  15. * // constraints
  16. * 4 + 2 0 0 0 1
  17. * 2 / 2 0 2 0 3
  18. * 1 - 2 1 0 1 1
  19. * 3 - 2 1 2 1 3
  20. * 5 + 2 2 0 3 0
  21. * 6 * 2 2 1 2 2
  22. * 2 / 2 3 1 3 2
  23. * 2 - 2 2 3 3 3
  24. * // solution
  25. * 3 1 4 2
  26. * 2 3 1 4
  27. * 4 2 3 1
  28. * 1 4 2 3
  29. *
  30. */
  31.  
  32. public Kenken(Scanner file)
  33. {
  34. // read puzzle to be solved from a file Scanner
  35. Scanner line = new Scanner(file.nextLine()); // get size - first token on line 1
  36. SIZE = line.nextInt();
  37. puzzle = new Cell [SIZE][SIZE]; // setup puzzle based on the SIZE
  38. for (int i = 0; i < puzzle.length; i++)
  39. for (int j = 0; j < puzzle.length; j++)
  40. puzzle[i][j] = new Cell();
  41.  
  42. constraints = new Constraint[line.nextInt()]; // get number of constraints - second token on line 1
  43. file.nextLine(); // get rid of comment line in file format
  44.  
  45. int index = 0;
  46. for (int i = 0; i < constraints.length; i++) // process each constraint in the file
  47. {
  48. String s = file.nextLine();
  49. Scanner constraint = new Scanner(s); // one constraint per line
  50. int value = constraint.nextInt(); // the value
  51. char op = constraint.next().charAt(0); // the operator
  52. if (op == 's') // space substitution char is 's'
  53. op = ' ';
  54. int cells = constraint.nextInt(); // the number of Cells in the cage
  55. Cell [] cage = new Cell[cells];
  56. for (int j = 0; j < cells; j++) { // the pairs of ints for the Cell indices in puzzle
  57. int a = constraint.nextInt();
  58. int b = constraint.nextInt();
  59. cage[j] = puzzle[a][b];
  60. }
  61.  
  62. constraints[index++] = new Constraint(value, op, cage); // create the constraint from input data
  63. }
  64.  
  65. // fill in puzzle array with solution to be tested
  66. file.nextLine(); // get rid of comment line in file format
  67.  
  68. for (int i = 0; i < puzzle.length; i++) {
  69. for (int j = 0; j < puzzle.length; j++) {
  70. puzzle[i][j].setValue(file.nextInt());
  71. }
  72. }
  73. }
  74.  
  75. public String toString()
  76. {
  77. String s = "";
  78. for (int i = 0; i < puzzle.length; i++)
  79. for (int j = 0; j < puzzle.length; j++)
  80. s += puzzle[i][j] + ((j != puzzle.length - 1) ? ", " : "\n");
  81. return s;
  82. }
  83.  
  84. public class KenkenIterator implements Iterator<Cell []>
  85. {
  86. private Cell [][] puzzle;
  87. private int cursor;
  88. private int cursorConstraint;
  89.  
  90. public KenkenIterator()
  91. {
  92. cursor = cursorConstraint = 0;
  93. }
  94.  
  95. public boolean hasNext()
  96. {
  97. return cursor < puzzle.length;
  98. }
  99.  
  100. public Cell[] next()
  101. {
  102. Cell [] value = puzzle[cursor];
  103. cursor ++;
  104. return value;
  105. }
  106.  
  107. public void remove()
  108. {
  109. }
  110. }
  111.  
  112. public KenkenIterator iterator()
  113. {
  114. KenkenIterator temp;
  115. return temp;
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement