You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
3.8 KiB

  1. /**
  2. * @file Common.java
  3. *
  4. * @author Christos Choutouridis AEM:8997
  5. * @email cchoutou@ece.auth.gr
  6. */
  7. package net.hoo2.auth.labyrinth;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. class Const {
  11. static final int noSupply =-1;
  12. static final int noTileId =-1;
  13. }
  14. /**
  15. * Application wide object to hold settings like values for the session.
  16. */
  17. class Session {
  18. static int boardSize = 15; /**@< Default board's size (if no one set it via command line) */
  19. }
  20. /**
  21. * Helper C++-like enumerator class to hold direction
  22. */
  23. class Direction {
  24. static final int UP =1; /**< North direction */
  25. static final int RIGHT =3; /**< East direction */
  26. static final int DOWN =5; /**< South direction */
  27. static final int LEFT =7; /**< West direction */
  28. static final int Begin =1;
  29. static final int End =8;
  30. static final int Step =2;
  31. }
  32. /**
  33. * @brief
  34. * An Application wide board position implementation holding just the id coordinate.
  35. *
  36. * Position is a helper class to enable us cope with the redundant position data (id and coordinates).
  37. * This class provide both static conversion functionalities between id and coordinates
  38. * and data representation in the coordinates system.
  39. * For clarity we adopt a row-column naming convention.
  40. */
  41. class Position {
  42. /**
  43. * Basic constructor from col-row coordinates
  44. * @param row The row coordinate
  45. * @param col The column coordinate
  46. */
  47. protected Position(int row, int col) {
  48. this.id = toID(row, col);
  49. }
  50. /**
  51. * Basic constructor from Id
  52. * @param tileId The id of tile
  53. */
  54. protected Position(int tileId) {
  55. this.id = tileId;
  56. }
  57. /** @name non-static API */
  58. /** @{ */
  59. protected int getRow() { return toRow(id); } /**< Read access to virtual row coordinate */
  60. protected int getCol() { return toCol(id); } /**< Read access to virtual column coordinate */
  61. protected int getId() { return id; } /**< Read access to id coordinate */
  62. /** @} */
  63. /** @name Static convention utilities */
  64. /** @{ */
  65. /**
  66. * Takes row and column coordinates and return the calculated Id coordinate
  67. * @param row The row coordinate
  68. * @param col The column coordinate
  69. * @return The converted value
  70. */
  71. protected static int toID(int row, int col) {
  72. return row * Session.boardSize + col;
  73. }
  74. /**
  75. * Takes Id coordinate and return the corresponding row coordinate
  76. * @param id The id coordinate
  77. * @return The row coordinate
  78. */
  79. protected static int toRow(int id){
  80. return id / Session.boardSize;
  81. }
  82. /**
  83. * Takes Id coordinate and return the corresponding column coordinate
  84. * @param id The id coordinate
  85. * @return The column coordinate
  86. */
  87. protected static int toCol(int id) {
  88. return id % Session.boardSize;
  89. }
  90. /** @} */
  91. /** @name private data types */
  92. /** @{ */
  93. private int id; /**< The id coordinate of the constructed Position object */
  94. /** @} */
  95. }
  96. class ShuffledRange {
  97. ShuffledRange() {
  98. numbers = new ArrayList<Integer>();
  99. }
  100. ShuffledRange(int begin, int end) {
  101. numbers = new ArrayList<Integer>();
  102. init (begin, end);
  103. }
  104. ShuffledRange(int begin, int step, int end) {
  105. numbers = new ArrayList<Integer>();
  106. init (begin, step, end);
  107. }
  108. void init (int begin, int end) {
  109. numbers.clear();
  110. for (int i=begin ; i<end ; ++i)
  111. numbers.add(i);
  112. Collections.shuffle(numbers);
  113. }
  114. void init (int begin, int step, int end) {
  115. numbers.clear();
  116. for (int i=begin ; i<end ; i+=step)
  117. numbers.add(i);
  118. Collections.shuffle(numbers);
  119. }
  120. int get () {
  121. try {
  122. return numbers.remove(0);
  123. }
  124. catch (IndexOutOfBoundsException e) {
  125. return Const.noTileId;
  126. }
  127. }
  128. private ArrayList<Integer> numbers;
  129. }