A java PacMan game application for A.U.TH (data structures class)
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

125 linhas
2.9 KiB

  1. /**
  2. * @file Queue2D.java
  3. * @brief
  4. * File containing the Queue2D class. A queue for x,y coordinate
  5. * pairs
  6. *
  7. * @author Christos Choutouridis 8997 cchoutou@ece.auth.gr
  8. * @author Konstantina Tsechelidou 8445 konstsec@ece.auth.gr
  9. */
  10. package gr.auth.ee.dsproject.node;
  11. /**
  12. * @brief
  13. * A queue implementation for (x, y) coordinates
  14. */
  15. public class Queue2D
  16. {
  17. int [][] q; //!< The queue array
  18. //int f;
  19. int r; // Rear pointer
  20. int size, nItem; // Queue helper size and queued items
  21. /*
  22. * ============ Constructors ==============
  23. */
  24. /**
  25. * @brief
  26. * Simple constructor. Initialize all to zero
  27. * @note
  28. * As long as there is no Setter for buffer and read pointer
  29. * this constructor is useless.
  30. */
  31. public Queue2D () {
  32. //f = 0;
  33. r = 0;
  34. size = nItem = 0;
  35. q = null;
  36. }
  37. /**
  38. * @brief
  39. * Constructor with buffer size
  40. * @param size The size of buffer to allocate
  41. */
  42. public Queue2D (int size) {
  43. //f = 0;
  44. r = -1; // Init the rear value
  45. nItem = 0;
  46. this.size = size;
  47. q = new int[size][2];
  48. }
  49. /**
  50. * @return The "waiting" items in queue
  51. */
  52. public int size () { return nItem; }
  53. /**
  54. * @return True if queue is empty
  55. */
  56. public boolean isEmpty () { return (nItem == 0) ? true : false; }
  57. /**
  58. * @return True if queue is full
  59. */
  60. public boolean isFull () { return (nItem >= size) ? true : false; }
  61. /**
  62. * @return
  63. * The first item waiting in queue, without removing it
  64. * If the queue is empty return {-1, -1}
  65. */
  66. public int [] peek () {
  67. int [] none = {-1, -1};
  68. if (!isEmpty ()) {
  69. return q[0]; //return q[f];
  70. }
  71. else
  72. return none;
  73. }
  74. /**
  75. * @return
  76. * The first item waiting in queue and removes it
  77. * If the queue is empty return {-1, -1}
  78. */
  79. public int [] remove () {
  80. int [] ret = {-1, -1};
  81. if (!isEmpty ()) {
  82. ret[0] = q[0][0]; //ret[0] = q[f][0];
  83. ret[1] = q[0][1]; //ret[1] = q[f][1];
  84. // shift the buffered items
  85. for (int i=0 ; i<r ; ++i) {
  86. q[i][0] = q[i+1][0];
  87. q[i][1] = q[i+1][1];
  88. }
  89. //++f;
  90. --r;
  91. --nItem;
  92. return ret;
  93. }
  94. else
  95. return ret;
  96. }
  97. /**
  98. * @brief
  99. * Insert an item to the queue
  100. * @param it Reference to item to insert
  101. * @return The inserted item
  102. * If the queue is full return {-1, -1}
  103. */
  104. public int [] insert (int [] it) {
  105. int [] none = {-1, -1};
  106. if (!isFull ()) {
  107. ++r;
  108. q[r][0] = it[0];
  109. q[r][1] = it[1];
  110. ++nItem;
  111. return it;
  112. }
  113. else
  114. return none;
  115. }
  116. }