-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnightsTour.java
164 lines (147 loc) · 5.17 KB
/
KnightsTour.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
import java.util.LinkedList;
public class KnightsTour {
public static void main(String[] args) {
Knight k = new Knight(8);
}
}
class Table {
public static int[][] table;
int length;
public Table(int userLength) {
length = userLength;
table = new int[length][length];
}
public void setTable(Position p ,int value) {
int i = p.getX();
int j = p.getY();
if(i - 1 > table.length || i - 1 < 0 || j - 1 > table.length || j - 1 < 0) {
System.out.println("Position " + i + " " + j + " Invalid");
} else {
table[i - 1][j - 1] = value;
//System.out.println("Position " + i + " " + j + " Set");
}
}
public int getTableValue(Position p) {
return (table[p.getX() - 1][p.getY() - 1]);
}
public void displayTable() {
for(int rowNum = 0; rowNum < length; rowNum++) {
System.out.println();
for(int colNum = 0; colNum < length; colNum++) {
System.out.print(" " + table[rowNum][colNum] + " ");
}
}
System.out.println();
}
public int getTableLength() {
return length;
}
}
class Position {
int x;
int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
class Knight {
private Position startPos;
public Knight(int length) {
Table bestTable = null;
int bestVal = 0;
for(int testNum = 0; testNum < 10000001; testNum++) {
Table table = new Table(length);
int startX = 1 + (int)(Math.random() * ((length) - 1));
int startY = 1 + (int)(Math.random() * ((length) - 1));
startPos = new Position(startX, startY);
int iterVal = iterateTour(table, startPos) - 1;
if(iterVal > bestVal) {
table.displayTable();
bestTable = table;
bestVal = iterVal;
if(iterVal == 64) {
System.out.println("Iteration Number : " + testNum);
testNum = 10000002;
}
}
}
System.out.println(bestVal);
}
public int iterateTour(Table table, Position startPos) {
Position newP = startPos;
int iteration = 1;
//System.out.println(newP.getX() + " " + newP.getY());
while(newP != null) {
table.setTable(newP, iteration++);
//System.out.println("Start Position Set at " + newP.getX() + " " + newP.getY());
newP = getNextPosition(newP, table);
}
return iteration;
}
private Position getNextPosition(Position currentPosition, Table table) {
int x = currentPosition.getX();
int y = currentPosition.getY();
LinkedList<Position> possiblePositions = getListOfPossiblePositions(x, y, table);
return decidePosition(possiblePositions, table);
}
private Position decidePosition(LinkedList<Position> openPositions, Table table) {
// Define Algorithm
int algo = 2;
int pos = 0;
if(openPositions.size() == 0) {
return null;
} else {
if(algo == 1) {
pos = (int)(Math.random() * (openPositions.size()));
} else if(algo == 2) {
int valToChoose = 0;
int minMoves = 9999;
for(int mov = 0; mov < openPositions.size(); mov++) {
Position checkPos = openPositions.get(mov);
int posMoves = getListOfPossiblePositions(checkPos.getX(), checkPos.getY(), table).size();
if(posMoves < minMoves) {
valToChoose = mov;
minMoves = posMoves;
}
}
pos = valToChoose;
}
return openPositions.get(pos);
}
}
private LinkedList<Position> getListOfPossiblePositions(int x, int y, Table table) {
int length = table.getTableLength();
LinkedList<Position> openSpots = new LinkedList<Position>();
openSpots.add(new Position(x - 2, y + 1));
openSpots.add(new Position(x - 2, y - 1));
openSpots.add(new Position(x + 2, y + 1));
openSpots.add(new Position(x + 2, y - 1));
openSpots.add(new Position(x + 1, y + 2));
openSpots.add(new Position(x + 1, y - 2));
openSpots.add(new Position(x - 1, y + 2));
openSpots.add(new Position(x - 1, y - 2));
int iterator = 0;
while(iterator < openSpots.size()) {
Position posIteration = openSpots.get(iterator);
int posX = posIteration.getX();
int posY = posIteration.getY();
if(posX < 1 || posX > length || posY < 1 || posY > length) {
openSpots.remove(iterator);
} else {
if (table.getTableValue(posIteration) != 0) {
openSpots.remove(iterator);
} else {
iterator++;
}
}
}
return openSpots;
}
}