This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
KnightMoves.java
119 lines (104 loc) · 3.83 KB
/
KnightMoves.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
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.BorderFactory;
import javax.swing.SwingConstants;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
public class KnightMoves {
public static final int BOARD_SIZE = 8;
public static final int[] d1 = { 2, -2 };
public static final int[] d2 = { 1, -1 };
public static final ArrayList<ArrayList<Point>> solutions = new ArrayList<>();
public static void main(String[] args) {
ArrayList<Point> moves;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
moves = new ArrayList<Point>();
moves.add(new Point(i, j));
moveKnight(moves, i, j);
}
}
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setSize(480, 480);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(BOARD_SIZE, BOARD_SIZE));
JLabel[][] fields = new JLabel[BOARD_SIZE][BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
fields[i][j] = new JLabel();
fields[i][j].setHorizontalAlignment(SwingConstants.CENTER);
fields[i][j].setOpaque(true);
fields[i][j].setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
fields[i][j].setBackground(Color.WHITE);
fields[i][j].setForeground(Color.WHITE);
fields[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
f.add(fields[i][j]);
}
}
f.setVisible(true);
Thread t = new Thread(() -> {
Iterator<Point> iter = solutions.get(0).iterator();
int count = 1;
while (iter.hasNext()) {
Point p = iter.next();
fields[p.x][p.y].setBackground(Color.DARK_GRAY);
fields[p.x][p.y].setText(String.valueOf(count++));
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
});
}
public static void moveKnight(ArrayList<Point> moves, int x, int y) {
ArrayList<Point> options = new ArrayList<>();
for (int i : d1) {
for (int j : d2) {
options.add(new Point(x + i, y + j));
options.add(new Point(x + j, y + i));
}
}
if (solutions.size() < 2)
move(moves, options);
}
public static void move(ArrayList<Point> moves, ArrayList<Point> options) {
ArrayList<Point> saveMoves = new ArrayList<>(moves);
Iterator<Point> iter = options.iterator();
Point p;
int x;
int y;
boolean flag = false;
while (iter.hasNext()) {
p = iter.next();
x = (int) p.getX();
y = (int) p.getY();
if (isAppropriate(p) && !moves.contains(p)) {
moves.removeIf(e -> true);
moves.addAll(saveMoves);
moves.add(new Point(x, y));
moveKnight(moves, x, y);
if (!flag)
flag = true;
}
}
if (!flag && moves.size() == BOARD_SIZE * BOARD_SIZE) {
solutions.add(moves);
}
}
public static boolean isAppropriate(Point p) {
int x = (int) p.getX();
int y = (int) p.getY();
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE;
}
}