-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzle.java
205 lines (175 loc) · 5.58 KB
/
Puzzle.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package pipepuzzle;
import java.awt.Point;
import java.util.ArrayList;
/**
*
* @author bLind
*/
public class Puzzle {
private static int TOP = PipePuzzle.DIR_TOP;
private static int LEFT = PipePuzzle.DIR_LEFT;
private static int BOTTOM = PipePuzzle.DIR_BOTTOM;
private static int RIGHT = PipePuzzle.DIR_RIGHT;
private ArrayList<Card> cards;
/**
* the number of possible directions, must be product of 2 because I'm lazy
* and no genius, I will set this to 4 and implement a 2D grid instead of
* something crazy
*/
private int numberOfDirections;
/**
* the number of pipe connections per card
*/
private int numberOfPipeConnections;
private Card[][] grid;
private int gridsize;
public Puzzle(int numberOfCards, int numberOfPipeConnections) {
this.numberOfDirections = 4;
this.numberOfPipeConnections = numberOfPipeConnections;
this.cards = new ArrayList<>();
this.gridsize = numberOfCards * 2 - 1;
this.grid = new Card[this.gridsize][this.gridsize];
}
public void setInitialCard(Card card) {
this.grid[this.gridsize / 2][this.gridsize / 2] = card;
this.cards.add(card);
}
public void addCard(Card card, Point location) {
this.addCard(card, location.x, location.y);
}
public void addCard(Card card, int x, int y) {
if (hasCard(x, y)) {
return;
}
this.cards.add(card);
this.grid[x][y] = card;
if (hasCard(x, y - 1)) {
card.setNeighbour(TOP, this.getCard(x, y - 1));
}
if (hasCard(x, y + 1)) {
card.setNeighbour(BOTTOM, this.getCard(x, y + 1));
}
if (hasCard(x - 1, y)) {
card.setNeighbour(LEFT, this.getCard(x - 1, y));
}
if (hasCard(x + 1, y)) {
card.setNeighbour(RIGHT, this.getCard(x + 1, y));
}
}
public void removeCard(Point location) {
this.removeCard(location.x, location.y);
}
public void removeCard(int x, int y) {
if (!hasCard(x, y)) {
System.err.println("Trying to remove card from empty grid cell.");
return;
}
Card card = this.grid[x][y];
if (hasCard(x, y - 1)) {
card.removeNeighbour(TOP);
}
if (hasCard(x, y + 1)) {
card.removeNeighbour(BOTTOM);
}
if (hasCard(x - 1, y)) {
card.removeNeighbour(LEFT);
}
if (hasCard(x + 1, y)) {
card.removeNeighbour(RIGHT);
}
this.cards.remove(card);
this.grid[x][y] = null;
}
public void hasCard(Point location) {
this.hasCard(location.x, location.y);
}
public boolean hasCard(int x, int y) {
if (x < 0 || y < 0 || x >= this.gridsize || y >= this.gridsize) {
return false;
}
return (this.grid[x][y] != null);
}
public Card getCard(int x, int y) {
return this.grid[x][y];
}
public boolean hasNeighbour(int x, int y) {
if (hasCard(x, y - 1)) {
return true;
}
if (hasCard(x, y + 1)) {
return true;
}
if (hasCard(x - 1, y)) {
return true;
}
if (hasCard(x + 1, y)) {
return true;
}
return false;
}
public ArrayList<Point> getAvailableLocations() {
ArrayList<Point> locations = new ArrayList<>();
for (int x = 0; x < this.gridsize; x++) {
for (int y = 0; y < this.gridsize; y++) {
if (!hasCard(x, y) && hasNeighbour(x, y)) {
locations.add(new Point(x, y));
}
}
}
return locations;
}
public int getCurrentScore() {
int score = 0;
int counter = 0;
for (Card card : this.cards) {
counter++;
for (Pipe pipe : card.getPipes()) {
if (pipe.isEvaluated()) {
continue;
}
score += pipe.length();
pipe.setEvaluated(true);
}
}
// reset pipe-evaluation status
for (Card card : this.cards) {
for (Pipe pipe : card.getPipes()) {
pipe.setEvaluated(false);
}
}
return score;
}
/**
* Tells you the id of the opposite of the given direction. if you have a
* square with directions 0, 1, 2 and 3 getOppositeDirection(0) will return
* 2 getOppositeDirection(3) will return 1, and so on
*
* @param direction the direction you need the opposite of
* @return id of the opposite direction
*/
public int getOppositeDirection(int direction) {
return (direction + this.numberOfDirections / 2) % this.numberOfDirections;
}
public int getOppositePipeConnection(int pipeConnection) {
int offset = (this.numberOfPipeConnections % 2 == 0) ? 1 : 0;
return (pipeConnection - this.numberOfPipeConnections / 2) * (-1) + (this.numberOfPipeConnections / 2) - offset;
}
public String toString() {
String grid = "";
for (int y = 0; y < this.gridsize; y++) {
for (int x = 0; x < this.gridsize; x++) {
if (hasCard(x, y)) {
grid += getCard(x, y).getName() + (getCard(x, y).isUpSideDown() ? ". " : "' ");
} else {
grid += "X ";
}
}
grid += '\n';
}
return grid;
}
}