A java snake game for A.U.TH. Data structures class
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.

103 lines
3.4 KiB

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