-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessChallenge.java
More file actions
284 lines (219 loc) · 8.29 KB
/
ChessChallenge.java
File metadata and controls
284 lines (219 loc) · 8.29 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import java.sql.Date;
import java.util.HashSet;
public class ChessChallenge {
HashSet<String> solutions;
private int [] config;
private int rowsNumber;
private int colsNumber;
private long init;
private long end;
private static final int CONF_QUEEN_INDEX = 0;
private static final int CONF_BISHOP_INDEX = 1;
private static final int CONF_ROOK_INDEX = 2;
private static final int CONF_KING_INDEX = 3;
private static final int CONF_KNIGHT_INDEX = 4;
/**
* Run the program from command line
* rm *.class && javac *.java && java ChessChallenge
*
* @param args [description]
*/
public static void main(String[] args) {
// int rows = 3;
// int columns = 3;
//
// int nKings = 0;
// int nQueens = 2;
// int nBishops = 0;
// int nKnights = 0;
// int nRooks = 0;
//BIG PROBLEM
int rows = 7;
int columns = 7;
int nKings = 2;
int nQueens = 2;
int nBishops = 2;
int nKnights = 1;
int nRooks = 0;
if (rows == columns){
ChessChallenge problem = new ChessChallenge(rows, columns, nKings, nQueens, nBishops, nRooks, nKnights);
problem.printSolutions();
} else{
System.out.println("The dimensions of the board have to be square");
}
}
/**
* Problem constructor and entry point of the application
*
*
* @param rowsNumber
* @param columnsNumber
* @param nKings
* @param nQueens
* @param nBishops
* @param nRooks
* @param nKnights
*/
public ChessChallenge(int rowsNumber, int columnsNumber, int nKings, int nQueens, int nBishops, int nRooks, int nKnights) {
this.init = System.currentTimeMillis();
this.rowsNumber = rowsNumber;
this.colsNumber = columnsNumber;
solutions = new HashSet<String>();
ChessBoard board = new ChessBoard(rowsNumber, columnsNumber);
this.config = new int[]{nQueens, nBishops, nRooks, nKings, nKnights};
solve(this.config, board);
long elapsed = System.currentTimeMillis() - init;
System.out.println("elapsed time: " + elapsed + " ms.");
}
/**
* Count the number of pieces in the configuration
*
* @param configuration
* @return int
*/
private int countPieces(int [] configuration) {
return configuration[ChessChallenge.CONF_QUEEN_INDEX] +
configuration[ChessChallenge.CONF_BISHOP_INDEX] +
configuration[ChessChallenge.CONF_ROOK_INDEX] +
configuration[ChessChallenge.CONF_KING_INDEX] +
configuration[ChessChallenge.CONF_KNIGHT_INDEX];
}
/**
* Returns the number of different solutions with the given configuration
*
* @param []conf
* @param board
* @param rowIndex
* @param colIndex
* @return
*/
public void solve(int [] conf, ChessBoard board) {
//printConfiguration(conf, board.getRowCount(), board.getColumnCount());
if(countPieces(conf) == 0 && !solutions.contains(board.getHashCode())) {
//We get a solution!!
//each solution gives us another 3 (board rotations)
addSolutionAndRotations(board);
return;
}
for(int i = 0; i < board.getRowCount(); i++) {
for (int j = 0; j < board.getColumnCount(); j++) {
putPiece(getConfigCopy(conf), board, i, j, CONF_QUEEN_INDEX);
putPiece(getConfigCopy(conf), board, i, j, CONF_BISHOP_INDEX);
putPiece(getConfigCopy(conf), board, i, j, CONF_ROOK_INDEX);
putPiece(getConfigCopy(conf), board, i, j, CONF_KNIGHT_INDEX);
putPiece(getConfigCopy(conf), board, i, j, CONF_KING_INDEX);
}
}
}
/**
* Returns a copy of the given configuration array
*
* @param conf
* @return
*/
private int[] getConfigCopy(int [] conf) {
int [] configCopy = new int[conf.length];
System.arraycopy(conf, 0, configCopy, 0, conf.length);
return configCopy;
}
/**
* If we have pieces of the given index into the configuration array,
* then we check if we can put the piece
*
* @param conf
* @param board
* @param rowIndex
* @param colIndex
* @param confIndex
* @param pieceName
*/
private void putPiece (int [] conf, ChessBoard sourceBoard, int x, int y, int pieceIndex) {
if(conf[pieceIndex] > 0) {
ChessBoard updatedBoard = new ChessBoard(sourceBoard);
boolean piecePlaced = false;
if(pieceIndex == CONF_KING_INDEX) {
piecePlaced = updatedBoard.putKing(x, y);
}else if(pieceIndex == CONF_QUEEN_INDEX) {
piecePlaced = updatedBoard.putQueen(x, y);
}else if(pieceIndex == CONF_BISHOP_INDEX) {
piecePlaced = updatedBoard.putBishop(x, y);
}else if(pieceIndex == CONF_ROOK_INDEX) {
piecePlaced = updatedBoard.putRook(x, y);
}else if(pieceIndex == CONF_KNIGHT_INDEX) {
piecePlaced = updatedBoard.putKnight(x, y);
}
if(piecePlaced) {
conf[pieceIndex]--;
solve(conf, updatedBoard);
}else {
return;
}
}
}
/**
* Adds the hashCode of the board solutions and the 3 board rotations.
* The contains check is for this scenario (with any piece):
*
* -1 -1 -1
* -1 Q -1
* -1 -1 -1
*
* the three rotations are equal to the first board state
*
* @param board
*/
private void addSolutionAndRotations(ChessBoard board) {
ChessBoard aux = new ChessBoard(board);
if(!solutions.contains(aux.getHashCode())){
//aux.printBoard();
solutions.add(aux.getHashCode());
}
aux.rotate();
if(!solutions.contains(aux.getHashCode())){
//aux.printBoard();
solutions.add(aux.getHashCode());
}
aux.rotate();
if(!solutions.contains(aux.getHashCode())){
//aux.printBoard();
solutions.add(aux.getHashCode());
}
aux.rotate();
if(!solutions.contains(aux.getHashCode())) {
//aux.printBoard();
solutions.add(aux.getHashCode());
}
System.out.println(solutions.size() + " solutions");
}
/**
* Helper function for print all the solutions to the given configuration
*/
public void printSolutions() {
int numberOfSolutions = solutions.size();
printConfiguration(this.config, rowsNumber, colsNumber);
if(numberOfSolutions > 0) {
if(numberOfSolutions == 1) {
System.out.println("There is only one solution :)");
}else {
System.out.println("There are " + numberOfSolutions + " solutions :)");
}
}else {
System.out.println("There is no solutions for the given configuration :(");
}
}
/**
* Helper function for print the given configuration
*/
private void printConfiguration(int [] configuration, int rows, int columns) {
if(configuration.length == 5 ) {
StringBuilder confString = new StringBuilder("Configuration ");
confString.append(rows).append("x").append(columns).append(": ");
confString.append(configuration[0]).append(" queens, ");
confString.append(configuration[1]).append(" bishops, ");
confString.append(configuration[2]).append(" rooks, ");
confString.append(configuration[3]).append(" kings, ");
confString.append(configuration[4]).append(" knights");
System.out.println(confString.toString());
}
}
}