-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEightneighbor.java
More file actions
90 lines (79 loc) · 2.42 KB
/
Copy pathEightneighbor.java
File metadata and controls
90 lines (79 loc) · 2.42 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
import java.io.*;
import java.util.*;
public class Eightneighbor{
public static void main(String [] a){
boolean[][] array = {{true,true,false,false},
{false,false,true,true},
{true,true,false,false},
{true,false,false,true}
};
Location loc = new Location(0,3);
System.out.println("Element: " + "[" + loc.getRow() + "]" + "[" + loc.getCol() + "]");
int i = 0;
int j=0;
while(i<array.length){//prints initial boolean array
j=0;
while(j<array[0].length){
if(array[i][j]==true){
System.out.print("t ");
}
else{
System.out.print("f ");
}
j++;
}
System.out.println();
i++;
}
ArrayList<Location> list = trueNeighbors(array, loc);
for(Location l : list){
System.out.println("["+l.getRow()+", " + l.getCol()+"]");//prints locations
}
}
public static ArrayList<Location> trueNeighbors(boolean[][] array, Location loc){
//parameters: boolean[][] array, specified location
//purpose: returns ArrayList with locations of true values in the inputted boolean[][]
ArrayList <Location> list = new ArrayList<Location>();
if(loc.getRow()>0){
if(loc.getCol()>0 && array[loc.getRow()-1][loc.getCol()-1]==true){
list.add(new Location(loc.getRow()-1,loc.getCol()-1));
//northwest case
}
if(array[loc.getRow()-1][loc.getCol()] == true){
list.add(new Location(loc.getRow() - 1, loc.getCol()));
//north case
}
if(loc.getCol()<array[loc.getRow()].length-1 && array[loc.getRow()-1][loc.getCol()+1]){
list.add(new Location(loc.getRow()-1, loc.getCol()+1));
//northeast case
}
}
if(loc.getCol()>0){
if(array[loc.getRow()][loc.getCol()-1] == true){
list.add(new Location(loc.getRow(), loc.getCol()-1));
//west case
}
}
if(loc.getCol()<array.length-1){
if(array[loc.getRow()][loc.getCol()+1] == true){
list.add(new Location(loc.getRow(), loc.getCol()+1));
//east case
}
}
if(loc.getRow()<array.length-1){
if(loc.getCol()>0 && array[loc.getRow()+1][loc.getCol()-1]){
list.add(new Location(loc.getRow()+1, loc.getCol()-1));
//southwest case
}
if(array[loc.getRow()+1][loc.getCol()] == true){
list.add(new Location(loc.getRow()+1, loc.getCol()));
//south case
}
if(loc.getCol()<array[loc.getRow()].length-1 && array[loc.getRow()+1][loc.getCol()+1]==true){
list.add(new Location(loc.getRow()+1,loc.getCol()+1));
//southeast case
}
}
return list;
}
}