I'm learning a lot more about Java 8 and its functional capabilities, and I wanted to do some more practice with it. Say, for example, I have the following imperative code which is for wrapping a circle around the bounds of the screen:
if (circle.getPosition().getX() > width + circle.getRadius()){
circle.getPosition().setX(-circle.getRadius());
}else if (circle.getPosition().getX() < -circle.getRadius()){
circle.getPosition().setX(width + circle.getRadius());
}
if (circle.getPosition().getY() > height + circle.getRadius()){
circle.getPosition().setY(-circle.getRadius());
}else if (circle.getPosition().getY() < -circle.getRadius()){
circle.getPosition().setY(height + circle.getRadius());
}
How could I go about trying to "Functionalize" it? Maybe some pseudo-code? It seems to me that mutability and state seem inherent in this example.
Is functional programming not a good fit for game development? I love the both, so I'm trying to combine them.
There is nothing inherent about the requirement for mutability in this example. The imperative approach is to modify an existing circles by applying side-effects which alter the state of an existing circle.
The functional approach is to have an immutable data structure and create a function that takes data from the first structure and creates a new structure. In your example, a functional approach would have the circle being immutable, i.e. no setX() or setY() methods.
private Circle wrapCircleAroundBounds(Circle circle, double width, double height) {
double newx = (circle.getPosition().getX() > width + circle.getRadius()) ? -circle.getRadius() : width + circle.getRadius()
double newy = (circle.getPosition().getY() > height + circle.getRadius()) ? -circle.getRadius() : height + circle.getRadius()
return new Circle(newx, newy)
}
Using Java8's functional features, you could then imagine mapping a list of circles to wrapped circles:
circles.stream().map(circ -> wrapCircleAroundBounds(circ, width, height))
The imperative and functional approaches have different advantages, the functional approach, for example, is intrisicaly threadsafe because of the immutability so you should be able to more readily parallelise this kind of code. For instance, one could equally safely write:
circles.parallelStream().map(circ -> wrapCircleAroundBounds(circ, width, height))
I don't think that functional programming is necessarily badly suited to game development but, although it has be done, it's certainly not a standard approach so you won't get the same level of library support if you're using a functional language.
As dfeuer states in his answer, Java's functional features are pretty primitive - you don't have support for algebraic data types, pattern matching, etc which will make it much easier to express problems in a functional style (at least once you get used to those idioms). I agree that at least reading a bit about Haskell, which has an excellent tutorial: http://learnyouahaskell.com/chapters would be a good way to get started. Unlike Scala, which is very much a multiparadigm language, you won't have OOP features to fall back on while you're learning the new style.
For your first point: You "functionalize" your example by thinking about what the code ought to achieve. And this is, you have a circle, and want to compute another circle based on some conditions. But for some reason your imperative upbringing makes you assume that the input circle and the output circle should be stored in the same memory locations!
For being functional, the first thing is to forget memory locations and embrace values. Think of every type the same way you think of int or java.lang.Integer or the other numeric types.
For an example, assume some newbie shows you some code like this:
double x = 3.765;
sin(x);
System.out.println("The square root of x is " + x);
and complains that sin doesn't seem to work. What would you think then?
Now consider this:
Circle myCircle = ....;
wrapAroundBoundsOfScreen(myCircle);
System.out.println("The wrapped-around circle is now " + myCircle);
You will have climbed the first step to functional programming when the latter code seems as absurd to you as the former. And yes, this does mean not to use certain features of the imperative language you are using, or use them extremely sparingly.
Here not much 'functionalization' applicable. But at least we can fight with mutability.
First of all pure functions. This will help to separate logic. Make it clear and easy to test.
Answer the question: what is your code do? It accepts some params and returns two params new x and y.
Next samples will be written with pseudo scala.
So you need a function that will be invoked two times for both x and y calculation.
def (xOrY: Int, widthOrHeight: Int, radius: Int): Int = {
if (x > widthOrHeight + radius) -1*radius else widthOrHeight + radius
// do your calculation here - return x or y values.
}
P.S> so far no matter where you want to apply functional style: as you need to do some business logic it's good to go with functional approach.
But do not try overcomplicate it as it does not help.
So what I would not do for this sample is next (pseudo scala goes next):
def tryToMakeMove(conditions: => Boolean, move: => Unit) = if (conditions) move()
/// DO NOT DO IT AT HOME :)
tryToMakeMove(circle.getPosition().getX() > width + circle.getRadius(), circle.getPosition().setX(-circle.getRadius())).andThen()
tryToMakeMove(circle.getPosition().getX() < -circle.getRadius()), circle.getPosition().setX(width + circle.getRadius()))
).andThen ... so on.
That how functional programs can looks like. I've created the higher-order function (that accepts other functions as an arguments and invoke it inside).
With this functions, i've invoked one be one operations you have to do...
But such functional style does not really help. At all. You should apply it properly only in a places where it's simplify the code.
You can write functional code in just about any programming language, but you can't easily learn functional programming in any language. Java in particular makes functional programming sufficiently painful that people who wanted to do functional programming in the JVM came up with Clojure and Scalaz. If you want to learn the functional way of thinking (what problems it deals with naturally and how, what problems are more awkward and how it manages them, etc.), I strongly recommend that you spend some time with a functional or mostly-functional language. Based on a combination of language quality, ease of sticking to functional idioms, learning resources, and community, my top pick would be Haskell and my next would be Racket. Others will of course have other opinions.
How could I go about trying to "Functionalize" it? Maybe some
pseudo-code? It seems to me that mutability and state seem inherent in
this example.
You could try to limit the mutability to a few functions, and also use final variables inside the functions (which forces you to use expressions rather than statements). Here's one possible way:
Position wrapCircle(Circle circle, int width, int height) {
final int radius = circle.getRadius();
final Position pos = circle.getPosition();
final int oldX = pos.getX();
final int x = (oldX > width + radius) ? -radius : (
(oldX < -radius) ? (width + radius) : oldX);
final int y = // similar
return new Position(x, y);
}
circle.setPosition(wrapCircle(circle, width, height));
Aside, I would make wrapCircle a method of the Circle class, to get:
circle.wrapCircle(width, height);
Or I could go one step further and define a getWrappedCircle method, that returns me a new circle instance:
Circle getWrappedCircle(width, height) {
newCircle = this.clone();
newCircle.wrapCircle(width, height);
return newCircle();
}
.. depending on how you intend to structure the rest of the code.
Tip: Use final keyword as often as you can in Java. It automatically lends to a more functional style.
Is functional programming not a good fit for game development? I love the both, so I'm trying to combine them.
Pure functional programming is slower, because it requires lots of copying / cloning of data. If performance is important, then you could definitely try a mixed approach, as shown above.
I would suggest using as much immutability as possible, followed by benchmarking, and then converting to mutability in only the performance critical sections.
Functional programming fits game development (why would not it?). The question is usually more about performance and memory consumption or even if any functional game engine can beat an existing non-functional one in those metrics. You are not the only person who loves functional programming and game development. Seems like John Carmack does too, watch his keynotes about the topics at Quakecon 2013 starting from 02:05. His notes here and here even give insight on how a functional game engine can be structured.
Setting theoretical foundation aside, there are usually two concepts perceived inherent in functional programming by a newcomer and from a practical prospect. They are data immutability and state absence. The former means that data never changes and the latter means every task is performed as if for the first time with no prior knowledge.
Considering that, you imperative code has two problems: the setters mutate the circle position and the code relies on outside values (a global state) of width and height. To fix them make your function return a new circle on each update and take the screen resolutions as arguments. Let's apply the first clue from the video and pass a reference to the static snapshot of the world and a reference to an entity being "updated" (it is simply this here) to an update function:
class Circle extends ImmutableEntity {
private int radius;
public Circle(State state, Position position, int radius) {
super(state, position);
this.radius = radius;
}
public int getRadius() {
return radius;
}
#Override
public ImmutableEntity update(World world) {
int updatedX = getPosition().getX();
if (getPosition().getX() > world.getWidth() + radius){
updatedX = -radius;
} else if (getPosition().getX() < -radius){
updatedX = world.getWidth() + radius;
}
int updatedY = getPosition().getX();
if (getPosition().getY() > world.getHeight() + radius){
updatedY = -radius;
} else if (getPosition().getY() < -radius) {
updatedY = world.getHeight() + radius;
}
return new Circle(getState(), new Position(updatedX, updatedY), radius);
}
}
class Position {
private int x;
private int y;
//here can be other quantities like speed, velocity etc.
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
class State { /*...*/ }
abstract class ImmutableEntity {
private State state;
private Position position;
public ImmutableEntity(State state, Position position) {
this.state = state;
this.position = position;
}
public State getState() {
return state;
}
public Position getPosition() {
return position;
}
public abstract ImmutableEntity update(World world);
}
class World {
private int width;
private int height;
public World(int width, int height) {
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Now the tricky part is how to affect the state of the world and other entities. You can follow the second clue from the video and use event passing mechanism to pass such changes to and fro so the rest of the game knows about all the effects.
Obviously, you can keep only events and rely completely on them even when changing your circle positions. So, if you introduce sort of an id to your entities you will be able to pass MoveEntity(id, newPosition).
OK, it's time for us all to get over how new and shiny Java 8's functional features look. "Functionalizing" something is really not a valid goal to have.
However, the original code here has a good ol' object-oriented problem:
When you say circle.getPosition().setX(...), you are messing with the internal state of the circle (its position) without involving the object itself. That breaks encapsulation. If the circle class were properly designed, then the getPosition() method would return a copy of the position or an immutable position so that you couldn't do this.
That is the problem you really need to fix with this code...
How, then, should you do that?
Well, you could certainly come up with some functional interface in Circle, but honestly your code will be more readable if you just have circle.move(double x, double y);
Related
I´m currently programming my first chess game and I have a small problem with my pieces. I´ll add some of my code below, but generally, I would like to check whether the tile a player moves a certain piece (in this example the king) to, is already occupied by another piece because in that case the other piece would either be killed (if the piece belongs to the other player) or the move would be false (tile occupied by own piece).
If you have any ideas how it could be done, I´d appreciate it greatly.
Best regards
public abstract class Piece
{
protected int x;
protected int y;
protected Tile finalTile;
protected Type type;
protected boolean killed;
protected boolean white;
public Piece(boolean white, int x, int y)
{
this.setWhite(white);
this.x = x;
this.y = y;
this.finalTile = finalTile;
}
public boolean isWhite() {
return this.white;
}
public void setWhite (boolean white) {
this.white = white;
}
public boolean isKilled() {
return this.killed;
}
public void setKilled (boolean killed) {
this.killed = killed;
}
// which type of Piece is being used
public abstract Type getType();
//is the chosen move valid?
public abstract boolean canMovePiece (Chessboard chessboard, int finalX, int finalY);
//The validation of the move does not show in which direction the piece is being moved. To set
the piece at its new spot (tile), a determination of the direction is needed for the
// calculation of the new spot on the chessboard
public abstract void setNewPieceLocation();
}
public class King extends Piece
{
public King(boolean white, int x, int y)
{
super(white, x, y);
type = Type.KING;
}
public Type getType() {
return Type.KING;
}
#Override
public boolean canMovePiece(Chessboard chessboard, int finalX, int finalY) {
int x_diff = Math.abs(finalX - this.x);
int y_diff = Math.abs(finalY - this.y);
//the king can only move one tile per round. The direction does not matter. Therefore, the
movements can be either one on the x-axis, the y-axis or one on both.
if ( x_diff == 1 && y_diff == 0 || x_diff == 0 && y_diff == 1 || x_diff == 1 && y_diff ==1) {
return true;
}
}
#Override
public void setNewPieceLocation() {
int finalX = this.x;
int finalY = this.y;
}
}
In tile-based computer games, you always have to make a very important initial design decision about what you choose as your central data structure:
The field itself, e.g. as a two-dimensional array, where the state of each tile in the field represents which piece is on it or
A collection of pieces with coordinates.
Option 1 is great if you have a small field and the pieces can only be directly on a tile.
Option 2 should only be done if option 2 is not possible, i.e.:
your game world is very large and sparse, e.g. 1 million x 1 million, then you would have an array with one trillion entries or (inclusive or)
your pieces can be halfway or otherwise fractionally located respectively with the tiles
Option 1 is easiest to implement if there is at most one piece at any one tile at the same time but you can work around that and it would depend on the specific circumstances, which one is best.
Now which case do we have in chess? As an exercise you can think about this yourself but I will give the solution below:
The first option is the perfect fit for chess, because you have a very small field (8x8), the pieces are exactly on the tiles (in theory, even if they may be a few millimeters off in practice) and there can be at most one piece per tile.
Option 1 has the advantage that it becomes extremely easy to check, which piece is on a certain tile: just check the field data structure at that point!
Another very important point for beginners is however how to map your coordinate system to your data structure especially in the case of a two dimensional array. Which dimension of the array is which axis of your game world and in which direction? There is however no right or wrong here, just clearly document it and be consistent!
For example, I personally like to have y going from top to bottom as the first dimension of the array and x going from left to right as field[y][x] because computer graphics memory is aligned that way (at least it was this way in the old 10h 320x200 8 bit palette video mode) but a mathematics teacher may very well prefer x first and y going from bottom to top as field[x][y] because that's how you usually draw 2-dimensional plots and write points with x first in mathematical notation.
So in my mental model, checking the top left would be checking the value field[0][0], top right would be field[0][7], bottom left field[7][0] and bottom right would be field[7][7].
Our hypothetical mathematics teacher could instead use field[0][7] for top left, top right would be field[7][7], bottom left field[0][0] and bottom right would be field[7][0].
Just use what works for you (and your team if you have one).
In order to prevent problems with object equality vs object identity I would not use a class for the type but an enum as that is the perfect fit for this case. As an empty field, you can either use null, but then you may need a bunch of checks to prevent NullPointerException, or you create a special value for an empty field.
Checking, which piece is in a certain spot with option 2 is not hard either, but as option 1 is so much easier here and also faster in case that matters, I will not get into detail about that here as that is not the best solution to your problem.
I'm trying to create a bullet hell type game similar to Touhou in Java.
I need some help getting started, like an example to follow. I tried my hand in it making it from scratch using JFrame, but my main problem is lag when creating bullets.
I have a bullet class, with parameters to make it go from A to B. But whenever the program creates or removes bullet objects (which are stored in an ArrayList) it freezes for a tiny fraction of a second, but this becomes very pronounced when there are many bullets.
Am I doing it wrong to use an ArrayList here? What guides are recommended to learn how to do this properly?
Here is my Bullet class:
import java.awt.Graphics;
public class Bullet
{
double x,y, xend, yend;
int width, height;
int life = 200;
private static final double speed = 8;
double dirX, dirY;
public Bullet(double x, double y, double xend, double yend, int width, int height)
{
this.x=x;
this.y=y;
this.xend = xend;
this.yend = yend;
this.width = width;
this.height = height;
dirX = speed*Math.cos(getAngle());
dirY = speed*Math.sin(getAngle());
}
public double getAngle()
{
double angle = (double) Math.toDegrees(Math.atan2(yend-y, xend - x));
if(angle < 0){
angle += 360;
}
System.out.println(angle);
return Math.toRadians(angle);
}
public void tick()
{
life--;
x+=dirX;
y+=dirY;
}
public void draw (Graphics g)
{
g.fillRect((int) Math.round(this.x),(int) Math.round(this.y),this.width,this.height);
}
}
And this is the code that is run when the user creates a bullet. It uses the mouseListener for the xend yend coordinates - finding the bullet's destination.
public void fireNormal(double xend, double yend)
{
Bullet bullet= new Bullet (x,y,xend,yend,5,5);
firedBullets.add(bullet);
}
}
I'm using a timer to refresh the game but it still isn't at a consistent speed because of the freezes.
ArrayList.add() should usually be extremely fast. Occasionally, when the implementation has to add new capacity, it will allocate a new backing array, copy the data from one to the other, before continuing. You can avoid this by specifying a suitable initial capacity in the ArrayList(int initialCapacity) constructor.
However you would need tens of thousands of entries for this to take long enough to add noticeable game lag.
ArrayList.remove(int index) has to shift all the entries above the removed item, down the backing array by one slot. However, this is highly optimised at the CPU level, and again should not add noticeable game lag unless you have tens of thousands of bullets.
ArrayList.remove(Object o) has to examine every element in turn, and run equals() on each one, until it finds the target element. Avoid this!
It's possible that the pauses you are seeing are due to garbage collection, and this could be because you're creating thousands of short-lived Bullet objects. These last from the moment they're fired until they go off screen. When they go out of scope, they remain in memory until the GC has to clear up thousands of them.
You could avoid this by maintaining an object pool of Bullets. There are libraries with object pool implementations, or you can roll your own using Collections.
In environments with tight memory constraints (like 8 bit games consoles) it's necessary to ensure a game runs within fixed memory, and this is similar to the kinds of things programs on those platforms would do.
However you should prove what your issue is, before trying to fix it, and for that you need to use a profiler such as VisualVM. This will show you where your execution is spending its time, and how it is using memory. Garbage collection in modern Java is very high-performing in most circumstances, though - don't assume it's the problem.
I need to get the horizontal and vertical movements of a vehicle using the speed, time and angle input. In my target class I have:
public double toRadians = Math.toRadians(angle);
public double getHorizontal() {
return (speed*time*Math.cos(toRadians));
}
public double getVertical() {
return (speed*time*Math.cos(toRadians));
}
however, the values of horizontal and vertical movement give me just the speed*time, so I assume there must be something wrong in the way I declared the math function?
There is something not right about your code.
Hint: what is the difference between these two functions apart from their respective names?
public double getHorizontal() {
return (speed*time*Math.cos(toRadians));
}
public double getVertical() {
return (speed*time*Math.cos(toRadians));
}
Hint 2: Suppose you plot a graph with f(t) on the X axis against f(t) on the Y access, and vary the values of t. what will that graph look like?
I assume there must be something wrong in the way I declared the math function?
The declarations are not the problem. It is the function bodies that the problem.
(Though actually, it is a bit of the problem that, speed, time and radians (or angle) are instance variables rather than arguments to the functions / methods. But deal with that problem after the problem with .... the formulae.)
So I just started learning Java yesterday coming from a different language, and I am reading through my textbook and finding it to be pretty nice so far. However I did an exercise that basically required me to create a new Object use Rectangle and find the area. Below is the working code I came up with.
Now coming from other programming languages I was just toying around with this and did int area,width,height;and then it gave me an error saying that I had to use double in order to utilize .getWidth();, .getHeight(). I couldn't find anything in my book telling me why I had to make this a double and I started looking online and found this link
Now I found some documentation online where It told me to use double as well, but I'm not really sure why would I need to set these as doubles. Is it because the people who made Java, knew that precision is needed when we are working with coordinates and doing math with widths, heights and coordinates? My book says that it takes more memory to make a double variable rather than an int ( I come from doing lots of javascript and PHP, so reading on what a float and double does was something good for me).
I.E. Why do I need to make my area,height,width variable doubles in order to use .getWidth,.getHeight
package keepo;
import java.awt.Rectangle;
public class tuna{
public static void main(String [] args){
Rectangle rect = new Rectangle(10,20,50,40);
double area,width,height;
width = rect.getWidth();
height = rect.getHeight();
area = width * height;
System.out.println("Width is : " + width + "Height is : " + height);
System.out.println("Area is : " + area);
}
}
It is because this is how these methods have been defined in the java api. As you can see under the modifier and type column that the methods getWidth(), getHeight() all return value of type double.
Because in this case, you should not use those methods. The AWT class Rectangle does store coordinates as ints. You can easily read them back as ints if that's what you want to do, by accessing the fields instead of calling the getter methods:
int area, width, height;
width = rect.width; // not getWidth()
height = rect.height; // not getHeight()
area = width * height;
The getWidth() and getHeight() methods serve zero purpose here, as they will always return the same value as the fields, except as a different type (and you can already assign any int value to a double anyway, when a double is what you want to use).
So why do those two methods (along with getX() and getY()) exist at all? Because in Java 1.2 the geometry stuff in the API was expanded. People wanted to be able to work with floating-point coordinates, which Rectangle cannot do. And the Java maintainers couldn't change the fields of Rectangle from int to double because that would break backwards compatibility with how old code was already using it. So two new classes, Rectangle2D.Float and Rectangle2D.Double were added, which store coordinates as floats and doubles respectively.
But what if you want to work generically with any rectangle, without writing separate code for all the rectangle flavors? A new abstract class, Rectangle2D was also added, as the superclass of the three rectangle classes. This class is abstract (meaning it cannot be created on its own, as it is incomplete) and it does not store any coordinates itself. It does however, specify a contract that its subclasses follow (meaning that any Rectangle2D method is available in all three of its implementations). That includes the getWidth() and getHeight() methods that return doubles, regardless of the actual storage type of the particular rectangle.
Taking the abstraction an extra, perhaps superfluous, level, they also added RectangularShape as the superclass of several shapes with rectangular bounds: Rectangle2D, RoundRectangle2D, Ellipse2D and Arc2D. That is the class that actually declares the getWidth() and getHeight() methods, which all RectangularShape subclasses must provide:
// What's this shape? A rectangle? An ellipse? Does it use ints? floats? doubles?
RectangularShape something = ......;
// We don't care!
System.out.println("The shape (whatever it is) occupies an area of:");
System.out.println(something.getWidth() + " × " + something.getHeight());
So you can call those getter methods on any rectangle (or "rectangular shape") to get its coordinates, but if you know you have a particular shape class, you can/should access its fields directly, as that is simpler, and it gives you the values without converting them to a different type.
P.S. It is a similar story with Point, which uses int coordinates, but provides double getX() and double getY() methods, because of the later-added classes Point2D.Float, and Point2D.Double, and the abstract superclass Point2D.
P.P.S. There is actually a small advantage to using double (or long) for your rectangle's area, even if your rectangle coordinates are ints. Large multiplications could overflow the 32-bit range of an int, producing the wrong result. If you convert at least one of the values to a larger type, it will cause the multiplication to be done in that larger type, which you can then safely store without overflow:
Rectangle big = new Rectangle(0, 0, 1000000, 1000000);
int area = big.width * big.height;
long bigArea = (long)big.width * big.height;
System.out.println(area); // -727379968 (uh oh!)
System.out.println(bigArea); // 1000000000000
Imran Ali is right.
This is java documentations for getHeight() and for getWidth() it's same.
java.awt.Rectangle
public double getHeight()
Returns the height of the bounding Rectangle in double precisionReturns:
the height of the bounding Rectangle.
But if you want/need to use int instead of double, use following codes for height and repeat them for width too:
using getSize() method which returns rectangle dimension then use it's fields (width and height)
int height = rect.getSize().height;
using data type casting
int height = (int) rect.getHeight();
int height = (int) rect.getSize().getHeight();
The Rectangle.getWidth() and Rectangle.getHeight()methods both return their values with double precision, as stated by others. It is easier if you just keep using them, in order to prevent the Rectangle's values from being changed on accident, by simply casting the value to an int:
int width = (int)rect.getWidth()
and int height = (int)rect.getHeight()
I'm relatively new to TDD design and am working on a project to gain experience with it. My project is a tower defense style game - I'm starting out with a basic Creep (monster) and designing it based off of tested behavior. However, how the creep moves is a rather complicated method. It currently looks something along the lines of:
public void moveToward(Point2D destination) {
if (canMove()) {
if (speedGreaterThanDistance(destination) {
leftoverDistance = calculateLevtoverDistance(destination);
currentLocation.setLocation(destination);
} else {
// do math to calculate x distance and y distance to move
Point2D newLocation = new Point2D.Double(oldX + xTrans, oldY + yTrans);
currentLocation.setLocation(newLocation);
}
}
}
This really doesn't feel like the right design and structure, but I'm unsure how to go about segmenting it up. Does anyone have any suggestions or links to point me to on how to go about doing this? My design has a Creep object as part of my business logic, so I know it's going to have to be at least somewhat complex, but it just doesn't seem like I have it right at the moment. Could part of my problem be that I don't have appropriate value objects in place?
Thanks,
If I understand your problem correctly, you have a Creep with a CurrentPosition (a Point2D), a Direction (a Vector xTrans, yTrans) and a Destination (a Point2D).
The Creep behavior is, as far as I understand:
When the Creep cannot move, its CurrentPosition after a turn should remain the same,
When the Creep can move and the distance between CurrentPosition and Destination is less than the norm of Direction, the after a turn CurrentPosition should be Destination,
When the Creep can move and the distance between CurrentPosition and Destination is strictly greater than the norm of Direction, the after a turn CurrentPosition should be CurrentPosition + Direction.
In that perspective, I would tend to extract the calculation/geometry part into a Geometry class, and state the Creep behavior as
public void moveToward(Point2D destination)
{
if (CanMove())
{
var distanceToDestination = Geometry.Distance(CurrentPosition, destination);
var distancePerTurn = Geometry.Length(Direction);
if (distanceToDestination <= distancePerTurn)
{
CurrentPosition = destination;
}
else
{
CurrentPosition = Geometry.Add(CurrentPosition, Direction);
}
}
}
You can now test the methods on the Geometry class, and the 3 behaviors of the Creep should be testable as well.