-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
108 lines (88 loc) · 2.71 KB
/
Card.java
File metadata and controls
108 lines (88 loc) · 2.71 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pipepuzzle;
import java.util.ArrayList;
import java.util.HashMap;
/**
*
* @author bLind
*/
public class Card {
private String name;
private boolean upSideDown;
private Puzzle puzzle;
private ArrayList<Pipe> pipes;
/**
* Maps from the direction to the corresponding neighbour card
*/
private HashMap<Integer, Card> neighbours;
/**
* Maps from the direction to the corresponding pipeConnector
*/
private HashMap<Integer, PipeConnector> pipeConnectors;
public Card(Puzzle puzzle, String name) {
this.puzzle = puzzle;
this.pipes = new ArrayList<>();
this.neighbours = new HashMap<>();
this.pipeConnectors = new HashMap<>();
this.name = name;
this.upSideDown = false;
}
public void setNeighbour(int direction, Card neighbour) {
int oppositeDirection = puzzle.getOppositeDirection(direction);
this.neighbours.put(direction, neighbour);
neighbour.neighbours.put(oppositeDirection, this);
this.pipeConnectors.get(direction).setNeighbour(neighbour.getPipeConnector(oppositeDirection));
}
public Card getNeighbour(int direction) {
return this.neighbours.get(direction);
}
public int getNeighbourCount() {
return this.neighbours.size();
}
public void removeNeighbour(int direction) {
int oppositeDirection = this.puzzle.getOppositeDirection(direction);
Card neighbour = this.getNeighbour(direction);
if (neighbour == null) {
return;
}
this.neighbours.remove(direction);
neighbour.neighbours.remove(oppositeDirection);
this.pipeConnectors.get(direction).removeNeighbour();
}
private void addPipe(Pipe pipe) {
pipes.add(pipe);
}
public ArrayList<Pipe> getPipes() {
return this.pipes;
}
public void addPipeConnector(PipeConnector pipeConnector) {
this.pipeConnectors.put(pipeConnector.getDirection(), pipeConnector);
for (Pipe pipe : pipeConnector.getPipes()) {
if (this.pipes.contains(pipe)) {
continue;
}
this.addPipe(pipe);
}
}
public PipeConnector getPipeConnector(int direction) {
return this.pipeConnectors.get(direction);
}
public Puzzle getPuzzle() {
return puzzle;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void rotate() {
this.upSideDown = !this.upSideDown;
}
public boolean isUpSideDown() {
return upSideDown;
}
}