Browse Source

An Apple class rework

tags/v1.0
Christos Houtouridis 6 years ago
parent
commit
384481b698
1 changed files with 39 additions and 25 deletions
  1. +39
    -25
      src/SnakePkg/Apple.java

+ 39
- 25
src/SnakePkg/Apple.java View File

@@ -3,19 +3,35 @@ package SnakePkg;
import java.lang.Math; import java.lang.Math;
/** /**
* A class to represent an Apple in the Board
* @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 * @author Christos Choutouridis 8997
* @email cchoutou@ece.auth.gr
*/ */
public class Apple { public class Apple {
/** @name Constructors */ /** @name Constructors */
/** @{ */ /** @{ */
/** Default ctor */
/**
* A Default, doing nothing constructor
* @note We don't use this constructor
*/
Apple () { Apple () {
appleId = appleTileId= 0; appleId = appleTileId= 0;
color = ""; color = "";
points = 0; points = 0;
} }
/** Main ctor */
/**
* 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) { Apple (int appleId, int appleTileId, String color, int points) {
this.appleId = appleId; this.appleId = appleId;
this.appleTileId = appleTileId; this.appleTileId = appleTileId;
@@ -23,7 +39,7 @@ public class Apple {
this.points = points; this.points = points;
} }
/** Copy constructor /** Copy constructor
* @note We don't use clone as long as we don't inherit Cloneable iface
* @param a The apple we want to copy
*/ */
Apple (Apple a) { Apple (Apple a) {
appleId = a.getAppleId (); appleId = a.getAppleId ();
@@ -49,7 +65,7 @@ public class Apple {
* @arg Otherwise zero * @arg Otherwise zero
*/ */
void setColor (String color) { void setColor (String color) {
this.color = color;
this.color = color.toLowerCase();
if (color == "red") points = Math.abs(points); if (color == "red") points = Math.abs(points);
else if (color == "black") points = -Math.abs(points); else if (color == "black") points = -Math.abs(points);
else points = 0; else points = 0;
@@ -62,27 +78,25 @@ public class Apple {
} }
/** @} */ /** @} */
/** @name Data members (private) */
/** @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 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
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 */
/** @} */ /** @} */
} }
//class BadColor extends Exception {
// // default constructor
// BadColor() { }
// // parametrized constructor
// BadColor (String str) { super(str); }
//}

Loading…
Cancel
Save