template for the classic "Game of Live" or "Langton's Ant" for TestDrivenDevelopment TDD practice
- Implement a
de.bas.game_of_life.<GameName>.javaclass, according to - Start with
de.bas.game_of_life.<GameName>Test.javaunit test - brake down all the functionality you will need into small chunks i.e. methods not longer then 9-11 lines for each method, so that they can be easily unit-tested.
- write all the tests before the implementation.
- but write an empty class of
de.bas.game_of_life.<GameName>.javawith the empty methods you plan even before the tests - the playing field should be a simple
char[][]array, prefilled with you test data, which is given in the constructor. - make sure that
de.bas.game_of_life.<GameName>.javato implements the interfacee.bas.game_of_life.MatrixSource.java - make sure everything is thourougly tested.
add the following constructor to de.bas.game_of_life.<GameName>.java:
//...for Game of Live:
public class GameOfLive implements MatrixSource {
GameOfLive(char[][] array, boolean randomInit) {
//...
} //...
} or for Langton's Ant:
//...for Langton's Ant:
public class LangtonsAnt implements MatrixSource {
LangtonsAnt(char[][] array, int antX, int antY, String antDirection,boolean randomInit ) {
//...if randomInit is true then array content (but not array.size), antX, etc. are ignored and generated as valid random values,
} //...
} with these constaints:
- if
randomInitis false , thenarraygiven to the Constructor is expected to be initialized withMatrixPaneConverter.LIVING_CELL, which is thechar'■'- or
MatrixPaneConverter.DEAD_CELL, which is thespacechar' '; - and before calling the constuctor of GameOfLive
- or, if
randomInitistrue, the array need not be initialized but only allocated withnew, before calling the Constructor of GameOfLive. Then the Constructor should replace the content by random with '■' or ' ' for each element of thearray.
the goal is, not to need to fix anything (apart from the Constructor and the initialization ) in de.bas.game_of_life.<GameName>.java after its integrated with the
de.bas.tdd_helpers.MatrixSequenceViewer.java
Use 2 of the 4 statics in MatrixPaneConverter (MatrixPaneConverter.LIVING_CELL ... MatrixPaneConverter.WHITE_CELL ) but don't bother with the rest of MatrixPaneConverter, ExampleMatrixSource, you don't need this.
Finaly just uncomment line 23 or 24 in MatrixSequenceViewer and comment in line 21 instead.
then run MatrixSequenceViewer
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)
......
..■■*.
..■...
..■...
......
......
ant position : x=4, y=1 , direktion east (because last field was a white field)