-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnightsTourGUI.java
163 lines (135 loc) · 5.02 KB
/
KnightsTourGUI.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
import javax.swing.*;
import java.awt.*;
import java.util.concurrent.TimeUnit;
import java.util.Scanner;
public class KnightsTourGUI extends JPanel {
private static final int CELL_SIZE = 50; // Size of each cell in pixels
private int[][] board;
private int boardSize;
public KnightsTourGUI(int size) {
this.boardSize = size;
this.board = new int[size][size];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
board[i][j] = -1;
}
}
setPreferredSize(new Dimension(size * CELL_SIZE, size * CELL_SIZE));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < boardSize; i++) {
for (int j = 0; j < boardSize; j++) {
// Alternate cell color
if ((i + j) % 2 == 0) {
g.setColor(Color.LIGHT_GRAY);
} else {
g.setColor(Color.WHITE);
}
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
// Draw green color for visited cells
if (board[i][j] >= 0) {
g.setColor(Color.GREEN);
g.fillRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
}
// Draw grid and move numbers
g.setColor(Color.BLACK);
g.drawRect(j * CELL_SIZE, i * CELL_SIZE, CELL_SIZE, CELL_SIZE);
if (board[i][j] >= 0) {
g.setColor(Color.BLACK);
g.drawString(String.valueOf(board[i][j]), j * CELL_SIZE + CELL_SIZE / 2 - 5,
i * CELL_SIZE + CELL_SIZE / 2 + 5);
}
}
}
}
public void updateBoard(int row, int col, int move) {
board[row][col] = move;
repaint();
try {
TimeUnit.MILLISECONDS.sleep(300); // Delay for visualization
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static boolean isValid(int row, int col, int[][] board) {
int N = board.length;
return row >= 0 && row < N && col >= 0 && col < N && board[row][col] == -1;
}
private static int degree(int[][] board, int row, int col) {
int[][] actions = { { 1, 2 }, { 2, 1 }, { -1, 2 }, { -2, 1 }, { -1, -2 }, { -2, -1 }, { 1, -2 }, { 2, -1 } };
int count = 0;
for (int[] action : actions) {
int nr = row + action[0];
int nc = col + action[1];
if (isValid(nr, nc, board)) {
count++;
}
}
return count;
}
private static boolean solveTour(int row, int col, int moves, int[][] board, KnightsTourGUI gui) {
int N = board.length;
if (moves == N * N - 1) {
return true;
}
int[][] actions = { { 1, 2 }, { 2, 1 }, { -1, 2 }, { -2, 1 }, { -1, -2 }, { -2, -1 }, { 1, -2 }, { 2, -1 } };
int min_deg = Integer.MAX_VALUE, min_row = -1, min_col = -1;
for (int[] action : actions) {
int newRow = row + action[0];
int newCol = col + action[1];
if (isValid(newRow, newCol, board)) {
int newDeg = degree(board, newRow, newCol);
if (newDeg < min_deg) {
min_deg = newDeg;
min_row = newRow;
min_col = newCol;
}
}
}
if (min_row == -1) {
return false;
}
board[min_row][min_col] = moves + 1;
gui.updateBoard(min_row, min_col, moves + 1);
if (solveTour(min_row, min_col, moves + 1, board, gui)) {
return true;
}
board[min_row][min_col] = -1;
gui.updateBoard(min_row, min_col, -1);
return false;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the size of the board: ");
int N = in.nextInt();
System.out.print("Enter the starting coordinates: ");
int px = in.nextInt();
int py = in.nextInt();
if (px < 0 || px >= N || py < 0 || py >= N) {
System.out.println("Invalid starting coordinates. Exiting.");
in.close();
return;
}
JFrame frame = new JFrame("Knight's Tour Visualization");
KnightsTourGUI gui = new KnightsTourGUI(N);
frame.add(gui);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
int[][] board = new int[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = -1;
}
}
board[px][py] = 0;
gui.updateBoard(px, py, 0);
boolean solved = solveTour(px, py, 0, board, gui);
if (!solved) {
System.out.println("No solution exists for the given starting position.");
}
in.close();
}
}