Id: 5304; Nick: n/a; Timestamp: 2008-02-07 11:54:12; Pasted as: Java
Description: n/a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
ublic class GameOfLife {
 
	public static Colony colony;
 
	public static void main(String[]input) {
		boolean[][] colonyGather = new boolean[input.length][];
		int i,j;
		int currentLine,longestLine=0;
 
 
		for (i=0;i<input.length;i++) {
			currentLine = input[i].length();
			if (currentLine > longestLine) { 
				longestLine = currentLine;
			}
			colonyGather[i] = new boolean[currentLine];
			for (j=0;j<currentLine;j++) {
				if (input[i].charAt(j) == '*') {
	 				colonyGather[i][j] = true;
				}
			}
		}
 
		int timesRun=0;
		int countChanged=Integer.MAX_VALUE;
 
		colony = new Colony(input.length, longestLine);
		Colony c = new Colony(input.length, longestLine);
		c.colony = colonyGather;
		colony.copyCells(0, 0, c);
 
		Colony temp;
 
		while ((countChanged > 0) && (timesRun < 100)) {
			temp = new Colony(input.length, longestLine);
			countChanged = stepColony(colony,temp);
			timesRun++;
		}
 
		System.out.println(timesRun);
 
	}
	public static boolean willLive(boolean stateOfCell, int liveNeighbours) {
		if (stateOfCell) {
			if (liveNeighbours == 2 || liveNeighbours == 3) { return true; }
			else { return false; }
		}
		else {
			if (liveNeighbours == 3) { return true; }
			else { return false; }
		}
	}
	public static int stepColony(Colony c1, Colony c2) {
		int i,j,changed=0;
		for (i=0;i<c1.colony.length;i++) {
			for (j=0;j<c1.colony[0].length;j++) {
				c2.colony[i][j] = willLive(c1.colony[i][j],c1.countNeighbours(i,j));
				if ((c1.colony[i][j] && !c2.colony[i][j]) || (!c1.colony[i][j] && c2.colony[i][j])) { changed++; }
			}
		}
		colony = c2;
		return changed;
	}
}
Options: New, Download,
;
Copyright © 2006 by Zyberdog - Some rights reserved. (Browse/Disclaimer/Stats/About) - Highlighted with GeSHi