Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package signpost;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import static java.lang.Math.max;
  7.  
  8. /** An (X, Y) position on a Signpost puzzle board. We require that
  9. * X, Y >= 0. Each Place object is unique; no other has the same x and y
  10. * values. As a result, "==" may be used for comparisons.
  11. * @author
  12. */
  13. class Place {
  14.  
  15. /** Convenience list-of-Place class. (Defining this allows one to create
  16. * arrays of lists without compiler warnings.) */
  17. static class PlaceList extends ArrayList<Place> {
  18. /** Initialize empty PlaceList. */
  19. PlaceList() {
  20. }
  21.  
  22. /** Initialze PlaceList from a copy of INIT. */
  23. PlaceList(List<Place> init) {
  24. super(init);
  25. }
  26. }
  27.  
  28. /** The position (X0, Y0), where X0, Y0 >= 0. */
  29. private Place(int x0, int y0) {
  30. x = x0; y = y0;
  31. }
  32.  
  33. /** Return the position (X, Y). This is a factory method that
  34. * creates a new Place only if needed by caching those that are
  35. * created. */
  36. static Place pl(int x, int y) {
  37. assert x >= 0 && y >= 0;
  38. int s = max(x, y);
  39. if (s >= _places.length) {
  40. Place[][] newPlaces = new Place[s + 1][s + 1];
  41. for (int i = 0; i < _places.length; i += 1) {
  42. System.arraycopy(_places[i], 0, newPlaces[i], 0,
  43. _places.length);
  44. }
  45. _places = newPlaces;
  46. }
  47. if (_places[x][y] == null) {
  48. _places[x][y] = new Place(x, y);
  49. }
  50. return _places[x][y];
  51. }
  52.  
  53. /** Returns the direction from (X0, Y0) to (X1, Y1), if we are a queen
  54. * move apart. If not, returns 0. The direction returned (if not 0)
  55. * will be an integer 1 <= dir <= 8 corresponding to the definitions
  56. * in Model.java */
  57. static int dirOf(int x0, int y0, int x1, int y1) {
  58. int dx = x1 < x0 ? -1 : x0 == x1 ? 0 : 1;
  59. int dy = y1 < y0 ? -1 : y0 == y1 ? 0 : 1;
  60. if (dx == 0 && dy == 0) {
  61. return 0;
  62. }
  63. if (dx != 0 && dy != 0 && Math.abs(x0 - x1) != Math.abs(y0 - y1)) {
  64. return 0;
  65. }
  66.  
  67. return dx > 0 ? 2 - dy : dx == 0 ? 6 + 2 * dy : 6 + dy;
  68. }
  69.  
  70. /** Returns the direction from me to PLACE, if we are a queen
  71. * move apart. If not, returns 0. */
  72. int dirOf(Place place) {
  73. if place.last {
  74. return dirOf(x, y, place.x, place.y);
  75. }
  76. else {
  77. return 0}
  78. }
  79. }
  80.  
  81. /** If (x1, y1) is the adjacent square in direction DIR from me, returns
  82. * x1 - x. */
  83. static int dx(int dir) {
  84. return DX[dir];
  85. }
  86.  
  87. /** If (x1, y1) is the adjacent square in direction DIR from me, returns
  88. * y1 - y. */
  89. static int dy(int dir) {
  90. return DY[dir];
  91. }
  92.  
  93. /** Return an array, M, such that M[x][y][dir] is a list of Places that are
  94. * one queen move away from square (x, y) in direction dir on a
  95. * WIDTH x HEIGHT board. Additionally, M[x][y][0] is a list of all Places
  96. * that are a queen move away from (x, y) in any direction (the union of
  97. * the lists of queen moves in directions 1-8). */
  98. static PlaceList[][][] successorCells(int width, int height) {
  99. PlaceList[][][] M = new PlaceList[width][height][9];
  100. int lim = Math.max(width, height);
  101. // FIXME
  102. return M;
  103. }
  104.  
  105. @Override
  106. public boolean equals(Object obj) {
  107. if (!(obj instanceof Place)) {
  108. return false;
  109. }
  110. Place other = (Place) obj;
  111. return x == other.x && y == other.y;
  112. }
  113.  
  114. @Override
  115. public int hashCode() {
  116. return (x << 16) + y;
  117. }
  118.  
  119. @Override
  120. public String toString() {
  121. return String.format("(%d, %d)", x, y);
  122. }
  123.  
  124. /** X displacement of adjacent squares, indexed by direction. */
  125. static final int[] DX = { 0, 1, 1, 1, 0, -1, -1, -1, 0 };
  126.  
  127. /** Y displacement of adjacent squares, indexed by direction. */
  128. static final int[] DY = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
  129.  
  130. /** Coordinates of this Place. */
  131. protected final int x, y;
  132.  
  133. /** Places already generated. */
  134. private static Place[][] _places = new Place[10][10];
  135.  
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement