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.

89 lines
2.8 KiB

  1. package SnakePkg;
  2. import java.lang.Math;
  3. /**
  4. * A class to represent an Apple in the Board
  5. * @author Christos Choutouridis 8997
  6. */
  7. public class Apple {
  8. /** @name Constructors */
  9. /** @{ */
  10. /** Default ctor */
  11. Apple () {
  12. appleId = appleTileId= 0;
  13. color = "";
  14. points = 0;
  15. }
  16. /** Main ctor */
  17. Apple (int appleId, int appleTileId, String color, int points) {
  18. this.appleId = appleId;
  19. this.appleTileId = appleTileId;
  20. this.color = color;
  21. this.points = points;
  22. }
  23. /** Copy constructor
  24. * @note We don't use clone as long as we don't inherit Cloneable iface
  25. */
  26. Apple (Apple a) {
  27. appleId = a.getAppleId ();
  28. appleTileId = a.getAppleTileId ();
  29. color = a.getColor ();
  30. points = a.getPoints ();
  31. }
  32. /** @} */
  33. /** @name Get/Set interface */
  34. /** @{ */
  35. int getAppleId () { return appleId; }
  36. void setAppleId (int appleId) { this.appleId = appleId; }
  37. int getAppleTileId () { return appleTileId; }
  38. void setAppleTileId (int appleTileId) { this.appleTileId = appleTileId; }
  39. String getColor () { return color; }
  40. /**
  41. * We set color and we update points sign.
  42. * @param color The desired color
  43. * @arg "red" Set color red and mark "points" positive
  44. * @arg "black" Set color black and mark "points" negative
  45. * @arg Otherwise zero
  46. */
  47. void setColor (String color) {
  48. this.color = color;
  49. if (color == "red") points = Math.abs(points);
  50. else if (color == "black") points = -Math.abs(points);
  51. else points = 0;
  52. }
  53. int getPoints () { return points; }
  54. /** We set the points and update its sign based on color */
  55. void setPoints (int points) {
  56. if (color == "red") this.points = Math.abs(points);
  57. else this.points = -Math.abs(points);
  58. }
  59. /** @} */
  60. /** @name Data members (private) */
  61. /** @{ */
  62. private int appleId; //!< Apples's ID
  63. private int appleTileId; //!< Apple's tile location
  64. private String color; //!< Apple's color
  65. /**^
  66. * @note
  67. * This way of representing color is not preferable by the author.
  68. * An enum Color { red, black } would be better and far less error prone
  69. * In order to support this style we have to strict the color names to
  70. * lower case letters. We also update the points sign to make thinks easier.
  71. * @see setColor
  72. */
  73. private int points; //!< The points added (algebraically) to the user
  74. /** @} */
  75. }
  76. //class BadColor extends Exception {
  77. // // default constructor
  78. // BadColor() { }
  79. // // parametrized constructor
  80. // BadColor (String str) { super(str); }
  81. //}