public class Grid private int height width private Cell grid private M

 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
public class Grid
{
private int height, width;
private Cell[][] grid;
private Mode mode;
public Grid(int size)
{
this(size, Mode.ONE);
}
public Grid(int size, Mode mode)
{
this(size, size, mode);
}
public Grid(int height, int width, Mode mode)
{
this.height = height;
this.width = width;
grid = new Cell[height][width];
Cell.resetId();
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
grid[i][j] = new Cell();
}
}
this.mode = mode;
}
public Cell get(int x, int y)
{
return grid[x][y];
}
public Point[] getNeighbours(Point p)
{
Point[] result;
int counter = 0;
if (mode == Mode.ONE)
{
result = new Point[4];
for (int i = 0; i < 2; i++)
{
result[counter++] = new Point(p.getX() + i, p.getY() + i - 1);
result[counter++] = new Point(p.getX() - i, p.getY() - i + 1);
}
} else
{
result = new Point[8];
for (int i = -1; i < 2; i++)
{
for (int j = -1; j < 2; j++)
{
if (i == j && i == 0)
continue;
result[counter++] = new Point(p.getX() + i, p.getY() + j);
}
}
}
return result;
}
}