-
Notifications
You must be signed in to change notification settings - Fork 50
/
snake.java
41 lines (35 loc) · 1.06 KB
/
snake.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
public class Board {
final int ROW_COUNT, COL_COUNT;
private Cell[][] cells;
public Board(int rowCount, int columnCount)
{
ROW_COUNT = rowCount;
COL_COUNT = columnCount;
cells = new Cell[ROW_COUNT][COL_COUNT];
for (int row = 0; row < ROW_COUNT; row++) {
for (int column = 0; column < COL_COUNT; column++) {
cells[row][column] = new Cell(row, column);
}
}
}
public Cell[][] getCells()
{
return cells;
}
public void setCells(Cell[][] cells)
{
this.cells = cells;
}
public void generateFood()
{
System.out.println("Going to generate food");
while(true){
int row = (int)(Math.random() * ROW_COUNT);
int column = (int)(Math.random() * COL_COUNT);
if(cells[row][column].getCellType()!=CellType.SNAKE_NODE)
break;
}
cells[row][column].setCellType(CellType.FOOD);
System.out.println("Food is generated at: " + row + " " + column);
}
}