-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimumStepsChessKnight.java
More file actions
124 lines (102 loc) · 3.11 KB
/
Copy pathMinimumStepsChessKnight.java
File metadata and controls
124 lines (102 loc) · 3.11 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
package edu.gwu.csci6212;
import java.util.LinkedList;
public class MinimumStepsChessKnight {
private boolean [][] board;
private Position start;
private Position goal;
private LinkedList<Position> queue;
private boolean gameOver;
private Position[] knightMovements;
private int size;
private int totalSteps;
MinimumStepsChessKnight(){
this.queue = new LinkedList<>();
this.gameOver = false;
this.knightMovements = new Position [] {
new Position(1,2,1),
new Position(1,-2,1),
new Position(2,1,1),
new Position(2,-1,1),
new Position(-2,1,1),
new Position(-2,-1,1),
new Position(-1,2,1),
new Position(-1,-2,1),
};
}
public class Position {
public int x;
public int y;
public int steps;
Position(){
this.x = 0;
this.y = 0;
this.steps = 0;
}
Position(int x, int y, int steps){
this.x = x;
this.y = y;
this.steps = steps;
}
public void add(Position p1){
this.x += p1.x;
this.y += p1.y;
this.steps += p1.steps;
}
public boolean equals(Position p2){
return (this.x == p2.x && this.y == p2.y);
}
}
public int startGame(int size, int[] start_array, int[] goal_array){
this.size = size;
board = new boolean[size][size];
start = new Position(start_array[0], start_array[1], 0);
goal = new Position(goal_array[0], goal_array[1], 0);
if (outOfBounds()) return -1;
if (start.equals(goal)){
return 0;
}
else{
board [start.x][start.y] = true;
queue.add(start);
while(!gameOver){
calculateSquares(queue.remove());
}
return totalSteps;
}
}
public void calculateSquares(Position origin){
for (Position knightMove : knightMovements){
Position temp = new Position();
calculateMovement(temp, origin, knightMove);
if(isInsideLimits(temp)){
if(!isUsed(temp)){
makeMovement(temp);
}
}
isGameOver(temp);
}
}
private boolean outOfBounds(){
return !isInsideLimits(start) || !isInsideLimits(goal);
}
private boolean isInsideLimits(Position temp){
return temp.x < size && temp.y < size && temp.x > 0 && temp.y > 0;
}
private boolean isUsed(Position temp){
return (board[temp.x][temp.y] == true);
}
private void makeMovement(Position temp){
queue.add(temp);
board[temp.x][temp.y] = true;
}
private void calculateMovement(Position temp, Position origin, Position knightMove){
temp.add(origin);
temp.add(knightMove);
}
private void isGameOver(Position temp){
if(temp.equals(goal)){
gameOver = true;
totalSteps = temp.steps;
}
}
}