Snake/src/SnakePkg/Apple.java
Christos Houtouridis 384481b698 An Apple class rework
2018-11-06 10:34:00 +02:00

103 lines
3.4 KiB
Java
Executable File

package SnakePkg;
import java.lang.Math;
/**
* @class Apple
* @brief Represent an Apple in the Board.
*
* Apples are part of the elements we place on the board.
* Each apple have a point contribution to the user and can be eaten once
* The point system is algebraic in order to simplify calculation.
*
* @author Christos Choutouridis 8997
* @email cchoutou@ece.auth.gr
*/
public class Apple {
/** @name Constructors */
/** @{ */
/**
* A Default, doing nothing constructor
* @note We don't use this constructor
*/
Apple () {
appleId = appleTileId= 0;
color = "";
points = 0;
}
/**
* The main constructor
* @param appleId The id of the apple
* @param appleTileId The tile where the apples is placed
* @param color The color of the apple
* @param points The points the apple is adding or subtracking from user
*/
Apple (int appleId, int appleTileId, String color, int points) {
this.appleId = appleId;
this.appleTileId = appleTileId;
this.color = color;
this.points = points;
}
/** Copy constructor
* @param a The apple we want to copy
*/
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.toLowerCase();
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 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 @ref author
* An <pre>
* enum Color {
* red, black
* }</pre>
* would be better and far less error prone.
* In order to support this style of color encoding we have to strict the
* color names to lower case letters and also convert user input.
* We also update the points sign to make calculations easier.
* @see setColor
*/
private int points; /**< The points added (algebraically) to the user */
/** @} */
}