-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModel.java
47 lines (36 loc) · 992 Bytes
/
Model.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
import java.util.Random;
public class Model {
private Cell[][] _matrix;
public Model()
{
_matrix = new Cell[GameManager.ARENA_WIDTH][GameManager.ARENA_HEIGHT];
for(int i = 0; i < GameManager.ARENA_WIDTH; i++)
{
for(int j = 0; j < GameManager.ARENA_HEIGHT; j++)
{
_matrix[i][j] = new Cell(GameManager.CELL_SIZE);
}
}
}
public Cell getCell(int x, int y)
{
return _matrix[x][y];
}
public void placeRandomFood()
{
Random rand = new Random();
while(true)
{
int randX = (rand.nextInt(Integer.MAX_VALUE)) % GameManager.ARENA_WIDTH;
int randY = (rand.nextInt(Integer.MAX_VALUE)) % GameManager.ARENA_HEIGHT;
Cell c = getCell(randX, randY);
if(randX == 0 && randY == 0 || randX == GameManager.ARENA_WIDTH && randY == 0 || randX == 0 && randY == GameManager.ARENA_HEIGHT || randX == GameManager.ARENA_WIDTH && randY == GameManager.ARENA_HEIGHT)
continue;
if(!c.isAlive())
{
c.makeFood();
return;
}
}
}
}