This is an exercice in Test Driven Development
- Implement a
LangtonsAnt
class, according to- these rules here
- Start with
LangtonsAntTest
unit test - break down all the functionality you will need into small chunks/methods not longer then 9-11 lines for each method, so that they can be easily unit-tested.
- write all the tests before its implementation.
- but write an empty class of
LangtonsAnt
with the empty methods you plan even before the tests - the board should be a simple
char[][]
array, prefilled with your test data, which is given in the constructor. - also provide the ant's
x
andy
position as anint
and it's direction ('north','east','south','west') as anstring
orenum
LangtonsAnt
class should have a method (getNextBoardState()
) , which returns the new state of the board after the Ant's rules are applied once.LangtonsAnt
class should also keep the state, so that each subsequent call togetNextBoardState()
should return the following state after after the Ant's rules are applied again once.
.. should look like:
public class LangtonsAnt {
public static char BLACK_CELL = '■';
public static char WHITE_CELL = ' ';
LangtonsAnt(char[][] array, int antX, int antY, String antDirection ) {
}
public char[][] getNextBoardState(){//...
}
}
Find the ONE error in the following example steps! and add another step at the end!
example start 6x6 array: the ant on x=2,y=3, direction=west, "." is a white field, "■" is a black field
......
..■...
...■..
..*...
......
......
with the ant on x=2,y=3, direction=west,
- At a white square, turn 90° clockwise, flip the color of the square, move forward one unit
- At a black square, turn 90° counter-clockwise, flip the color of the square, move forward one unit
- NEW: when ant is facing to the border of the board, it does NOT move forward, but still does flip the color of the square and roteate according to rule 1 and 2 would be:
......
..■...
..*■..
..■...
......
......
while ants position is now x=2,y=2 , direction north (because ant was on a white field):
......
..■...
..■✪..
..■...
......
......
ant position x=3, y=2, direction=east (because ant was on a white field),
......
..■.*.
..■...
..■...
......
......
ant position : x=3, y=1 , direktion north (because last field was a black field)