forked from mrThe/Game-of-Life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTorArray.java
83 lines (62 loc) · 1.65 KB
/
TorArray.java
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
/**
* @author mr.The
* @skype mr-the
* @twitter @mr_The
*/
package javalife;
/**
* 2d array to tor
*/
public class TorArray {
public Integer data[][];
private int width, height;
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public void setHeight(int height) {
this.height = height;
}
public void setWidth(int width) {
this.width = width;
}
public TorArray(int width, int height) {
data = new Integer[width][height];
setWidth(width);
setHeight(height);
}
private int calcX(int x) {
if(x > getWidth() - 1) x = 0;
if(x < 0) x = getWidth() - 1;
return x;
}
private int calcY(int y) {
if(y > getHeight() - 1) y = 0;
if(y < 0) y = getHeight() - 1;
return y;
}
public void setCell(int x, int y, int value) {
x = calcX(x);
y = calcY(y);
data[x][y]=value;
}
public int getCell(int x, int y) {
x = calcX(x);
y = calcY(y);
return data[x][y];
}
public int getCellsCount(int i, int j) {
int result=0;
if( getCell(i-1, j+1) == 1 ) result++;
if( getCell(i-1, j-1) == 1 ) result++;
if( getCell(i-1, j) == 1 ) result++;
if( getCell(i, j-1) == 1 ) result++;
if( getCell(i+1, j+1) == 1 ) result++;
if( getCell(i+1, j-1) == 1 ) result++;
if( getCell(i+1, j) == 1 ) result++;
if( getCell(i, j+1) == 1 ) result++;
return result;
}
}