-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnoten.java
140 lines (111 loc) · 3.04 KB
/
Knoten.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import java.util.ArrayList;
import java.util.Arrays;
public class Knoten {
private int[][] field;
private Knoten parent;
public Knoten() {
initField();
setParent(null);
}
/*Used when creating child nodes
* Parameters:
* field of Parent node
* Coordinates of the field to be swapped with the empty field */
public Knoten(int[][] field,int posx, int posy,Knoten parent)
{
initField();
setParent(parent);
setField(field);
swap(posx,posy);
}
public void initField(){
this.field = new int[3][3];
}
public Knoten getParent()
{
return this.parent;
}
public int getNumber(int posx, int posy)
{
return this.field[posx][posy];
}
public void setNumber(int posx, int posy, int value)
{
this.field[posx][posy] = value;
}
public int[] getPosition(int value)
{
int[] pos = new int[2];
for(int x = 0;x < 3;x++){
for(int y = 0;y < 3;y++){
if(getNumber(x,y) == value){
pos[0] = x;
pos[1] = y;
return pos;
}
}
}
return null;
}
public void setField(int[][] field)
{
for(int x = 0;x < 3;x++){
for(int y = 0;y < 3;y++){
setNumber(x,y,field[x][y]);
}
}
}
public int[][] getField()
{
return this.field;
}
public void setParent(Knoten parent) {
this.parent = parent;
}
//Swaps given Field with empty Field
public void swap(int posx, int posy)
{
int tmp = getNumber(posx,posy);
int[] zero = getPosition(0);
setNumber(posx,posy,0);
setNumber(zero[0],zero[1],tmp);
}
public void printKnoten(){
for (byte x = 0;x < 3;x++){
for (byte y = 0;y < 3;y++) {
System.out.print(getNumber(x,y)+" ");
}
System.out.println();
}
System.out.println();
}
public ArrayList<Knoten> getChildren(){
ArrayList<Knoten> children = new ArrayList<Knoten>();
int[] zero = getPosition(0);
if(zero[0] != 2) {
children.add(new Knoten(field,zero[0]+1,zero[1],this));
}
if(zero[0] != 0) {
children.add(new Knoten(field,zero[0]-1,zero[1],this));
}
if(zero[1] != 2) {
children.add(new Knoten(field, zero[0],zero[1]+1,this));
}
if(zero[1] != 0) {
children.add(new Knoten(field, zero[0],zero[1]-1,this));
}
return children;
}
public ArrayList<Knoten> getChildrenNoCycle(){
ArrayList<Knoten> children = new ArrayList<Knoten>();
for (Knoten i:getChildren()){
if(getParent() != null) {
if (!Arrays.deepEquals(getParent().getField(), i.getField())) {
children.add(i);
}
}
else children.add(i);
}
return children;
}
}