Repository navigation
Expand file tree
/
Copy pathCreature.java
More file actions
107 lines (97 loc) · 2.85 KB
/
Copy pathCreature.java
File metadata and controls
107 lines (97 loc) · 2.85 KB
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
public abstract class Creature extends Life
{
//set up booleans for live and reproduce
//continually updated
protected boolean live = true;
protected boolean reproduce = false;
//set up row and col for each creature
protected int row, col;
public Creature(int row, int col)
{
this.row = row;
this.col = col;
}
//accessors
public boolean getLive()
{
return live;
}
public int getRow ()
{
return row;
}
public int getCol()
{
return col;
}
public boolean getReproduce()
{
return reproduce;
}
//change row and col, so object's row and col are the same as the one in grid
public void changeRow (int r)
{
row = r;
}
public void changeCol (int c)
{
col = c;
}
//accepts grid, row, and col
//returns x and y positions of new creature to be made in int[]position
public int[] reproduce(Life[][]grid, int row, int col)
{
//set up array to store x and y
int[] position = new int[2];
position[0] = -1;
position[1] = -1;
//only do if creature can reproduce
if(reproduce)
{
//hard coded all cases
//==null means cell is empty
//check if row and col are within grid
if(row-1 >= 0 && grid[row-1][col] == null)
{
position[0] = row-1;
position[1] = col;
}
else if (row-1 >= 0 && col-1 >= 0 && grid[row-1][col-1] == null)
{
position[0] = row-1;
position[1] = col-1;
}
else if (row-1 >= 0 && col+1 < grid[0].length && grid[row-1][col+1] == null)
{
position[0] = row-1;
position[1] = col+1;
}
else if (row+1 < grid.length && grid[row+1][col] == null)
{
position[0] = row+1;
position[1] = col;
}
else if (row+1 < grid.length && col-1 >= 0 && grid[row+1][col-1] == null)
{
position[0] = row+1;
position[1] = col-1;
}
else if (row+1 < grid.length && col+1 < grid[0].length && grid[row+1][col+1] == null )
{
position[0] = row+1;
position[1] = col+1;
}
else if (col-1 >= 0 && grid[row][col-1] == null)
{
position[0] = row;
position[1] = col-1;
}
else if(col+1 < grid[0].length && grid[row][col] == null)
{
position[0] = row;
position[1] = col+1;
}
}
return position;
}
}