-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipe.java
More file actions
132 lines (108 loc) · 3.44 KB
/
Pipe.java
File metadata and controls
132 lines (108 loc) · 3.44 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
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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pipepuzzle;
/**
*
* @author bLind
*/
public class Pipe {
public enum Color {
Black, Blue, Green, Red, Yellow
}
private Position end1;
private Position end2;
private Color color;
private boolean evaluated;
public Pipe(PipeConnector connector1, int pipeConnection1, PipeConnector connector2, int pipeConnection2, Color color) {
this(new Position(connector1.getDirection(), pipeConnection1, connector1), new Position(connector2.getDirection(), pipeConnection2, connector2), color);
connector1.addPipe(pipeConnection1, this);
connector2.addPipe(pipeConnection2, this);
}
public Pipe(Position end1, Position end2, Color color) {
this.end1 = end1;
this.end2 = end2;
this.color = color;
this.evaluated = false;
}
public int length() {
int length = 0;
length += follow(end1);
length += follow(end2);
this.setEvaluated(true);
if (length == 0) {
return 0;
}
return length + 1;
}
public int follow(Position end) {
Puzzle puzzle = end.connector.getCard().getPuzzle();
int oppositePipeConnection = puzzle.getOppositePipeConnection(end.connection);
int oppositePipeDirection = puzzle.getOppositeDirection(end.direction);
PipeConnector neighbourConnector = end.connector.getNeighbour();
if (neighbourConnector == null) {
return 0;
}
Pipe nextPipe = neighbourConnector.getPipe(oppositePipeConnection);
if (nextPipe == null) {
return 0;
}
if (nextPipe.getColor() != this.getColor()) {
return 0;
} else {
nextPipe.setEvaluated(true);
return 1 + nextPipe.follow(nextPipe.otherEnd(nextPipe.getEnd(oppositePipeDirection)));
}
}
public Position getEnd(int direction) {
if (this.end1.direction == direction) {
return this.end1;
}
if (this.end2.direction == direction) {
return this.end2;
}
return null;
}
public Position otherEnd(Position end) {
if (this.end1.equals(end)) {
return this.end2;
} else {
return this.end1;
}
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public boolean isEvaluated() {
return evaluated;
}
public void setEvaluated(boolean evaluated) {
this.evaluated = evaluated;
}
public String toString() {
return this.getColor().name() + " from " + PipePuzzle.getDirName(this.end1.direction) + " to " + PipePuzzle.getDirName(this.end2.direction);
}
public static class Position {
public int direction;
public int connection;
public PipeConnector connector;
public boolean equals(Position other) {
if (this.direction != other.direction) {
return false;
}
if (this.connection != other.connection) {
return false;
}
return true;
}
public Position(int direction, int connection, PipeConnector connector) {
this.direction = direction;
this.connection = connection;
this.connector = connector;
}
}
}