package SnakePkg; import java.lang.Math; /** * A class to represent an Apple in the Board * @author Christos Choutouridis 8997 */ public class Apple { /** @name Constructors */ /** @{ */ /** Default ctor */ Apple () { appleId = appleTileId= 0; color = ""; points = 0; } /** Main ctor */ Apple (int appleId, int appleTileId, String color, int points) { this.appleId = appleId; this.appleTileId = appleTileId; this.color = color; this.points = points; } /** Copy constructor * @note We don't use clone as long as we don't inherit Cloneable iface */ Apple (Apple a) { appleId = a.getAppleId (); appleTileId = a.getAppleTileId (); color = a.getColor (); points = a.getPoints (); } /** @} */ /** @name Get/Set interface */ /** @{ */ int getAppleId () { return appleId; } void setAppleId (int appleId) { this.appleId = appleId; } int getAppleTileId () { return appleTileId; } void setAppleTileId (int appleTileId) { this.appleTileId = appleTileId; } String getColor () { return color; } /** * We set color and we update points sign. * @param color The desired color * @arg "red" Set color red and mark "points" positive * @arg "black" Set color black and mark "points" negative * @arg Otherwise zero */ void setColor (String color) { this.color = color; if (color == "red") points = Math.abs(points); else if (color == "black") points = -Math.abs(points); else points = 0; } int getPoints () { return points; } /** We set the points and update its sign based on color */ void setPoints (int points) { if (color == "red") this.points = Math.abs(points); else this.points = -Math.abs(points); } /** @} */ /** @name Data members (private) */ /** @{ */ private int appleId; //!< Apples's ID private int appleTileId; //!< Apple's tile location private String color; //!< Apple's color /**^ * @note * This way of representing color is not preferable by the author. * An enum Color { red, black } would be better and far less error prone * In order to support this style we have to strict the color names to * lower case letters. We also update the points sign to make thinks easier. * @see setColor */ private int points; //!< The points added (algebraically) to the user /** @} */ } //class BadColor extends Exception { // // default constructor // BadColor() { } // // parametrized constructor // BadColor (String str) { super(str); } //}