/[xulu]/trunk/src/skrueger/gol/GameOfLife.java
ViewVC logotype

Contents of /trunk/src/skrueger/gol/GameOfLife.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations)
Wed Feb 25 11:54:01 2009 UTC (15 years, 9 months ago) by mojays
File size: 4640 byte(s)
First Commit, corresponds to Revision 1008 of Wikisquare-SVN 
1 /** XULU - This file is part of the eXtendable Unified Land Use Modelling Platform (XULU)
2
3 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
4 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
5 You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
6
7 Diese Bibliothek ist freie Software; Sie dürfen sie unter den Bedingungen der GNU Lesser General Public License, wie von der Free Software Foundation veröffentlicht, weiterverteilen und/oder modifizieren; entweder gemäß Version 2.1 der Lizenz oder (nach Ihrer Option) jeder späteren Version.
8 Diese Bibliothek wird in der Hoffnung weiterverbreitet, daß sie nützlich sein wird, jedoch OHNE IRGENDEINE GARANTIE, auch ohne die implizierte Garantie der MARKTREIFE oder der VERWENDBARKEIT FÜR EINEN BESTIMMTEN ZWECK. Mehr Details finden Sie in der GNU Lesser General Public License.
9 Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit dieser Bibliothek erhalten haben; falls nicht, schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
10 **/
11
12 package skrueger.gol;
13
14 import java.awt.image.DataBuffer;
15
16 import schmitzm.data.WritableGrid;
17 import schmitzm.data.property.ScalarProperty;
18 import edu.bonn.xulu.model.AbstractStepModel;
19
20 public class GameOfLife extends AbstractStepModel {
21 private WritableGrid inputGrid;
22 private WritableGrid outputGrid;
23
24 public GameOfLife() {
25 super(new GameOfLifeContentManager() );
26
27 stepCount = 1000;
28 }
29
30 @Override
31 public void performModelStep(int stepNo) {
32
33 int[][] diffs = new int[][] {
34 { -1,-1 }, {0,-1}, {1,-1} ,
35 { -1,0 }, {1,0},
36 { -1,1 }, {0,1}, {1,1}
37 };
38
39 boolean anyBodyAlive = false;
40
41 boolean[][] tmpGrid = new boolean[inputGrid.getWidth()][inputGrid.getHeight()];
42
43 for (int i = 1; i < inputGrid.getWidth()-1; i++ ) {
44 for (int ii = 1; ii < inputGrid.getHeight()-1; ii++ ) {
45 checkBreakingCommands();
46
47 int X = inputGrid.getMinX() + i;
48 int Y = inputGrid.getMinY() + ii;
49
50 int anzNachbarn = 0;
51
52 for (int d = 0; d < diffs.length; d++) {
53 float rasterSample = outputGrid.getRasterSampleAsFloat(X +diffs[d][0], Y +diffs[d][1]);
54 if (rasterSample > 0f)
55 anzNachbarn++;
56 }
57
58 boolean lebt = outputGrid.getRasterSampleAsFloat(X , Y ) > 0f;
59
60 // Neugruendung
61 if (!lebt && (anzNachbarn==3))
62 tmpGrid[i][ii] = true;
63 else
64 if (lebt && ((anzNachbarn < 2) || (anzNachbarn > 3 )) )
65 tmpGrid[i][ii] = false;
66 else
67 tmpGrid[i][ii] = lebt;
68
69 if (anzNachbarn>0) anyBodyAlive = true;
70 }
71 }
72
73 // TempGrid auf das echte Grid schreiben
74 for (int i = 1; i < inputGrid.getWidth()-1;i++ ) {
75 for (int ii = 1; ii < inputGrid.getHeight()-1; ii++ ) {
76
77 int X = inputGrid.getMinX() + i;
78 int Y = inputGrid.getMinY() + ii;
79
80 outputGrid.setRasterSample( tmpGrid[i][ii] ? 1f : 0f ,X ,Y);
81
82
83 }
84 }
85
86 if (!anyBodyAlive) {
87 stepCount = stepNo;
88 statusOut.println("Lebt keiner mehr... höre hier auf!");
89 }
90 }
91
92 @Override
93 public void performModelDispose() {
94 // TODO Auto-generated method stub
95
96 }
97
98 @Override
99 public void performModelInit() {
100 ScalarProperty sp = (ScalarProperty) this.contManager.getResource(0).getData();
101 inputGrid = (WritableGrid) sp.getOneTimeReadAccess().getValue();
102
103 sp = (ScalarProperty) this.contManager.getResource(1).getData();
104 outputGrid = (WritableGrid) sp.getOneTimeWriteAccess().getValue();
105
106 int startAnz = 0;
107 for (int i = 0; i < inputGrid.getWidth();i++ ) {
108 for (int ii = 0; ii < inputGrid.getHeight(); ii++ ) {
109 if (inputGrid.getSampleType() == DataBuffer.TYPE_FLOAT) {
110 float val = (float) Math.random();
111 if (val > 0.7 ) {
112 val = 1f;
113 startAnz++;
114 }
115 else val = 0f;
116
117 inputGrid.setRasterSample(val, inputGrid.getMinX() + i, inputGrid.getMinY() + ii);
118 outputGrid.setRasterSample(val, inputGrid.getMinX() + i, inputGrid.getMinY() + ii);
119 } else {
120 statusOut.println("schlecht!");
121 }
122 }
123 }
124 statusOut.println("Anzahl der startenden Individuen: "+startAnz);
125
126
127 }
128
129 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26