!" #
$ % &
EclipseUML by examples 2
' (
' (
How to Install
Class Diagram Example The Game of Life
' (
' (
Overview and how to install
Class Diagram Example The Game of Life
)* )* & & + ( + ( , ,
& + (
- . *+ (
* (/00... (
1* 1 + ( ( " . * *
2 3 . * 4 *
* + ( (
& 1 1 . 1 1 /
+ ( +
.* * ( 5
+ ( + ( +
.* *( 3 36 3 $
( 3
EclipseUML by examples 5
7 . + (
7 . + (
* (/00... ( 0 . 01 0 89(
. + ( 44 . *- &
( " :;" 44". ! ( ( " :;" 44" 8" < (
. + (
( " =4 4 !44 !9
- & 3 ( ( " :;
/> (
> > > (
9 "9 ( " =4 4 !44 !9
( 1 - * * -
EclipseUML by examples 6
' (
' (
Overview and how to install
Class Diagram Example
The Game of Life
: +8 (
: +8 (
* 1 . ( *1 * ( -
( >( > =4 4> >
> : > : +8 ( > : +8 ( *
* .
* (
: +8 (
: +8 (
EclipseUML by examples 9
' (
' (
Overview and how to install Class Diagram Example
The Game of Life
EclipseUML by examples 10
' 1 ' 1
* 1 . 8 1
. .* * ( 1* &(
? + + @( 9
* . ( $-3
(- * $-+ ( -3
* .*
* (/00... ( 0 0
' 1* A 1
' 1* A 1 1 1
1 ( - 1 B C < *
$ D$ 8 1 - -
$
* . $-( < B
* . $- * B (-
+ * * * *$ *
1* * -
((- ( 1 * 3. * $
1 *$ 1 * )* * (( 8
( * $
' 1* A 1 1 ' 1* A 1 1
. * 8 - * *$
$ C$ * D
. * . * *$ -
C D
* 3
C * D
EclipseUML by examples 13
)* $9 . 1 -, )* $9 . 1 -,
* $ * - -$
Board Game Rule
!
EclipseUML by examples 14
9 9
+ 9
public Board() {
this.cells = new int[10][10];
}
public Board(int[][] cells) { this.cells = cells;
}
public String toString() {
return "Board for game of life";
}
A A 9 9 ' ' 9 9
+ A 9
public Game(Board aBoard) { this.board = aBoard;
this.generation = 1;
}
public Game() {
setBoard( new Board());
}
+ ' 9
public Rule(String name) { this .name = name;
}
($ <(
($ <(
( . * ($ < * 1 . /
int[][] boardCells = {
{0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}};
Board board = new Board(boardCells);
Game game = new Game(board);
game
EclipseUML by examples 17
( (
7 . - 1* A . .
* (( ,
6
: 1 ( 1 * *
A
- /
public static Game getIstance() { if(theInstance == null)
theInstance = new Game();
return theInstance;
}
EclipseUML by examples 18
+ +
+ * /
public String toString(){
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < getCells().length; i++){
buffer.append("\n");
for (int j = 0; j < getCells()[0].length; j++){
if (getCells()[i][j] == 0) buffer.append(”0");
else
buffer.append(”1");
} }
return buffer.toString();
}
($ <(
($ <(
+8 . * ($ < * 1 . /
int[][] boardCells = {
{0, 0, 0, 1, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0 , 0, 0, 1}, {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1}, {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1}, {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0 , 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0 , 0, 0, 1}, {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1}, {0, 0, 1, 1, 1, 0, 0 , 0, 0, 1}};
Board board = new Board(boardCells);
System.out.println(board)
' 1 ' 1
* * Board TwoDimensionalBoard
' 1 "E'
$ Board . *
$ evolve *
public abstract void evolve(Game);
public abstract void
evolve(Game, int);
EclipseUML by examples 21
' ' ' 1 ' 1
* * Rule 1 abstract
* 1 . /
EclipseUML by examples 22
1 1
. 1 TwoDimensionalRule . * . * / ((-CD
F CD
* (- 3
1 $ * . $ 43
1 * . $
1 1 ( (
( * !
(
EclipseUML by examples 25
' * A
' * A
< * Game ( - 1 abstract
TwoDimensionalGame * * 1 Game
: 1 * Game * /
public Game() {
this.rules = new Rule[3];
this.generation = 1;
}
( TwoDimensionalGame
* 1 . $ * 1* /
private TwoDimensionalGame(){
setBoard(new TwoDimensionalBoard());
getRules()[0] = new TwoDimensionalBirthRule("Birth Rule");
getRules()[1] = new TwoDimensionalSurvivalRule("Survivel Rule");
getRules()[2] = new TwoDimensionalDeathRule("Death Rule");
}
EclipseUML by examples 26
* *
* *
( evolve * * TwoDimensionalBoard
public void evolve(Game aGame) { boolean doesAnyApply = false;
TwoDimensionalRule rule = null;
for (int i = 0; i < cells.length; i++){
for (int j = 0; j < cells[0].length; j++){
doesAnyApply = false;
for (int k = 0; k < aGame.getRules().length; k++){
rule = (TwoDimensionalRule)aGame.getRules()[k];
if(rule.doesApply(getCells(), i, j)){
getCells()[i][j] = rule.getCellValue();
doesAnyApply = true;
break;
} }
if (!(doesAnyApply))
System.out.println("No rules apply for cell[" + i + "][" + j + "]");
} } }
+ *
+ *
( evolve * .* * $
- ( index/
public void evolve(Game aGame, int index){
if (index == 0) return;
for (int i = 1; i <= index; i++){
evolve(aGame);
System.out.println(this);
} }
( * * Game /
public void run(){
getBoard().evolve( this , getGeneration());
}
* *
. ( < . . * * <
* 1 . /
public static void main(String[] args) { int[][] boardCells = {
{0, 0, 0, 1, 0, 0, 1 , 0, 1, 0}, {0, 0, 1, 0, 1, 0, 1 , 0, 0, 0}, {1, 0, 0, 1, 0, 0, 1 , 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1 , 0, 0, 0}, {0, 0, 1, 0, 1, 0, 1 , 1 ,0, 0}, {0, 0, 0, 1, 0, 0, 1 , 1, 1, 0}, {0, 0, 0, 1, 0, 0, 1 , 1, 0, 1}, {1, 0, 1, 0, 1, 0, 1 , 0, 1, 0}, {0, 0, 1, 0, 1, 0, 1 , 0, 1, 1}, {0, 0, 0, 1, 0, 0, 1 , 0, 0, 1}};
TwoDimensionalBoard board = new TwoDimensionalBoard(boardCells);
TwoDimensionalGame game = TwoDimensionalGame.getInstance();
game.setBoard(board);
game.setGeneration(10);
game.run();
}