A java snake game for A.U.TH. Data structures class
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

101 lignes
3.4 KiB

  1. package net.hoo2.auth.dsproject.snake;
  2. /**
  3. * @class Apple
  4. * @brief Represent an Apple in the Board.
  5. *
  6. * Apples are part of the elements we place on the board.
  7. * Each apple have a point contribution to the user and can be eaten once
  8. * The point system is algebraic in order to simplify calculation.
  9. *
  10. * @author Christos Choutouridis AEM:8997
  11. * @email cchoutou@ece.auth.gr
  12. */
  13. public class Apple {
  14. /** @name Constructors */
  15. /** @{ */
  16. /**
  17. * A Default, doing nothing constructor
  18. * @note We don't use this constructor
  19. */
  20. Apple () {
  21. appleId = appleTileId= 0;
  22. color = "";
  23. points = 0;
  24. }
  25. /**
  26. * The main constructor
  27. * @param appleId The id of the apple
  28. * @param appleTileId The tile where the apples is placed
  29. * @param color The color of the apple
  30. * @param points The points the apple is adding or subtracking from user
  31. */
  32. Apple (int appleId, int appleTileId, String color, int points) {
  33. this.appleId = appleId;
  34. this.appleTileId = appleTileId;
  35. this.color = new String(color);
  36. this.points = points;
  37. }
  38. /** Copy constructor
  39. * @param a The apple we want to copy
  40. */
  41. Apple (Apple a) {
  42. appleId = a.getAppleId ();
  43. appleTileId = a.getAppleTileId ();
  44. color = new String (a.getColor ());
  45. points = a.getPoints ();
  46. }
  47. /** @} */
  48. /** @name Get/Set interface */
  49. /** @{ */
  50. int getAppleId () { return appleId; }
  51. void setAppleId (int appleId) { this.appleId = appleId; }
  52. int getAppleTileId () { return appleTileId; }
  53. void setAppleTileId (int appleTileId) { this.appleTileId = appleTileId; }
  54. String getColor () { return color; }
  55. /**
  56. * We set color and we update points sign.
  57. * @param color The desired color
  58. * @arg "red" Set color red and mark "points" positive
  59. * @arg "black" Set color black and mark "points" negative
  60. * @arg Otherwise zero
  61. */
  62. void setColor (String color) {
  63. this.color = color.toLowerCase();
  64. if (color == "red") points = Math.abs(points);
  65. else if (color == "black") points = -Math.abs(points);
  66. else points = 0;
  67. }
  68. int getPoints () { return points; }
  69. /** We set the points and update its sign based on color */
  70. void setPoints (int points) {
  71. if (color == "red") this.points = Math.abs(points);
  72. else this.points = -Math.abs(points);
  73. }
  74. /** @} */
  75. /** @name Data members */
  76. /** @{ */
  77. private int appleId; /**< Apples's ID */
  78. private int appleTileId; /**< Apple's tile location */
  79. private String color;
  80. /**< Apple's color
  81. * @note
  82. * This way of representing color is not preferable by the @ref author
  83. * An <pre>
  84. * enum Color {
  85. * red, black
  86. * }</pre>
  87. * would be better and far less error prone.
  88. * In order to support this style of color encoding we have to strict the
  89. * color names to lower case letters and also "low case-convert" the user input.
  90. * We also update the points sign to make calculations easier.
  91. * @see setColor
  92. */
  93. private int points; /**< The points added (algebraically) to the user */
  94. /** @} */
  95. }