-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneration.pde
132 lines (121 loc) · 2.5 KB
/
Generation.pde
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
public void drawCells(Cell[][] cells)
{
for(int i = 0;i < RES_X ; i++)
{
for(int j = 0; j < RES_Y ; j++)
{
cells[i][j].drawIt();
}
}
}
public void updateCells(Cell[][] cells)
{
boolean[][] lives = new boolean[RES_X][RES_Y];
for(int i = 0;i < RES_X ; i++)
{
for(int j = 0; j < RES_Y ; j++)
{
lives[i][j] = checkNeighborhood(cells, i, j);
}
}
for(int i = 0;i < RES_X ; i++)
{
for(int j = 0; j < RES_Y ; j++)
{
cells[i][j].setLive(lives[i][j]);
}
}
}
public boolean checkNeighborhood(Cell[][] cells, int x, int y)
{
// 4 teams counter
int[] counter = new int[PLAYERS+1];
// removes the center cell
if(cells[x][y].getLive())
counter[cells[x][y].getTeam()]--;
for(int i = -1 ;i <= 1 ; i++)
{
for(int j = -1; j <= 1 ; j++)
{
int col = (x + i + RES_X) % RES_X;
int row = (y + j + RES_Y) % RES_Y;
//println("cols " + col + " row " + row);
counter[cells[col][row].getTeam()] += cells[col][row].getLive() ? 1 : 0;
}
}
boolean ret = false;
for(int i = 0; i<PLAYERS+1 && !ret; i++)
{
if(counter[i]<=1 || counter[i]>3)
{
ret = false;
}
else if(counter[i]==3)
{
ret = true;
cells[x][y].setTeam(findBiggestIndex(counter)+1);
}
else
{
ret = cells[x][y].getLive();
}
}
return ret;
}
public int findBiggestIndex(int[] arr)
{
int max = max(arr);
for(int i = 0; i<arr.length;i++)
if(arr[i] == max)
return i;
return 1;
}
public void userClick(Cell[][] cells, boolean live, int team, int radius)
{
int x = mouseX/SIZE;
int y = mouseY/SIZE;
if (radius == 0)
{
cells[x][y].setLive(live);
cells[x][y].setTeam(team);
}
else
{
for(int i=-radius;i<radius;i++)
{
if(x+i<0 || x+i>=RES_X)
continue;
for(int j=-radius;j<radius;j++)
{
if(y+j<0 || y+j>=RES_Y)
continue;
cells[x+i][y+j].setLive(live);
cells[x+i][y+j].setTeam(team);
}
}
}
}
public void clearScreen(Cell[][] cells)
{
for(int i = 0;i < RES_X ; i++)
{
for(int j = 0; j < RES_Y ; j++)
{
cells[i][j].setLive(false);
}
}
}
//public void userClick(Cell[][] cells, boolean live, int team, int radius)
//{
// for(int i = 0; i < RES_X ; i++)
// {
// for(int j = 0; j < RES_Y ; j++)
// {
// if(cells[i][j].pointInShape(mouseX, mouseY, radius))
// {
// cells[i][j].setLive(live);
// cells[i][j].setTeam(team);
// }
// }
// }
//}