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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
public class Colony {
public boolean[][] colony;
public int columns;
public int rows;
public Colony(int rows, int columns) {
colony = new boolean[rows][columns];
this.columns = columns;
this.rows = rows;
}
public int getWidth() {
return columns;
}
public int getHeight() {
return rows;
}
public boolean getCell(int row, int col) {
if (row < 0 || row > rows-1 || col < 0 || col > columns-1) {
return false;
}
else {
return colony[row][col];
}
}
public void setCell(int row, int col, boolean state) {
if (row >= 0 && row < rows && col >= 0 && col < columns) {
colony[row][col] = state;
}
}
/* kode fra ingrid
public boolean getCell(int rader, int kolonner){
boolean celle = colony[rader][kolonner];
if(rader<0 || kolonner<0){
return false;
}
if((rader>colony.length-1) || (kolonner>colony[0].length-1)){
return false;
}
else{
return celle;
}
}
public void setCell(int rad, int kolonne, boolean sannhetsverdi){
if(rad>=0 && kolonne>=0){
if((rad<colony.length) && (kolonne<colony[0].length)){
colony[rad][kolonne] = sannhetsverdi;
}
}
}
*/
public int countNeighbours(int row, int col) {
int i,j,neighbours=0;
for (i=row-1;i<=row+1;i++) {
if (i >= 0 && i < rows) {
for (j=col-1;j<=col+1;j++) {
if (j >= 0 && j < columns) {
if (!(i == row && j == col)) {
if (colony[i][j]) {
neighbours++;
}
}
}
}
}
}
return neighbours;
}
public String toString() {
StringBuffer s = new StringBuffer();
int i,j;
for (i=0;i<rows;i++) {
for (j=0;j<columns;j++) {
if (colony[i][j]) {
s.append("*");
}
else {
s.append(" ");
}
}
s.append("\n");
}
return s.toString();
}
public void copyCells(int row, int col, Colony alienColony) {
int a,b;
a = row;
for (int i=0;i<alienColony.colony.length;i++) {
b = col;
for (int j=0;j<alienColony.colony[i].length;j++) {
colony[a][b] = alienColony.colony[i][j];
b++;
}
a++;
}
}
} |