conway 27s game of life code java

Solutions on MaxInterview for conway 27s game of life code java by the best coders in the world

showing results for - "conway 27s game of life code java"
Giulio
25 Aug 2019
1
2
3
4/**
5
6 * Java class for simulation of Conway's Game of Life. 
7
8 * @author G. Cope
9
10 *
11
12 */
13
14public class Conway2D {
15
16
17
18	private final int width;
19
20
21
22	private final int height;
23
24	
25
26	private final int size;
27
28	
29
30	private int seedCount = 9500;
31
32	/*
33
34	 * Data representing the grid in 1d format. Specified as byte to allow for multiple
35
36	 * phases. 
37
38	 */
39
40	byte[] data;
41
42	
43
44	public Conway2D(){
45
46		this(100, 100);
47
48	}
49
50	/**
51
52	 * Constructs a new Game of Life with the specified dimensions. 
53
54	 * @param width
55
56	 * @param height
57
58	 */
59
60	public Conway2D(int width, int height){
61
62		this.width = width;
63
64		this.height = height;
65
66		this.size = width * height;
67
68		data = new byte[size];
69
70	}
71
72
73
74    /**
75
76	 * Iterates the game one step forward
77
78	 */
79
80	public void iterate(){
81
82		byte[] prev = new byte[size];
83
84		System.arraycopy(data, 0, prev, 0, size);
85
86
87
88		byte[] next = new byte[size];
89
90		for ( int i = 0; i < width; i++ ){
91
92			for ( int j = 0; j < height; j++ ){
93
94				int type = isAlive(i, j, prev);
95
96				
97
98				if ( type > 0 ){
99
100					next[j * width + i] = 1;
101
102				}else{
103
104					next[j * width + i] = 0;
105
106				}
107
108				
109
110			}
111
112		}
113
114		
115
116		System.arraycopy(next, 0, data, 0, size);
117
118		
119
120	}
121
122	
123
124	/**
125
126	 * Checks if the cell is alive
127
128	 * @param x The x position
129
130	 * @param y The y position
131
132	 * @param d The grid data.
133
134	 * @return
135
136	 */
137
138	protected int isAlive(int x, int y, byte[] d){
139
140		int count = 0;
141
142		int pos1 = y * width + x;
143
144		for ( int i = x-1; i <= x + 1; i++ ){
145
146			for ( int j = y - 1; j <= y + 1; j++ ){
147
148				int pos = j * width + i;
149
150				if ( pos >= 0 && pos < size - 1 && pos != pos1){
151
152					if ( d[pos] == 1 ){
153
154						count++;
155
156					}
157
158				}
159
160			}
161
162		}
163
164		//dead
165
166		if ( d[pos1] == 0 ){
167
168			if ( count == 3 ){//becomes alive.
169
170				return 1;
171
172			}
173
174			return 0;//still dead
175
176		}else{//live
177
178			if ( count < 2 || count > 3 ){//Dies
179
180				return 0;
181
182			}
183
184			return 1;//lives
185
186		}
187
188
189
190	}
191
192	
193
194	
195
196	/**
197
198	 * Randomly seeds the grid. 
199
200	 */
201
202	public void randomSeed(){
203
204		for ( int i = 0; i < seedCount; i++ ){
205
206			int x = (int)(Math.random() * width);
207
208			int y = (int)(Math.random() * height);
209
210			data[y * width + x] = 1;
211
212
213
214		}
215
216	}
217
218	
219
220	/**
221
222	 * Retrieves the grid data. 
223
224	 * @return
225
226	 */
227
228	public byte[] getData(){
229
230		return data;
231
232	}
233
234}
235
236