small improvements

This commit is contained in:
2018-03-09 22:30:42 +01:00
parent d86e5d542a
commit d1617e9cf3

View File

@@ -295,6 +295,7 @@ public class Worm {
return this.actionPoints; return this.actionPoints;
} }
@Basic
public long getMaxActionPoints() { public long getMaxActionPoints() {
return this.maxActionPoints; return this.maxActionPoints;
} }
@@ -410,10 +411,41 @@ public class Worm {
* Return the name of the worm * Return the name of the worm
* the name of the worm expresses the identity of the worm * the name of the worm expresses the identity of the worm
*/ */
@Basic
public String getName() { public String getName() {
return this.name; return this.name;
} }
/**
* check whether the given name is a valid name for all worms
*
* @param name
* the name to check
* @return -1 if and only if the given name is longer then 2,
* the first letter is uppercase and the name only exists
* of letters, " ", " ' " and " "" "
* |for (i = 0; i < name.length(); i++)
* |result == (name.length() > 2 && Character.isUpperCase(name.charAt(0)
* |&& Character.isLetter(name.charAt(i)) &&
* |allowedCharacters.indexOf(name.charAt(i)))
*/
public static int isValidName (String name) {
if (name.length() < 2 || !Character.isUpperCase(name.charAt(0))) {
return 0;
}
String allowedCharacters = "'\" ";
for (int i = 0; i < name.length(); i++) {
if (!Character.isLetter(name.charAt(i))) {
if (allowedCharacters.indexOf(name.charAt(i)) == -1) {
return i;
}
}
}
return -1;
}
/** /**
* set the name of the worm tot the given name * set the name of the worm tot the given name
* *
@@ -434,46 +466,16 @@ public class Worm {
this.name = name; this.name = name;
} }
/**
* check whether the given name is a valid name for all worms
*
* @param name
* the name to check
* @return -1 if and only if the given name is longer then 2,
* the first letter is uppercase and the name only exists
* of letters, " ", " ' " and " "" "
* |for (i = 0; i < name.length(); i++)
* |result == (name.length() > 2 && Character.isUpperCase(name.charAt(0)
* |&& Character.isLetter(name.charAt(i)) &&
* |allowedCharacters.indexOf(name.charAt(i)))
*/
public static int isValidName (String name) {
if (name.length() < 2 || !Character.isUpperCase(name.charAt(0))) {
return 0;
}
String allowedCharacters = "'\" ";
for (int i = 0; i < name.length(); i++) {
if (!Character.isLetter(name.charAt(i))) {
if (allowedCharacters.indexOf(name.charAt(i)) == -1) {
return i;
}
}
}
return -1;
}
/** /**
* this variable contains the name of the worm * this variable contains the name of the worm
*/ */
private String name; private String name;
//=================================================================================== //===================================================================================
// endregion // endregion
// region move // region move
//=================================================================================== //===================================================================================
@@ -551,7 +553,7 @@ public class Worm {
/** /**
* *
* @param angle * @param angle
* @return * @return ...
*/ */
private boolean canTurn(double angle) { private boolean canTurn(double angle) {
return 0 <= angle && angle < (2 * PI) && return 0 <= angle && angle < (2 * PI) &&
@@ -596,21 +598,10 @@ public class Worm {
this.actionPoints = 0; this.actionPoints = 0;
} }
/**
* Return a boolean whether the worm can jump or not
*
* @return True if and only if the action actionPoints is bigger then 0 and the orientation
* is lower then pi
* |result == this.actionPoints > 0 && this.orientation < Math.PI
*/
private boolean canJump() {
return this.actionPoints > 0 && this.orientation < PI;
}
/** /**
* Return the time the worm will jump * Return the time the worm will jump
* *
* @return the time the worm will jump. The distance divided by the velocity multiplied * @return the time the worm will jump. The distance divided by the velocity multiplied
* with the cosinus of the orientation * with the cosinus of the orientation
* |jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation)) * |jumpDistance(this.jumpVelocity) / (this.jumpVelocity * Math.cos(this.orientation))
*/ */
@@ -623,7 +614,7 @@ public class Worm {
} }
/** /**
* *
* @param deltaTime ... * @param deltaTime ...
* @return Tuple<Double, Double> * @return Tuple<Double, Double>
*/ */
@@ -634,7 +625,18 @@ public class Worm {
return Tuple.create(this.location.item1 + this.jumpVelocity() * cos(this.orientation) * deltaTime, return Tuple.create(this.location.item1 + this.jumpVelocity() * cos(this.orientation) * deltaTime,
this.location.item2 + this.jumpVelocity() * sin(this.orientation) * deltaTime - 0.5 * G * pow(deltaTime, 2)); this.location.item2 + this.jumpVelocity() * sin(this.orientation) * deltaTime - 0.5 * G * pow(deltaTime, 2));
} }
/**
* Return a boolean whether the worm can jump or not
*
* @return True if and only if the action actionPoints is bigger then 0 and the orientation
* is lower then pi
* |result == this.actionPoints > 0 && this.orientation < Math.PI
*/
private boolean canJump() {
return this.actionPoints > 0 && this.orientation < PI;
}
/** /**
* Return the distance the worm will jump * Return the distance the worm will jump
* *